Here is the deal... I am creating an app (from another one of my apps) but I am altering to from using only the NSDocumentDirectory, which obviously allows the user to see all of the user files, to seeing only a few of the files... namely user created PDFs.
I have it working... but, nothing shows in the FileSharing/Documents window in iTunes.
First are the methods that invoke the NSApplicationSupportDirectory in the persistence model...
+(NSString *)getDocumentpath
{
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
+(NSString *) documentsDirectoryPath
{
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
+(void) copyResourceFileToDocumentsDirectory: (NSString *) fileName
{
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager * fileManager = [NSFileManager defaultManager];
BOOL succeeded = [fileManager fileExistsAtPath:writablePath];
NSError *error;
//If file is not in the documents directory then only write
if (!succeeded)
{
NSString *newPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
succeeded = [fileManager copyItemAtPath:newPath toPath:writablePath error:&error];
if (succeeded == FALSE) {
NSLog(#"%# : copy failed", fileName);
} else {
NSLog(#"%# : copy success", fileName);
}
} else {
NSLog(#"%# : already exists", fileName);
}
}
This is the method for saving the PDF into the NSDocumentDirectory, which has not been changed from the other app...
- (NSString*)saveJournalToPDF:(UIView*)journal andName:(NSString*)name
{
NSString* fileName = [NSString stringWithFormat:#"%#.pdf",name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:fileName];
... // the rest of the data strings for creating the PDF
}
My question: how do I get ONLY the PDFs to be visible to the user without exposing the other data files? Right now, it seems that it is either all or nothing!
I neglected one small detail... setting the app to actually share files! The app's info.plist did not have "Application supports iTunes file sharing" set to YES. (UIFileSharingEnabled). Such a simple fix!!!
Related
I just want to replace static plist data to dynamic.this is my code
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"sample.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
i have some data in event array but its not writing/reading can you please suggest me where i am wrong.
NSLog(#"data coming from server....%#",self.eventDataArray);
//Writing array to document directory
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [docpaths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:#"%#/sample.plist", documentsDirectoryPath];
[self.eventDataArray writeToFile:path atomically:YES];
NSLog(#"plist path string....%#",path);
//reading from document directory
NSMutableArray *readingArray=[[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(#"plist reading data is here......%#",readingArray);
Using the code below is the only way I can get writeToFile to actually save a file. It outputs to this directory:
/Users/Eric/Library/Containers/net.ericmann.Event-Gadget-Workshop-App/Data/Documents/gadget.ics
- (IBAction)Saved:(id)sender {
[self writeToTextFile];
//mySaved.stringValue = [NSString stringWithFormat:#"Saved!"];
}
-(void) writeToTextFile{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSString *pathToDesktop = [NSString stringWithFormat:#"/Users/%#/Desktop", NSUserName()];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/gadget.ics", documentsDirectory];
//create content - four lines of text
NSString *content = myOutput.stringValue;
//save content to the documents directory
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
mySaved.stringValue = fileName;
myTestBox.stringValue = fileName;
}
Now, if I use the code below, nothing will write. I have the test file name output, and it's pointing here:
/Users/Eric/Desktop/gadget.ics
-(void) writeToTextFile{
//get the documents directory:
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
//NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathToDesktop = [NSString stringWithFormat:#"/Users/%#/Desktop", NSUserName()];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/gadget.ics", pathToDesktop];
//create content - four lines of text
NSString *content = myOutput.stringValue;
//save content to the documents directory
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
mySaved.stringValue = fileName;
myTestBox.stringValue = fileName;
You will likely end up figuring out a solution if you actually make use of the error parameter there in the "writeToFile" method:
e.g.:
NSError * error = NULL;
BOOL success = [content writeToFile:fileName atomically:NO encoding:NSUTF8StringEncoding error:&error];
if(success == NO)
{
NSLog( #"error saving to %# - %#", fileName, [error localizedDescription] );
}
I'm really struggling with this,Day three of the unending quest to save and load an image in my app. I'm picking an image from camera roll and trying to save it to the device via the appsdocsdirectory.
in the Appdelegate.m I have:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
}
in the class I want to save and load the image (picked from camera roll into UI imageView)
- (IBAction)save:(id)sender {
UIImage *myImage = [imageView image];
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
}
- (IBAction)load:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}
#end
I also imported Appdelegate.h, in this class, not sure if that was needed or correct?Now I have finally got rid of all errors and no exceptions being thrown but now my problem is nothing happens when I try to save the image and load it. I'm also getting yellow triangles telling me i have unused veriables in load UIImage* thumImage NSString *appDocsDirectory & NSData *data so I may have made a royal hash of this.
This is how I did it in one of my project.
I get the picture with the delegate method ot the imagePickerController :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
pictureInfo = info;
}
Then I save it like this :
-(void)createPin
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pinFolderName = [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970]];
NSString *pinFolderPath = [documentsDirectory stringByAppendingPathComponent:pinFolderName];
[fileManager createDirectoryAtPath:pinFolderPath withIntermediateDirectories:YES attributes:nil error:NULL];
self.fullsizePath = [pinFolderPath stringByAppendingPathComponent:#"fullsize.png"];
NSString *mediaType = [pictureInfo objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *fullsizeImage = [pictureInfo objectForKey:UIImagePickerControllerEditedImage];
NSData *fullsizeData = UIImagePNGRepresentation(fullsizeImage);
[fullsizeData writeToFile:self.fullsizePath atomically:YES];
}
}
Hope you can start from there.
If you need any explanations or help, feel free to ask
I have used NSuser defaults, all sorted
I've added a very simple .csv file to my app's NSDocumentDirectory using:
-(IBAction)exportModule:(id)sender{
NSString *fileName = #"exportedfile.csv";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *exportString = #"I've,been,exported";
NSData *testData=[exportString dataUsingEncoding:NSUTF8StringEncoding];
NSString *docDir = documentsDirectory;
NSString *completePath = [docDir stringByAppendingPathComponent:fileName];
[testData writeToFile:completePath atomically:YES];
if ([[NSFileManager defaultManager] fileExistsAtPath:completePath]) {
NSLog(#"Theres a file here!");
}
}
The app gets into the if statement printing "Theres a file here!", however I would like open the file and see if there were any problems in formatting the .csv.
If I were running on a physical device, I'd be able to open it up in iTunes and take a look there, but is there a way to examine the .csv while only using the simulator?
I have this at the beginning of all of my programs for using the simulator
NSLog(#"Documents: %#", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]);
I'm looking for a way to fill an NSArray with NSStrings that are the names of the directories in the apps documents directory.
This is what I have so far:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
for (//loop until no more directories)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:path];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
I'm not sure what the for loop's conditions need to be and I'm not sure how to retrieve the directories names.
Thanks for the advice in advance.
Try this
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:urDocumentsFolderPath error:nil] retain];
for(NSString *file in filesArray) {
//Do something
}
So in your case
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
filesArray = [[fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil] retain];
for(NSString *file in filesArray)
{
NSString *newDir = [documentsDirectory stringByAppendingPathComponent:file];
//Code for retrieving directory names
[directoriesArray addObject:newDir];
}
Hope this helps
"apps documents directory" = ~/Users/User/Applications? NSDocumentsDirectory or NSApplicationDirectory?
Maybe:
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileIsDirectory = FALSE;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *content = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL]; //OSX v10.5
NSEnumerator *enumeratorContent = [content objectEnumerator];
NSString *file;
while ((file = [emumeratorContent nextObject])) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
if (fileManager fileExistAtPath:[documentsDirectory stringByAppendingPathComponent:file] isDirectory:&fileIsDirectory && fileIsDirectory)
[directoriesArray addObject:file];
[pool drain];
Try to use NSDirectoryEnumerator(OSX v.10.0) or NSURL too (OSX v.10.6).