2015-12-11 24 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今のあなたのPHAsset作品のlocalIdentifierであることがわかりました。

これは、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のはあなただけはinstagramURLに設定されたパスに保存されたものiamges /動画が表示されます何かを持っているかどうかを知るしないでください。その道は絶対的でなければなりません。

まだ表示されない場合は、配列のinfo.plistファイルにLSApplicationQueriesSchemesを追加し、そのitem0をinstagramとして追加します。

+2

残念ながら、私はInstagramカスタムURLスキームから "AssetPath"引数が削除されていると思います:https://www.instagram.com/developer/mobile-sharing/iphone-hooks/( "InstagramCaption"と同様に) – Workshed

+0

質問のコードとの違いは? – jose920405

+1

はい、そうです、iOS9でこれをチェックしていません。それを開く新しい方法を見つけることができます。開発者サイトには詳細はほとんどありません。 –

0

これは私のために働いた

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

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

を交換してください!

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

迅速な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]; 
       } 
      } 

     } 
関連する問題