簡體   English   中英

iOS8 無法在應用組共享容器位置下創建文件夾/文件

[英]iOS8 Unable to create a folder/file under app group shared container location

我正在使用以下代碼在共享容器路徑下創建文件夾/文件。 這將有助於應用擴展程序和包含應用程序的擴展程序都可以訪問數據。

獲取共享容器 url 位置的代碼:

+(NSURL*)getSharedContainerURLPath
{
    NSFileManager *fm = [NSFileManager defaultManager];

    NSString *appGroupName = APP_EXTENSION_GROUP_NAME; /* For example */

    NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];

    return groupContainerURL;
}

創建目錄的代碼

+(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....");
        }
    }
}

上面的代碼沒有引發任何錯誤,它表示成功,但我無法在共享容器路徑下找到文件夾。 任何可能會受到贊賞的想法

我只是通過更改以下代碼使我的代碼工作

NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];

NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] path];    

對於斯威夫特

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!
    }

使用

var strSavePath : String = self.createProjectDirectoryPath("Large")

注意:在您的應用程序組設置之后,上面的代碼對於創建文件夾很有用。

@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM