Instagram intercepts multimedia issue

This is my code. The file is correctly added to the photo library, but in the instagram application this url β†’ instagram://library?AssetPath=assets-library%3A%2F%2Fasset%2Fasset.mp4%3Fid=5EDBD113-FF57-476B-AABB-6A59F31170B5&ext=mp4&InstagramCaption=my%caption does not open the last video.

 - (void)loadCameraRollAssetToInstagram:(NSURL*)assetsLibraryURL andMessage:(NSString*)message { NSString *escapedString = [self urlencodedString:assetsLibraryURL.absoluteString]; NSString *escapedCaption = [self urlencodedString:message]; NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@", escapedString, escapedCaption]]; NSLog(@"instagramURL ==> %@",instagramURL); if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { NSLog(@"Open Instagram!!"); [[UIApplication sharedApplication] openURL:instagramURL]; } else { NSLog(@"Cant open Instagram!!"); [[[UIAlertView alloc] initWithTitle:@"Instagram" message:@"App not installed" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show]; } } - (NSString*)urlencodedString:(NSString *)message { return [message stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; } - (void)saveToCameraRoll:(NSURL *)srcURL withCurrentAction:(NSString *)action { ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock = ^(NSURL *newURL, NSError *error) { if (error) { NSLog( @"Error writing image with metadata to Photo Library: %@", error ); [[[UIAlertView alloc] initWithTitle:@"Facebook" message:@"Pal - Currently we can't process your video. Please try again in few moments" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sign In", nil] show]; } else { NSLog( @"Wrote image with metadata to Photo Library: %@", newURL.absoluteString); if ([action isEqualToString:@"instagram"]) [self loadCameraRollAssetToInstagram:newURL andMessage:@"My caption"]; //Can be any text? } }; if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:srcURL]) { [library writeVideoAtPathToSavedPhotosAlbum:srcURL completionBlock:videoWriteCompletionBlock]; } } 

enter image description here

something very strange - it worked fine until I uninstalled and installed instagram. Don't know if this one has something to do

+9
ios objective-c hook instagram
Dec 11 '15 at 15:01
source share
8 answers

instagram://library?AssetPath=\(assetsLibraryUrl) stopped working some time ago. Instagram developers have probably moved into the scope of Photos and are no longer using AssetsLibrary.

Having this assumption, I tried several other parameter names and found that instagram://library?LocalIdentifier=\(localID) , where localId is the localIdentifier your PHAsset working so far.

This is still undocumented, as it could be corrupted in any future version of Instagram.

+32
Jan 30 '16 at 8:50
source share

Resuming this task after a long time and considering borisgolovnev's and ALAssetsLibrary is outdated, the final solution is:

 - (void)saveToCameraRollOpt2:(NSURL *)srcURL { __block PHAssetChangeRequest *_mChangeRequest = nil; __block PHObjectPlaceholder *placeholder; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ NSData *pngData = [NSData dataWithContentsOfURL:srcURL]; UIImage *image = [UIImage imageWithData:pngData]; _mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; placeholder = _mChangeRequest.placeholderForCreatedAsset; } completionHandler:^(BOOL success, NSError *error) { if (success) { [self loadCameraRollAssetToInstagram:[placeholder localIdentifier]]; } else { NSLog(@"write error : %@",error); [self showAlert:@"Error" msg:@"Error saving in camera roll" action:nil]; } }]; } - (void)loadCameraRollAssetToInstagram:(NSString *)localId { NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?LocalIdentifier=\%@", localId]]; if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { [[UIApplication sharedApplication] openURL:instagramURL options:@{} completionHandler:nil]; } else { [self showAlert:@"Error" msg:@"Instagram app is not installed" action:nil]; } } - (NSString*)urlencodedString:(NSString *)message { return [message stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; } 

Do not forget

 #import <Photos/Photos.h> Add `NSPhotoLibraryUsageDescription` and `QueriesSchemes` inside .plist file <key>NSPhotoLibraryUsageDescription</key> <string>Need permission to access to manage your photos library</string> <key>LSApplicationQueriesSchemes</key> <array> <string>instagram</string> </array> 
+3
Feb 22 '17 at 17:24
source share

The @borisgolovnev solution really works. You can get the local id of your last saved video using the following code. Transfer using instagram: // library? LocalIdentifier = (localID) opens Instagram with the video of your choice.

 let fetchOptions = PHFetchOptions() fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending:false)] let fetchResult = PHAsset.fetchAssetsWithMediaType(.Video, options: fetchOptions) if let lastAsset = fetchResult.firstObject as? PHAsset { self.localIdentifier = lastAsset.localIdentifier } 
+1
Mar 15 '16 at 18:29
source share

use this code

 NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"]; if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { NSURL *videoFilePath = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[request downloadDestinationPath]]]; // Your local path to the video NSString *caption = @"Some Preloaded Caption"; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library writeVideoAtPathToSavedPhotosAlbum:videoFilePath completionBlock:^(NSURL *assetURL, NSError *error) { NSString *escapedString = [self urlencodedString:videoFilePath.absoluteString]; NSString *escapedCaption = [self urlencodedString:caption]; NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",escapedString,escapedCaption]]; if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { [[UIApplication sharedApplication] openURL:instagramURL]; } }]; } 

Instagram displays only those iamges / videos that are saved along the path set in instagramURL. This path must be absolute.

If it is not already displayed, add LSApplicationQueriesSchemes to the info.plist file of the array, add its item0 as an instagram.

0
Dec 17 '15 at 5:55
source share

Replace

 - (NSString*)urlencodedString:(NSString *)message{ return [message stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; } 

FROM

 - (NSString*)urlencodedString:(NSString *)message{ return [message stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]]; } 

It worked for me!

0
Jan 09 '16 at 3:30
source share

swift 3 get last id simple and fast

  var lastIdentifier = "" let fetchOptions = PHFetchOptions() fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending:false)] let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions) if let lastAsset: PHAsset = fetchResult.lastObject { lastIdentifier = lastAsset.localIdentifier } 
0
Dec 02 '16 at 20:27
source share

Here is the code for sharing video on instagram: Perhaps you need to add a condition for substringFromIndex , but its working.

  - (void)ShareAssetURLvideoToInstagram:(NSURL*)assetsLibraryURL { NSMutableDictionary *queryStringDictionary = [[NSMutableDictionary alloc] init]; NSString *strParamater = [assetsLibraryURL.absoluteString substringFromIndex:[assetsLibraryURL.absoluteString rangeOfString:@"?"].location+1]; NSArray *urlComponents = [strParamater componentsSeparatedByString:@"&"]; for (NSString *keyValuePair in urlComponents) { NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="]; NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding]; NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding]; [queryStringDictionary setObject:value forKey:key]; } NSString *mediaId = [queryStringDictionary valueForKey:@"id"]; if (mediaId.length > 0) { NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@",mediaId]]; if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { [[UIApplication sharedApplication] openURL:instagramURL]; } } } 
0
Jul 27 '17 at 8:01
source share
 if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { NSLog(@"Open Instagram!!"); //enter code here [[UIApplication sharedApplication/*<enter your code here>*/] openURL:instagramURL]; 
-four
Dec 11 '15 at 15:04
source share



All Articles