URL Scheme to post to Instagram Stories

对着背影说爱祢 提交于 2019-12-18 04:27:26

问题


Is there a way to post a video or photo directly to the Instagram Stories of the user's Instagram account? For a normal photo share on Instagram, you can use the URL Scheme and open the Editor directly. Is there a way for Stories, too?

Thanks!


回答1:


Swift 4.2 version of knshn's answer:

func shareBackgroundImage() {
    let image = UIImage(imageLiteralResourceName: "backgroundImage")

    if let pngImage = image.pngData() {
        backgroundImage(pngImage, attributionURL: "http://your-deep-link-url")
    }
}

func backgroundImage(_ backgroundImage: Data, attributionURL: String) {
    // Verify app can open custom URL scheme, open if able

    guard let urlScheme = URL(string: "instagram-stories://share"),
        UIApplication.shared.canOpenURL(urlScheme) else {
            // Handle older app versions or app not installed case

            return
    }

    let pasteboardItems = [["com.instagram.sharedSticker.backgroundImage": backgroundImage,
                            "com.instagram.sharedSticker.contentURL": attributionURL]]
    let pasteboardOptions: [UIPasteboard.OptionsKey: Any] = [.expirationDate: Date().addingTimeInterval(60 * 5)]

    // This call is iOS 10+, can use 'setItems' depending on what versions you support
    UIPasteboard.general.setItems(pasteboardItems, options: pasteboardOptions)

    UIApplication.shared.open(urlScheme)
}



回答2:


Instagram officially supports this since March 2018. https://developers.facebook.com/docs/instagram/sharing-to-stories/

For iOS:

You need to add instagram-stories to the LSApplicationQueriesSchemes key in your app's Info.plist.

This sample code shows how to pass the Instagram app a background layer image asset and an attribution deep link.

- (void)shareBackgroundImage {
      [self backgroundImage:UIImagePNGRepresentation([UIImage imageNamed:@"backgroundImage"])
             attributionURL:@"http://your-deep-link-url"];
}

- (void)backgroundImage:(NSData *)backgroundImage 
         attributionURL:(NSString *)attributionURL {

  // Verify app can open custom URL scheme, open if able
  NSURL *urlScheme = [NSURL URLWithString:@"instagram-stories://share"];
  if ([[UIApplication sharedApplication] canOpenURL:urlScheme]) {

        // Assign background image asset and attribution link URL to pasteboard
        NSArray *pasteboardItems = @[@{@"com.instagram.sharedSticker.backgroundImage" : backgroundImage,
                                       @"com.instagram.sharedSticker.contentURL" : attributionURL}];
        NSDictionary *pasteboardOptions = @{UIPasteboardOptionExpirationDate : [[NSDate date] dateByAddingTimeInterval:60 * 5]};
        // This call is iOS 10+, can use 'setItems' depending on what versions you support
        [[UIPasteboard generalPasteboard] setItems:pasteboardItems options:pasteboardOptions];

        [[UIApplication sharedApplication] openURL:urlScheme options:@{} completionHandler:nil];
  } else {
      // Handle older app versions or app not installed case
  }
}



回答3:


You can use this answer to convert a PHAsset's identifier into an asset URL, then format it into an instagram:// URL using this logic. That used to only be capable of making conventional Instagram posts, but as of the past few weeks it actually prompts users if they want to make a story or a post with the asset you provided on launch!




回答4:


If you download .ipa of instagram and look at their Info.plist, we can found :

    <key>CFBundleURLSchemes</key>
    <array>
      <string>instagram-stories</string>
      <string>fb124024574287414</string>
      <string>instagram</string>
      <string>instagram-capture</string>
      <string>fsq+kylm3gjcbtswk4rambrt4uyzq1dqcoc0n2hyjgcvbcbe54rj+post</string>
    </array>

but of course it's private (not official) and not documented by Instagram. If anyone now how to use it/find parameters, I'm really curious to know !



来源:https://stackoverflow.com/questions/44283082/url-scheme-to-post-to-instagram-stories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!