iOS Share Extension从其他APP获取文件

参考链接:
https://stackoverflow.com/questions/33163431/how-to-get-image-form-url-passed-by-share-extension-to-an-app

存:
NSURL * urlTemp = 从self.extensionContext.inputItems属性中的NSExtensionItem对象的attachments属性中的NSItemProvider的loadItemForTypeIdentifier方法里获取的,例如,从微信中分享的就是源文件在微信中的路径。
NSData *urlData = [NSData dataWithContentsOfURL:urlTemp];//获取源路径下的数据
NSURL *sharedContainer = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.AppShareDemo"];
NSString *dirPath = [sharedContainer.path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",[urlTemp lastPathComponent]]];//创建/Shared/AppGroup/路径下的目的路径
BOOL b = [urlData writeToFile:dirPath atomically:YES];//把数据写进目的路径

                 NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.AppShareDemo"];
                 [userDefaults setValue:dirPath forKey:@"shareFilePath"];//把目的路径传给宿主APP(container applciation)

取:
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.AppShareDemo"];
NSString *filePath = [userDefaults valueForKey:@"shareFilePath"];
NSData *data = [NSData dataWithContentsOfFile:filePath];

最关键的一句是,将分享的数据写入分享扩展(AppGroup)中,然后拿到对应存储路径传给宿主APP
注意:以下来自以上网址的内容
You probably call getImageFromExtension from didSelectPost. At the end of didSelectPost you are supposed to call completeRequestReturningItems:completionHandler: to tell the host app to complete the app extension request with an array of result items. When that method is called the whole ShareViewController is dismissed and deallocated. When you call this at the end of didSelectPost, it gets executed before the NSItemProvider could retrieve the image URL. Because that happens asynchronously. To fix this you have to call completeRequestReturningItems:completionHandler: after NSItemProvider has retrieved the imageURL.

Here is how it should work:

  • (void)didSelectPost {
    NSString *typeIdentifier = (NSString *)kUTTypeImage
    NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
    NSItemProvider *itemProvider = item.attachments.firstObject;
    if ([itemProvider hasItemConformingToTypeIdentifier:typeIdentifier]) {
    [itemProvider loadItemForTypeIdentifier:typeIdentifier
    options:nil
    completionHandler:^(NSURL *url, NSError *error) {
    NSURL *imageURL = (NSURL *)item
    self.saveImage(imageURL)
    [self.extensionContext completeRequestReturningItems:@[]
    completionHandler:nil];
    }];
    }
    }
    By the way, there is no need to use dispatch_async when working with NSItemProvider as the provider is already doing his work asynchronously.

I've written a blog post about this, if you want to read a bit more about this.

EDIT

You can now load the image with the file path that you just retrieve. This works only in the extension! If you want to access the image from the containing app you cannot use the file URL you just retrieved. The share extension saves the selected image in a folder that is not accessible by the containing app.

To make the image available to the containing app you have to activate App Groups in you containing app and the extension. Then the extension saves the image to the shared container and the containing app can retrieve it:

func saveImage(url: NSURL) {
guard let imageData = NSData(contentsOfURL: url) else { return }
let selectedImage = UIImage(data: imageData)

let fileManager = NSFileManager.defaultManager()
if let sharedContainer = fileManager.containerURLForSecurityApplicationGroupIdentifier("YOUR_GROUP_IDENTIFIER") {
    if let dirPath = sharedContainer.path {
        let sharedImageFilePath = dirPath + "/image.png"
        let binaryImageData = UIImagePNGRepresentation(selectedImage!)
        binaryImageData?.writeToFile(sharedImageFilePath, atomically: true)
        // send the sharedImageFilePath to the containing app
    }
}

}

你可能感兴趣的:(iOS Share Extension从其他APP获取文件)