2014-09-10 17 views
16

Tôi đang sử dụng mã sau để tạo thư mục/tệp theo đường dẫn chứa được chia sẻ. Điều này sẽ giúp cả tiện ích mở rộng ứng dụng và tiện ích mở rộng chứa ứng dụng có thể truy cập dữ liệu.iOS8 Không thể tạo thư mục/tệp trong vị trí vùng chứa được chia sẻ của nhóm ứng dụng

mã để có được chia sẻ url chứa vị trí:

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

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

    NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName]; 

    return groupContainerURL; 
} 

mã để tạo một thư mục

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

mã trên không gây bất kỳ lỗi nó nói thành công nhưng tôi không thể tìm thấy bên dưới đường dẫn vùng chứa được chia sẻ. Bất kỳ ý kiến ​​cho rằng có thể được đánh giá cao

Trả lời

29

Tôi chỉ cần thực hiện việc mã của tôi bằng cách thay đổi mã sau

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

để

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

Khi bạn đã thiết lập Nhóm ứng dụng như được hiển thị trong liên kết sau. http://blog.parse.com/announcements/introducing-local-data-sharing-for-apple-watch-and-app-extensions/ – Thiru

+0

hãy đánh dấu câu hỏi là được chấp nhận để mọi người biết đó là giải pháp @loganathan –

+0

Nó làm việc cho tôi. Thanx – SPatel

2

Đối 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! 
    } 

để sử dụng

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

Lưu ý: Sau khi nhóm ứng dụng của bạn được thiết lập, mã trên đây hữu ích để tạo thư mục.

Các vấn đề liên quan