20

In the app extension is there a way to get images generated from the containing app which is store in /var/mobile/Containers/Data/Application//Documents// folder?

iphonic
  • 12,615
  • 7
  • 60
  • 107
Paul Smith
  • 395
  • 1
  • 3
  • 10

1 Answers1

29

In order to make files available to app extension you have to use Group Path, as App Extension can't access app's Document Folder, for that you have follow these steps,

  1. Enable App Groups from Project Settings-> Capabilities.
  2. Add a group extension something like group.yourappid.
  3. Then use following code.

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

You can add or change the path extension as per your image extensions you are generating.

Note - Remember you need to generate the images in the group folder rather than in Documents Folder, so it is available to both app and extension.

Cheers.

Swift 3 Update

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!)
}
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
iphonic
  • 12,615
  • 7
  • 60
  • 107
  • Thank you so much this totally works! Now I need to transfer all the images to AppGroup//Documents/ folder. – Paul Smith Aug 03 '16 at 04:01
  • hi @iphonic you have any idea for this question https://stackoverflow.com/questions/55547930/how-can-i-create-and-access-share-app-group-document-directory?noredirect=1#comment97797926_55547930 – ikbal Apr 06 '19 at 12:35
  • I want to write logs in extensions project , i have create the file in app group shared folder but still it could not write data on file , do you know why ? – Anita Nagori Feb 10 '21 at 21:30
  • @Anita have you found a solution for logging in extension to shared container? – Mehmet Baykar Sep 13 '21 at 11:37
  • Yes , we can not access the docuent folder for app accessing file , in app and extension you need to use shared folder Check this : https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html – Anita Nagori Sep 14 '21 at 07:03