2016-08-01 143 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 /文件夹。 –

相关问题