2016-08-01 161 views

回答

4

爲了使文件提供給應用程序的擴展,你必須使用Group Path,如應用信息無法訪問應用程序的文檔文件夾,你必須按照這些步驟,

  1. 從啓用應用組項目設置 - >功能
  2. 添加羣組擴展程序,如group.yourappid
  3. 然後使用下面的代碼。

    NSString *docPath=[self groupPath]; 
    
    NSArray *contents=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:nil]; 
    
    NSMutableArray *images=[[NSMutableArray alloc] init]; 
    
        for(NSString *file in contents){ 
         if([[file pathExtension] isEqualToString:@"png"]){ 
          [images addObject:[docPath stringByAppendingPathComponent:file]]; 
         } 
        } 
    
    
    -(NSString *)groupPath{ 
        NSString *appGroupDirectoryPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:group.yourappid].path; 
    
        return appGroupDirectoryPath; 
    } 
    

您可以添加或更改路徑擴展按照你生成圖像擴展。

注意 - 請記住,您需要在組文件夾中生成圖像,而不是在文檔文件夾中生成圖像,因此它可用於應用程序和擴展程序。

乾杯。

斯威夫特3更新

let fileManager = FileManager.default 
let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "YOUR_GROUP_ID")?.appendingPathComponent("logo.png") 

// Write to Group Container    
if !fileManager.fileExists(atPath: url.path) { 

    let image = UIImage(named: "name") 
    let imageData = UIImagePNGRepresentation(image!) 
    fileManager.createFile(atPath: url.path as String, contents: imageData, attributes: nil) 
} 

// Read from Group Container - (PushNotification attachment example)    
// Add the attachment from group directory to the notification content      
if let attachment = try? UNNotificationAttachment(identifier: "", url: url!) { 

    bestAttemptContent.attachments = [attachment] 

    // Serve the notification content 
    self.contentHandler!(self.bestAttemptContent!) 
} 
+0

非常感謝你這完全適用!現在我需要將所有圖像傳輸到AppGroup // Documents /文件夾。 –

相關問題