romatroskin / social_share_plugin

Social Share to Facebook and Instagram Flutter plugin.
BSD 2-Clause "Simplified" License
41 stars 87 forks source link

Changed instagram feed share implementation on ios #47

Open 45minuto opened 3 years ago

45minuto commented 3 years ago

On ios there's a instagram feed's share that doesn't work. I've changed the implementation in order to fix this.

Diaglyonok commented 3 years ago

@45minuto, i have an issue with your implementation. it takes not the last, but the pre-last image from iphone images. Don't you know how to fix it?

45minuto commented 3 years ago

Hi @Diaglyonok , yes i know how to fix but i can't do it now. For now I'll explain to you. If you see the native ios code, you can find this:

        //Save the image in the camera roll
        UIImage *image = [UIImage imageWithContentsOfFile: imagePath];
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        PHFetchOptions *options = [[PHFetchOptions alloc] init];
        options.sortDescriptors = @[
            [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO],
        ];

        //Pick the asset from the camera roll (it takes the last saved image in the camera roll)
        PHFetchResult *result = [PHAsset fetchAssetsWithOptions:options];
        PHAsset *asset = [result firstObject];
        //Retrieve the localIdentifier to be sent to instagram
        NSString *localId = asset.localIdentifier;

As you can see, i save the image in the photos album, then i declare the fetch options (pick photos from photos album ordered by descending creation date), and then i fetch the photoalbum taking the first object.

The problem is that the "pick" action is syncronous, the "save" action is asynchronous, so if the os pick the photo before the save task is completed, he take another photo.

How to fix? As you can read here (https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum), the UIImageWriteToSavedPhotosAlbum take a completion handler, so the "pick" operation must be inside the completion. In this case you are sure that the system will pick the last photo only after your photo is completely saved.