发布到 Instagram 故事的 URL 方案

Jon*_*ony 4 objective-c url-scheme ios instagram instagram-api

有没有办法将视频或照片直接发布到用户 Instagram 帐户的 Instagram 故事中?对于 Instagram 上的普通照片共享,您可以使用 URL Scheme 并直接打开编辑器。故事也有办法吗?

谢谢!

Tam*_*gel 11

Swift 4.2 版本的knshn 答案

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)
}
Run Code Online (Sandbox Code Playgroud)


kns*_*shn 7

Instagram 自 2018 年 3 月起正式支持此功能。https://developers.facebook.com/docs/instagram/sharing-to-stories/

对于 iOS:

您需要添加instagram-storiesLSApplicationQueriesSchemes应用程序的Info.plist.

此示例代码展示了如何向 Instagram 应用传递背景层图像资产和归因深层链接。

- (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
  }
}
Run Code Online (Sandbox Code Playgroud)


Tim*_*sen 0

您可以使用此答案将 PHAsset 的标识符转换为资产 URL,然后instagram://使用此逻辑将其格式化为 URL 。它过去只能发布传统的 Instagram 帖子,但在过去的几周里,它实际上会提示用户是否想使用您在发布时提供的资源来制作故事或帖子!