Bindiya
Bindiya

Reputation: 614

how to add custom path in URLByAppendingPathComponent in iOS

In my project I am saving jsondata into local jsonfile named AllBooking.json.

Code:

NSURL *fileURL =[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AllBooking.json"];

URLByAppendingPathComponent method creates this file to any irrelevent path !

And
[self applicationDocumentsDirectory] returns

[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];

Is there any way that I can give custom path to file ?

Upvotes: 1

Views: 606

Answers (1)

Abhinav
Abhinav

Reputation: 38162

Well, URLByAppendingPathComponent works all fine. Problem would be in your applicationDocumentsDirectory method.

I just gave a run for you. Here is how my applicationDocumentsDirectory method looks like (assuming you want to return URL for documents directory; however method name must end with URL for clarity):

- (NSURL *)applicationDocumentsDirectory {
    NSString *documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    return [NSURL URLWithString:documentsDirectoryPath];
}

And this is how I called it:

NSURL *fileURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AllBooking.json"];
NSLog(@"%@", [fileURL absoluteString]);

The log statement correctly prints:

/Users/abhinav/Library/Developer/CoreSimulator/Devices/8C8ACA37-0D0F-4FED-A431-BA35EF9F08F1/data/Containers/Data/Application/20B049C0-9A0D-4826-9867-027607D02715/Documents/AllBooking.json

Upvotes: 2

Related Questions