Home > Software design >  How to store file with local path in iOS
How to store file with local path in iOS

Time:04-07

I am a React native developer and I was trying to integrate a native ios code.

One of the instance takes NSURL * which should be a local path I guess

https://github.com/uber/startup-reason-reporter/blob/master/StartupReasonReporter/StartupReasonReporterPriorRunInfo/UBApplicationStartupReasonReporterPriorRunInfo.h#L21

  (nonnull instancetype)priorRunAtDirectoryURL:(nullable NSURL *)directoryURL;

I am not sure what does localPath Url looks like in IOS, like what should I pass? for example?

Ps: intentionally including swift tag as well because I think swift developers could also answer it.

CodePudding user response:

Try looking up documentation

- (NSArray<NSURL *> *)URLsForDirectory:(NSSearchPathDirectory)directory 
                         inDomains:(NSSearchPathDomainMask)domainMask;`

That will get you the URL and check out FileManager.SearchPathDirectory for all the viable options:

Here's an example for getting the caches directory

NSURL* url = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];`

I could be any one of the 25-26 options in SearchPathDirectory depending on where they put that stuff.

CodePudding user response:

Based on description of that function:

  • Returns the prior run information stored to disk at the given directory URL.
  • @param directoryURL The directory to use to to store the startup reason data.
  • @return the previous startup reason data if it was present on disk, or empty startup reason object. */

you need to provide a directory they can write into. So Apps's document directory would be the best (as a root) whatever folder you want (which, based on their code, they will even create for you). So:

NSURL* docs = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL* myCrashes = [docs URLByAppendingPathComponent:@"myCrashes" isDirectory:TRUE];

it will look something like:

file:///some/path/to/app/sandbox/data/Documents/myCrashes
  • Related