I am trying to read files from the App bundle (and Library) directory using absolute path like this:
NSString* imagePath = "/var/mobile/Applications/7AC2295E-2775-41EA-B017-AB4048A09F0C/My.app/image.jpg";
When I check if the file exist using
[[NSFileManager defaultManager] fileExistsAtPath:imagePath];
it returns TRUE but when I try to open the image with
[UIImage imangeNamed:imagePath];
it returns nil.
What am I doing wrong here. I am scratching my head for few hours!! please help.
Read the documentation for imageNamed:. It takes a name, not a full path, and searches the application bundle for it. If you want to use a full path you should use another initializer method, such as initWithContentsOfFile:.
Related
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.
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
Try looking up documentation for
- (NSArray<NSURL *> *)URLsForDirectory:(NSSearchPathDirectory)directory
inDomains:(NSSearchPathDomainMask)domainMask;`
That will get you the URL and check out FileManager.SearchPathDirectory enum for all the viable options:
Here's an example for getting the caches directory
NSURL* url = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];`
It could be any one of the 25-26 options in SearchPathDirectory depending on where they put that stuff.
I'm probably missing something really obvious here, but:
NSError *error;
NSURL *cachesDirectory = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask][0];
NSLog(#"Caches directory: %#", cachesDirectory);
NSURL *tmpDirectory = [[NSFileManager defaultManager] URLForDirectory:NSItemReplacementDirectory inDomain:NSUserDomainMask appropriateForURL:cachesDirectory create:YES error:&error];
NSLog(#"TMP directory: %#", tmpDirectory);
// Result:
// 2013-10-28 13:37:47.972 MyApp[220:907] Caches directory: file://localhost/var/mobile/Applications/029A4948-A67A-48E5-A35F-1BBCC744E9B0/Library/Caches/
// 2013-10-28 13:37:47.976 MyApp[220:907] TMP directory: file://localhost/var/mobile/Applications/029A4948-A67A-48E5-A35F-1BBCC744E9B0/Library/(A%20Document%20Being%20Saved%20By%20MyApp%2011)/
I was expecting my temporary directory to be created inside the caches directory. From the docs:
You can also use this method to create a new temporary directory for storing things like autosave files; to do so, specify NSItemReplacementDirectory for the directory parameter, NSUserDomainMask for the domain parameter, and a valid parent directory for the url parameter. After locating (or creating) the desired directory, this method returns the URL for that directory.
Also, the definition of url:
The name of a directory inside of which you want to create a unique temporary directory for autosaving documents or some other use. This parameter is ignored unless the directory parameter contains the value NSItemReplacementDirectory and the domain parameter contains the value NSUserDomainMask. When creating a temporary directory, the shouldCreate parameter is ignored and the directory is always created.
You can see that my temporary directory is being created inside Library. What am I doing wrong?
Looks like you are not doing anything wrong. I just tested it and found it behaving the way you described. Either the behavior does not comply with the doc or the docs are incorrect. I also happen to find the following link where people were discussing the exact same issue:
http://lists.apple.com/archives/cocoa-dev/2012/Feb/msg00197.html
I am using cocos2d-x and I found it used pathForResource to get full path for a existing file. But I need to get the full path of a directory. If I simply pass "" to pathForResource, it will search and return the first file in that directory.
I cannot just take application directory and put it in front of relative path since I don't know if it's a relative one or already a full one.
I can trim the filename but I think that's a weird solution.
So is there any function in objective-c that works like pathForResource but don't really search for files... just return the directory name
BTW, I am using opendir functions in dirent.h. I found it won't work if I just pass a relative path, which was fine under windows.
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"DirectoryName"];
NSLog(#"%#", sourcePath);
My project has a folder (not group) named "data" which contains many subfolders, each of which contains a set of files.
My question is, how do I grab a URL (or path) reference to the "data" folder? How about to its subfolders? I'm sure it's a fairly simple task, so forgive my ignorance, but I've never used folder refs in a project so I'm not familiar with the code. I did look over the NSFileManager ref but I'm fuzzy on how to make use of it.
Thanks in advance.
NSBundle can give you your absolute path in the system.
NSString (or NSURL) has methods for working with paths.
NSFileManager allows you to move, copy, delete (…) files.
This is how you get path to your Data directory:
NSString *dataDir = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"Data"];
// "/var/private/Application/.../YourApp.app/Data"
Now you just append multiple directory names to dataDir using the same method above and you should get any path you want.
In case you don't know the exact path and you want to scan the directory, you will have to use
:
NSArray *dataDirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataDir error:nil]
// "file1.data", "file2.data", ...
Then it's all about appending path components.
I am testing the MixerHost sample code. However the following code:
NSURL *beatsLoop = [[NSBundle mainBundle] URLForResource: #"beatsMono"
withExtension: #"caf"];
the beatsLoop is nil.
What's the reason for that?
Should I first create the beatsMono.caf file and then put into some specific path?
Any comments and solutions will be highly appreciated.
Thanks,
finspoo
Should I first create the beatsMono.caf file and then put into some specific path?
Yes. The file beatsMono.caf (case sensitive!) must exist in your application bundle for that method to succeed. This is done by adding the file to the target as a resource in XCode, you cannot do it at runtime.