Instagram 挂钩预选媒体问题

Posted

技术标签:

【中文标题】Instagram 挂钩预选媒体问题【英文标题】:Instagram hooks pre-select media issue 【发布时间】:2016-03-17 12:53:36 【问题描述】:

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

- (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。不知道这有没有关系

【问题讨论】:

你找到答案了吗?我遇到了同样的问题。 @scientiffic 无法找到修复它的方法,目前我忽略了这张票。 @jose920405 不幸的消息,还是谢谢你。 @jose920405 你能找到LocalIdentifier吗? 对不起@DevangGoswami,我没有时间尝试这个,因为我已经放弃了这个问题,我今天必须恢复,因为我需要这个用于另一个项目。查看我最近的回复 ==> ***.com/questions/34226433/… 【参考方案1】:

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

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

这仍然像以前一样没有记录,因此它可以在 Instagram 的任何未来版本中破坏。

【讨论】:

将视频保存到图库后如何获取此 LocalIdentifier? @Devang Goswami 您可以从资产 url 字符串中获取 LocalIdentifier。 有没有办法分享视频网址而不是本地网址?【参考方案2】:

在很长一段时间后恢复这个任务,并考虑到 borisgolovnev's 答案和ALAssetsLibrary 已被弃用,最终解决方案是:

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

【讨论】:

视频怎么做?【参考方案3】:

@borisgolovnev 的解决方案确实有效。您可以使用下面的代码获取上次保存的视频的 localIdentifier。使用 instagram://library?LocalIdentifier=(localID) 传递它会打开 Instagram 并选择您的视频。

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

【讨论】:

【参考方案4】:

使用此代码

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 中设置的路径的图片/视频。那条路径应该是绝对的。

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

【讨论】:

不幸的是,我认为“AssetPath”参数已从 instagram 自定义 URL 方案中删除:instagram.com/developer/mobile-sharing/iphone-hooks(与“InstagramCaption”一样) 和问题的代码有什么区别? 是的,你是对的,我没有在 ios9 中检查过这个。让我找到一种新的方法来打开它。他们的开发者网站上没有太多细节。 谢谢我的朋友,我很感激 @PratikPatel AssetPath 问题有什么进展吗?【参考方案5】:

替换

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

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

这对我有用!

【讨论】:

【参考方案6】:

swift 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
  

【讨论】:

【参考方案7】:

这是在 instagram 上分享视频的代码: 可能您需要为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];
                
            

        

【讨论】:

【参考方案8】:
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) 
NSLog(@"Open Instagram!!"); //enter code here
[[UIApplication sharedApplication/*<enter your code here>*/] openURL:instagramURL];

【讨论】:

我听不懂你。你可以清楚一点 请详细说明这是如何回答问题的。

以上是关于Instagram 挂钩预选媒体问题的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Twitter 实体获取直接的 Instagram 链接?

Instagram 媒体搜索时间戳问题

提供来自 instagram 的所有用户媒体

我无法通过 instagram API 获取 instagram 用户的媒体详细信息

Instagram/feed API 媒体 URL 显示“URL 签名已过期”

获取所有媒体 Instagram。超过33张图片