You can do this without using the UIDocumentInteractionController and go directly to Instagram using these 3 methods:
It works the same as all other well-known applications. The code is written in Objective-c, so you can translate it to fast if you want. What you need to do is save the image on the device and use URLScheme
add this to your .m file
#import <Photos/Photos.h>
First you need to save your UIImage to the device using this method:
-(void)savePostsPhotoBeforeSharing { UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:@"image_file_name.jpg"], self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); }
This method is a callback to save the image to your device:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo; { [self sharePostOnInstagram]; }
After the image is saved on the device, you need to request the image you just saved and get it as a PHAsset
-(void)sharePostOnInstagram { PHFetchOptions *fetchOptions = [PHFetchOptions new]; fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO],]; __block PHAsset *assetToShare; PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions]; [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) { assetToShare = asset; }]; if([assetToShare isKindOfClass:[PHAsset class]]) { NSString *localIdentifier = assetToShare.localIdentifier; NSString *urlString = [NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@",localIdentifier]; NSURL *instagramURL = [NSURL URLWithString:urlString]; if ([[UIApplication sharedApplication] canOpenURL: instagramURL]) { [[UIApplication sharedApplication] openURL: instagramURL]; } else {
And don't forget to put this in your LSApplicationQueriesSchemes under LSApplicationQueriesSchemes
<string>instagram</string>
aviv_elk Mar 28 '17 at 8:25 2017-03-28 08:25
source share