[英]iOS8 Unable to create a folder/file under app group shared container location
I am using following code to create a folder/file under the shared container path.我正在使用以下代码在共享容器路径下创建文件夹/文件。 Which will help both app extension and the extension containing app can access the data.
这将有助于应用扩展程序和包含应用程序的扩展程序都可以访问数据。
code to get the shared container url location:获取共享容器 url 位置的代码:
+(NSURL*)getSharedContainerURLPath
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *appGroupName = APP_EXTENSION_GROUP_NAME; /* For example */
NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];
return groupContainerURL;
}
code to create a directory创建目录的代码
+(void)createDirAtSharedContainerPath
{
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];
NSString *directoryToCreate = @"user_abc";
//basically this is <shared_container_file_path>/user_abc
NSString *dirPath = [sharedContainerPathLocation stringByAppendingPathComponent:directoryToCreate];
BOOL isdir;
NSError *error = nil;
NSFileManager *mgr = [[NSFileManager alloc]init];
if (![mgr fileExistsAtPath:dirPath isDirectory:&isdir]) { //create a dir only that does not exists
if (![mgr createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"error while creating dir: %@", error.localizedDescription);
} else {
NSLog(@"dir was created....");
}
}
}
the above code not raising any error it says success but i am not able to find the folder under the shared container path.上面的代码没有引发任何错误,它表示成功,但我无法在共享容器路径下找到文件夹。 Any idea that might be appreciated
任何可能会受到赞赏的想法
I just made my code work by changing the following code我只是通过更改以下代码使我的代码工作
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];
to到
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] path];
For Swift对于斯威夫特
func createProjectDirectoryPath(path:String) -> String
{
let containerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.abc")
let logsPath = containerURL!.URLByAppendingPathComponent(path)
//print(logsPath.path);
do {
try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
return logsPath.path!
}
To Use使用
var strSavePath : String = self.createProjectDirectoryPath("Large")
Note: After your app group is setup this above code is useful to create folder.注意:在您的应用程序组设置之后,上面的代码对于创建文件夹很有用。
@Hardik Thakkar code for Swift 5 This function create directory in shared app group container and return path to it. @Hardik Thakkar Swift 5 代码此函数在共享应用程序组容器中创建目录并返回它的路径。
func createProjectDirectoryPath(path:String) -> String
{
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Constants.Notifications.appGroupIdentifier)
let logsPath = containerURL!.appendingPathComponent(path)
//print(logsPath.path);
do {
try FileManager.default.createDirectory(atPath: logsPath.path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print("Unable to create directory \(error.debugDescription)")
}
return logsPath.path
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.