简体   繁体   English

iOS8 无法在应用组共享容器位置下创建文件夹/文件

[英]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.

相关问题 如何创建/使用共享应用程序组容器作为包含应用程序及其在ios中的扩展名之间的缓存存储 - How to create/use a shared app group container as cache storage between containing app and it's extension in ios 应用未显示在 iOS8 中共享扩展的共享选项的共享菜单中 - App is not showing in the share menu of shared options in shared extension in iOS8 Swift / iOS8:为websocket SocketIOCocoa创建共享实例 - Swift/iOS8: Create shared instance for websocket SocketIOCocoa ios8扩展和容器应用程序如何共享数据 - ios8 extension and container app how to share data iOS 13.3.1 在 App 文件夹下显示 .sqlite 文件 - iOS 13.3.1 shows .sqlite file under App's folder 调试 iOS 应用时如何查看 App Group Shared Container 目录的内容? - How can I view the contents of the App Group Shared Container directory when debugging an iOS app? 无法从iOS 10中的App组共享容器中获取扩展程序中的图像 - Not able to fetch images in extension app from App group shared container in iOS 10 位置管理器在 iOS8 中不起作用 - Location manager is not working in iOS8 定位服务在iOS8中不起作用 - Location services not working in ios8 未获得位置iOS8的授权 - Not getting authorization for location iOS8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM