2015-12-11 6 views
9

Dieses ist mein Code. Die Datei wurde korrekt zur Fotogalerie hinzugefügt, aber in Instagram App diese URL ->instagram://library?AssetPath=assets-library%3A%2F%2Fasset%2Fasset.mp4%3Fid=5EDBD113-FF57-476B-AABB-6A59F31170B5&ext=mp4&InstagramCaption=my%caption nicht das letzte Video öffnen.Instagram hooks Medienauswahl vorwählen

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

etwas sehr seltsam ist, dass funktionierte perfekt, bis ich deinstallieren gedreht und dann instagram installieren. Sie wissen nicht, ob dies etwas hat

+1

Haben Sie eine Antwort gefunden? Ich stoße auf das gleiche Problem. – scientiffic

+0

@scientiffic es war nicht möglich, einen Weg zu finden, um es zu beheben, für jetzt habe ich dieses Ticket vernachlässigt. – jose920405

+0

@ jose920405 leider Nachrichten, danke trotzdem. – saiday

Antwort

31

instagram://library?AssetPath=\(assetsLibraryUrl) vor einiger Zeit nicht mehr funktioniert. Instagram-Entwickler sind wahrscheinlich in das Photos-Framework gewechselt und verwenden die AssetsLibrary nicht mehr.

Mit dieser Annahme habe ich mehrere andere Parameternamen versucht und festgestellt, dass instagram://library?LocalIdentifier=\(localID) wo localId die jetzt localIdentifier Ihrer PHAsset Werke ist.

Dies ist immer noch undokumentiert wie es war, so dass es in jeder zukünftigen Version von Instagram einbrechen kann.

+3

Wie erhält man dieses LocalIdentifier nach dem Speichern des Videos in der Galerie? –

+0

Danke, funktioniert perfekt – jose920405

+0

@Devang Goswami können Sie LocalIdentifier von Asset-URL-Zeichenfolge erhalten. – jay

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

Ich kann dich nicht verstehen. Sie könnten ein wenig klarer sein – jose920405

+0

Bitte erläutern Sie, wie dies die Frage beantwortet. – JAL

0

Verwenden Sie diesen 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 wird nur die iamges/Videos zeigt zu tun, die im Pfad gespeichert werden, die Sie in instagramURL gesetzt. Dieser Pfad sollte absolut sein.

Wenn es immer noch nicht angezeigt wird, fügen Sie LSApplicationQueriesSchemes in der Datei info.plist des Arrays hinzu, fügen Sie item0 als Instagram hinzu.

+2

Leider habe ich das "AssetPath" -Argument aus dem instagram-eigenen URL-Schema entfernt: https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ (wie auch die "InstagramCaption") – Workshed

+0

und was ist der Unterschied mit dem Code für die Frage? – jose920405

+1

Ja, Sie haben recht, ich habe dies in iOS9 nicht überprüft. Lass mich einen neuen Weg finden, es zu öffnen. Auf ihrer Entwicklerseite gibt es nicht viele Details. –

0

ersetzen

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

Mit

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

Dieser arbeitete für mich!

1

@ borisgolovnev Lösung funktioniert tatsächlich. Sie können den localIdentifier Ihres zuletzt gespeicherten Videos mit dem folgenden Code abrufen. Übergeben Sie es mit instagram: // library? LocalIdentifier = (localID) öffnet Instagram mit Ihrem ausgewählten 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 
} 
0

swift 3 die neueste Identifier einfache und schnelle

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

wieder aufnehmen, diese Aufgabe nach einer langen Zeit erhalten und die borisgolovnev's Antwort und ALAssetsLibrary ist veraltet bedenkt, die endgültige Lösung ist :

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

Vergessen Sie nicht,

#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

Viel benötigte Antwort, danke. –

+0

Wie mache ich das für Videos? –

0

Hier Code ist Video auf instagram für den Austausch: sein kann Sie Bedingung für substringFromIndex aber seine Arbeits hinzufügen müssen.

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

     }