how to place a file in application sand box in iphone - iphone

Is there any way of placing a file into application "sandbox" i.e into eg:
/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents
of iphone?
My Application expects a file to be put into /Documents folder of application so that it can be uploaded to my local server.
This method should confirm to the rules of Apple so that it should not be rejected.

Why rejected? It is definitely normal to save a file to a Documents folder and Apple can not reject your application just because you save a file to the application Documents folder.
Use the following code to do so:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *pathToFile = [basePath stringByAppendingPathComponent:#"somefile.format"];
[someNSData writeToFile:pathToFile atomically: YES];

Related

Why i can only save file or create folder at NSDocumentDirectory

I use ASI to download files.
But it only works if I set the path to NSDocumentDirectory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
I haven't changed any other code, just replaced NSDocumentDirectory to
NSCachesDirectory or NSDownloadsDirectory,and it does not work.
Download progress is 100% and file didn't save.
I don't know why.
This code:
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
works perfect in many projects of mine. I think the problem is in your saving code. Could you show it?
You can do it much easier and write to ANY location.
If you are using ASI, you can download DATA.(NSData)
So:
NSFileManager *fileMgr = [NSFileManager defaultManager];
[fileMgr createFileAtPath:#"whateverpathyouwant" contents:downloaddata attributes:nil];
This only works if you are jailbroken I guess.

How to save audio data recorded using AVAudioRecorder for further use?

Hi i am creating application which can record sound. I can have that recorded sound file and i can play it immediately. I do not know how can I save it on iPhone so that I can play it after reopening application.
Is there any way by which this file will be stored as application save data?
Thanks in advance.
Your can do it like this to record it into your documents directory :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"audioFile.ext"]; // Where ext is the audio format extension you have recorded your sound
[yourAudioPlayer.data writeToFile:filePath atomically:YES];

How to add a ringtone from an application to ringtones of iphone?

I have created an application included a ringtone, but how can i add it to ringtones of iphone?
Use iTunes file sharing in your app and copy the ringtone file to the app's documents directory.
Set "Application supports iTunes file sharing" to YES in your info.plist
Wherever appropriate in your app copy out the file with the code below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyRingtone" ofType:#"m4r"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:filePath];
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:#"MyRingtone.m4r"]
contents:mainBundleFile
attributes:nil];
The user can now access the ringtone via itunes and add it to their device's ringtones.
You cannot. Apple doesn't release an API to export/write ringtones to the operating system.

Downloading music files to the iphone

I am trying to develop an application which downloads online music data (music files) and makes them available when I am offline.
Can anyone help me? What is the best way to do that?
I like ASIHTTPRequest framework for downloading. You may also use standard NSURLRequest, but as for me it's not as easy-to-use as ASIHTTPRequest.
Update:
To save file, you may use Documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [paths objectAtIndex:0];
To save loaded data into a file, use
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
To load:
+ (id)dataWithContentsOfFile:(NSString *)path

Where should I store an image selected on the iPhone?

I want to take a picture or select an existing picture from the users existing photos. What is the best way to do this?
Specifically I am concerned where I should to store the image. The storage location should be private to the application. I need to be able to reuse the image as a background every time the application opens. Thanks!
You should use the UIImagePickerController to retrieve images from the library or the camera. You can persist the picture in the App's Documents folder. This folder is private to your app and is writeable.
You can get the path to the documents folder like so
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
And to save a file there.
NSString *filePath = [NSString stringWithFormat:#"%#whatever.jpg",docDirectory];
NSData *toSave = UIImageJPEGRepresentation(image,1.0); //image is a UIImage
[toSave writeToFile:filePath atomically:YES];