Transfering text files from a MacBook Pro to an iPad - ipad

I am writing an iPad app that will use several text files on my MacBook Pro as a data source for UITableViews that will display on the iPad.
Several questions:
I understand that in order for my app to fetch files from my MacBook Pro over the USB/iPad connector, my app must support file sharing. How do I accomplish this?
Since Apple made the iPad an appliance, I can't see its file system. So how can I declare paths to store the fetched files? Is the iPad a multi-user computer with multiple user home directories?
Can I write my app to interface with an SD card in the accessory connector so as to fetch text files from that card? What class should I use to do that?

[http://developer.apple.com/library/ios/#documentation/General/Reference /InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW1][1]
Add the UIFileSharingEnabled key to your Info.plist file. To see the files, open iTunes, look on the left panel, click on your iPad, look at the toolbar above the main content pane, click on "Apps", SCROLL DOWN, and you will see that you can drop files and export files (but you can't drag them, which is annoying). I actually got stumped on this, too
The way you make the file sharing files visible is to write them to a magical directory, which is obtained by the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirPath = [paths objectAtIndex:0];
Here is the routine I use in-full:
NSString* FileUtils_newUserVisibleFilePath(NSString* toFile) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentDirPath = [paths objectAtIndex:0];
NSString* filePath;
if ([documentDirPath characterAtIndex:[documentDirPath length]-1] != '/') {
filePath = [[NSString alloc] initWithFormat:#"%#/%#",documentDirPath,toFile];
} else {
filePath = [[NSString alloc] initWithFormat:#"%#%#",documentDirPath,toFile];
}
[pool release];
return filePath;
}
I have no idea. But, I also know that a lot of iPad users don't use SD cards so I would consider this a minority feature
What some developers end up doing is making an HTTP server available on the iPad.
[1]: http://developer.apple.com/library/ios/#documentation/General/Reference /InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW1

Related

iOS: Writing file to Documents folder using C/C++ and retrieve it easily with Files/iTunes or thirdparty tools

I've consulted this question:
Writing to file in iOS using C/C++
and its answer:
https://stackoverflow.com/a/39022407/987846
I seemed to have written files onto the disk, i.e., all the file opening and writing finished. But when I expect to see the files written to disk appear in the Files.app on iOS or iTunes on macOS, I found none there. In particular, my app doesn't appear in the iTunes's File Sharing category.
I've also tried thirdparty tools such as:
https://github.com/ios-control/ios-deploy
calling its file downloading command gives me an empty folder
./ios-deploy --download=/Documents --bundle_id com.mycompany.myapp -2 dest_dir
So how should I retrieve that file after closing the app?
OK, figured it out myself.
I needed to add a target property to my Info.plist, by typing in a new row:
UIFileSharingEnabled
or simply select from the property list
Application supports iTunes file sharing
Make the value YES.
Then my app will show up in iTunes File Sharing category.
The file saved into Documents folder will appear there.
For completeness, below is ObjC/C code snippets that can be inserted into an ObjC-based project.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"file.ext"];
const char* filePath = [file UTF8String];
FILE* pFile = NULL;
pFile = fopen(filePath, "ab");
float data = 9.9f;
fwrite(&data, sizeof(float), 1, pFile);
fclose(pFile);
}

Writing big file on IOS [duplicate]

I save some run-time generated files inside the .app bundle of my iOS app. In the simulator it works fine, in the device it crashes:
Could create output files in the given shader cache path
'/var/mobile/Applications/CB064997-B40E-4FE3-9834-B3217CE33489/SimedTest.app/Ogre3D/assets/RTShaderLib/cache/
Is there a good overview of where I should and shouldn't put files - how to use Documents, Library and tmp, etc?
To clarify, these are files created at startup which pre-calculate some data to save time. IF they are not present they get created so it's fine they are deleted, but not while the app is running.
The bundle is read-only. You don't want to mess around with it for two reasons:
Code Signing: the signature is verified by against the contents of the bundle; if you mess around with the bundle, you break the signature.
App Updates: updates work by replacing the entire app bundle with a newly downloaded one; any changes you make will get lost.
Where you should save stuff:
Documents: if you want it to persist and be backed up
Library/Caches: if you just want to cache downloaded data, like profile pics; will be auto deleted by the system if it is low on room unless you specify with a special do-not-delete flag.
tmp: temporary files, deleted when your app is not running
For a full explanation check out File System Programming Guide and QA1719.
No, every time you change your bundle you invalidate your signature.
If you want to write files you`l need to write in the best folder depending on what you want to do with that file.
Documents folder for long duration files
Cache for small operations
and so on
EDIT
To get the path you`ll need something like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"filename.ext"];
With this path you can write or read like this:
write:
NSString *content = #"One\nTwo\nThree\nFour\nFive";
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
read:
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil];

