Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can app extension access files in containing app Documents/ folder

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?

like image 894
Paul Smith Avatar asked Aug 01 '16 06:08

Paul Smith


People also ask

How do I access files inside an app?

On your Android 10 device, open the app drawer and tap the icon for Files. By default, the app displays your most recent files. Swipe down the screen to view all your recent files (Figure A). To see only specific types of files, tap one of the categories at the top, such as Images, Videos, Audio, or Documents.

What is the extension for apps?

The extension of android application is . apk. Android executable concept is not like windows executable(.exe).

What is an app file extension?

A file extension is the set of three or four characters at the end of a filename; in this case, .app. File extensions tell you what type of file it is, and tell Windows what programs can open it. Windows often associates a default program to each file extension, so that when you double-click the file, the program launches automatically.

How do I change what apps have access to my files?

Here’s how you can adjust, control, manage, and change what apps have access to files and folders on the Mac: Click the Apple logo in the top-left corner of the Mac screen. Click “System Preferences.” Click “Security & Privacy.” Click the “Privacy” tab. Click “Files and Folders” in the pane on the left.

How do I open an app file?

how to open a .app file? Launch a .app file, or any other file on your PC, by double-clicking it. If your file associations are set up correctly, the application that's meant to open your .app file will open it. It's possible you may need to download or purchase the correct application.

What is a file extension?

what is a file extension? A file extension is the set of three or four characters at the end of a filename; in this case, .app. File extensions tell you what type of file it is, and tell Windows what programs can open it.


1 Answers

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!)
}
like image 78
iphonic Avatar answered Oct 20 '22 12:10

iphonic