Obtain the path of app Group from FileManager

China☆狼群 提交于 2019-12-08 04:50:59

问题


I am trying to update my app for WatchKit and I save a NSKeyedArchiver file to the NSDocumentsDirectory normally. With updating to app groups I need to store it in the app groups folder. The issue I am having is I cant figure out how to just get the path, and not have it referenced as a file I am looking for.

The way it is set up now is to find the file it gives the path as a NSString

/Users/ME/Library/Developer/CoreSimulator/Devices/43F/data/Containers/Data/Application/5E/Documents/fav

but when I store to app groups, no matter which way I access the folder, it is returned

file:///Users/ME/Library/Developer/CoreSimulator/Devices/43F/data/Containers/Data/Application/5E/Documents/fav

What is the best way to just obtain the path to the shared group, rather than have the app looking for the direct file?


回答1:


So coffee deprived me had forgotten about the .path for filemanager.

NSURL *fileManagerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com"];
NSString *tmpPath = [NSString stringWithFormat:@"%@", fileManagerURL.path];
NSString *finalPath = [NSString stringWithFormat:@"%@",[string stringByAppendingString:@"/Favourites2"]];



回答2:


I was running into the same problem. I was going through the whole process of building a string to my save location and now I'm switching over to app groups and using the

NSURL *fileManagerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupID];

Well, the problem is, now instead of a string to the location that starts with "/Users/yourname/Library..." you get "file:///Users/yourname/Library..."

Here's what I did. I created the NSURL. Then I called absoluteString on it.

NSURL groupPath = [[fileManager containerURLForSecurityApplicationGroupIdentifier:groupID] absoluteString];

I now have a string that I need to strip off the first 7 characters of, then my old code works just fine, except now instead of being in the Documents directory, it's in a shared app group that can be accessed by both my old code and my new watchkit extension.

Here's the code to strip off the first 7 characters (index 6 since you start with 0), you should be able to use either method...

NSString *newGroupPath = [groupPath substringFromIndex:6];

or

NSString *newGroupPath = [groupPath substringWithRange:NSMakeRange(6, [str length]-6)];

This just removes the "file://" from the absoluteString that was made from the NSURL and gives you back your older string path the starts "/Users/YourName/Library/Developer/yada yada yada"

Hope that helps you, I have spent 4 hours figuring it out.

It seems to work for me on the simulator, I haven't tried it on the Watch yet. But at least my app is now working the way it was before, just saving the data in a shared app group. (I have a singleton that manages all of my data throughout my app, and I want that same singleton to provide data to my watch app).



来源:https://stackoverflow.com/questions/29454771/obtain-the-path-of-app-group-from-filemanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!