Using NSTemporaryDirectory without increasing tmp folder size

In my app the user has the possibility to upload and download 700*700mb images. These images are downloaded and displayed in a few UIImageViews.
My app itself is quite small, but the Documents & Data folder is enormous e.g. 17gb and it keeps increasing exponentially. My app runs on both ipad and on iPhone. Will the docs & data folder in my ipad is 6mb, on my ipod it is 17gb. I have downloaded the app package, and found out that the tmp folder is huge. there are some Stack-log files 1gb big. I found out the problem was related to the NSTemporarydirectory.
I save an image in this way:
NSString *title = [[NSUserDefaults standardUserDefaults]objectForKey:#"Folder3"];
PhotoiPhoneViewController* sharedSingleton = [PhotoiPhoneViewController sharedManager];
NSString *destDir = [[NSUserDefaults standardUserDefaults]objectForKey:#"Folder2"];
NSString *filename3 = [NSString stringWithFormat:#"%#%#sizefile.pages.%#", destDir, title, sharedSingleton.tagString];
NSString *SizePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"%#sizefile.pages.%#", title, sharedSingleton.tagString]];
[self.restClient loadFile:filename3 intoPath:SizePath];
Is this the right way to do it, or should I use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *CommentPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#commentfile.txt.%ld", title, (long)sharedSingleton.tagNumber]]
Since I want the files to be deleted after use, I thought that the NStemporaryDirectory would be deleted as soon as the file was used. What should I do??
You could look into using NSURLCache as these manage the purging.
See also NSHipster/NSURLCache
Or you could use a library like SDWebImage which will handle a cache size as well
Edit: In answer to your question
The operating system used to leave files in the temporary directory, however, since iOS 5.0 it will purge files from this directory if it sees the need. See this link for more information

Does an iOS app have write access inside its bundle?

I save some run-time generated files inside the .app bundle of my iOS app. In the simulator it works fine, in the device it crashes:
Could create output files in the given shader cache path
'/var/mobile/Applications/CB064997-B40E-4FE3-9834-B3217CE33489/SimedTest.app/Ogre3D/assets/RTShaderLib/cache/
Is there a good overview of where I should and shouldn't put files - how to use Documents, Library and tmp, etc?
To clarify, these are files created at startup which pre-calculate some data to save time. IF they are not present they get created so it's fine they are deleted, but not while the app is running.
The bundle is read-only. You don't want to mess around with it for two reasons:
Code Signing: the signature is verified by against the contents of the bundle; if you mess around with the bundle, you break the signature.
App Updates: updates work by replacing the entire app bundle with a newly downloaded one; any changes you make will get lost.
Where you should save stuff:
Documents: if you want it to persist and be backed up
Library/Caches: if you just want to cache downloaded data, like profile pics; will be auto deleted by the system if it is low on room unless you specify with a special do-not-delete flag.
tmp: temporary files, deleted when your app is not running
For a full explanation check out File System Programming Guide and QA1719.
No, every time you change your bundle you invalidate your signature.
If you want to write files you`l need to write in the best folder depending on what you want to do with that file.
Documents folder for long duration files
Cache for small operations
and so on
EDIT
To get the path you`ll need something like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"filename.ext"];
With this path you can write or read like this:
write:
NSString *content = #"One\nTwo\nThree\nFour\nFive";
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
read:
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil];

Managing files in iOS5

I want to handle files in my iOS App.
My app should create a file with a custom suffix (e.g. file.mysuff) and save it to the device so I'll be able to copy it using iTunes File Sharing.
Then I want to be able to attach that file to a new mail.
When the receiver opens the document, mail should launch my app and handle that file.
Are there good tutorials on that topic?
I'm still quite new to cocoa / cocoa touch so it should be easy to my.
Is maybe a wrapper out there that I could implement so I just have to code something like
[self [saveMyFile path:[NSURL] contents:[NSString]]]??
Thanks for help!
Greets, J.
This is one example of how you can save the file to documents on the iPhone to use later. This stores a dictionary from a list, changes a value and then writes the updates dictionary back to the file specified. Let me know if this is what you were looking for.
//user document directory and instantiate dictionary
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *plistFilePathInDocumentsDirectory = [documentsDirectoryPath stringByAppendingPathComponent:#"YourFile"];
NSMutableDictionary *yourList= [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilePathInDocumentsDirectory];
//save the new information to the plist in the user documents directory
[yourList setObject:someObject forKey:someKey];
[yourList writeToFile:plistFilePathInDocumentsDirectory atomically:YES];

Resources