2015-12-11 201 views
9

這是我的代碼。該文件已正確添加到照片庫,但在Instagram應用程序此URL - >instagram://library?AssetPath=assets-library%3A%2F%2Fasset%2Fasset.mp4%3Fid=5EDBD113-FF57-476B-AABB-6A59F31170B5&ext=mp4&InstagramCaption=my%caption不打開最後一個視頻。Instagram掛鉤預選媒體問題

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

很奇怪的東西是完美的工作,直到我轉身卸載,然後安裝的Instagram。不知道這個事做

+1

你找到答案了嗎?我遇到了同樣的問題。 – scientiffic

+0

@scientiffic找不到解決方法,現在我忽略了這張票。 – jose920405

+0

@ jose920405不幸的消息,無論如何,謝謝。 – saiday

回答

31

instagram://library?AssetPath=\(assetsLibraryUrl) 前一段時間停止工作。 Instagram開發者可能轉移到Photos框架並不再使用AssetsLibrary。

有了這個假設,我試過其他幾個參數名稱和發現instagram://library?LocalIdentifier=\(localID)其中localId是你的作品PHAssetlocalIdentifier現在。

這仍然沒有記錄,因此它可以在任何未來版本的Instagram中打破。

+3

將視頻保存到圖庫後如何獲取此LocalIdentifier? –

+0

謝謝,完美的作品 – jose920405

+0

@Devang Goswami你可以從資產url字符串獲得LocalIdentifier。 – jay

-4
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { 
NSLog(@"Open Instagram!!"); //enter code here 
[[UIApplication sharedApplication/*<enter your code here>*/] openURL:instagramURL]; 
+0

我不明白你。你可以更清楚一點 – jose920405

+0

請詳細說明如何回答這個問題。 – JAL

0

使用此代碼

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將只顯示那些iamges /它被保存在您的instagramURL設置路徑的視頻。這條道路應該是絕對的。

如果它仍然沒有顯示,則在info.plist文件中添加LSApplicationQueriesSchemes,添加它的item0作爲instagram。

+2

不幸的是,我認爲「AssetPath」參數已從instagram自定義URL方案中刪除:https://www.instagram.com/developer/mobile-sharing/iphone-hooks/(與「InstagramCaption」相同) – Workshed

+0

與問題的代碼有什麼不同? – jose920405

+1

是的,你說得對,我沒有在iOS9中檢查過它。讓我找到一種打開它的新方法。他們的開發人員網站上沒有太多細節。 –

0

更換

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

隨着

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

這爲我工作!

1

@ 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 
} 
0

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 
    } 
3

恢復時間長了以後這個任務,並考慮到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> 
+0

非常需要的答案,謝謝。 –

+0

如何做視頻? –

0

這裏是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]; 
       } 
      } 

     }