Instagram挂钩预选媒体问题

这是我的代码。 该文件已正确添加到照片库,但在Instagram应用程序此url – > instagram://library?AssetPath=assets-library%3A%2F%2Fasset%2Fasset.mp4%3Fid=5EDBD113-FF57-476B-AABB-6A59F31170B5&ext=mp4&InstagramCaption=my%caption不会打开最后一个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]; } } 

在这里输入图像说明

非常奇怪的是,工作完美,直到我转过卸载,然后安装instagram。 不知道这是否有什么好做的

instagram://library?AssetPath=\(assetsLibraryUrl)停止工作。 Instagram开发者可能转移到Photos框架,不再使用AssetsLibrary。

有了这个假设,我尝试了几个其他的参数名称,发现instagram://library?LocalIdentifier=\(localID)其中localId是您的PHAsset现在工作。

这仍然是没有logging的,所以它可以在任何未来版本的Instagram中打破。

很长时间后恢复这个任务,并考虑borisgolovnev's答案和ALAssetsLibrary已被弃用,最后的解决scheme是:

 - (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]]; } 

别忘了

 #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> 

@ borisgolovnev的解决scheme实际上工作。 您可以使用下面的代码获取上次保存的video的localIdentifier。 通过instagram:// library?LocalIdentifier =(localID)打开Instagram,并select您的video。

 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 } 

使用这个代码

 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将只显示那些保存在您在instagramURL中设置的path的图像/video。 这条道路应该是绝对的。

如果仍然不显示,则在数组的info.plist文件中添加LSApplicationQueriesSchemes,将其item0添加为instagram。

更换

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

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

这对我有用!

迅速3获得最新的标识符简单和快速

  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 } 

这里是在Instagram上分享video的代码:可能是你需要为substringFromIndex添加条件,但它的工作。

  - (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]; } } } 
 if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { NSLog(@"Open Instagram!!"); //enter code here [[UIApplication sharedApplication/*<enter your code here>*/] openURL:instagramURL];