简体   繁体   English

URL 计划发布到 Instagram 故事

[英]URL Scheme to post to Instagram Stories

Is there a way to post a video or photo directly to the Instagram Stories of the user's Instagram account?有没有办法将视频或照片直接发布到用户 Instagram 帐户的 Instagram 故事? For a normal photo share on Instagram, you can use the URL Scheme and open the Editor directly. Instagram 上普通的照片分享,可以使用 URL Scheme,直接打开 Editor。 Is there a way for Stories, too?故事也有办法吗?

Thanks!谢谢!

Swift 4.2 version of knshn's answer : 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)
}

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

For iOS:对于 iOS:

You need to add instagram-stories to the LSApplicationQueriesSchemes key in your app's Info.plist .您需要将instagram-stories添加到应用程序的Info.plistLSApplicationQueriesSchemes键。

This sample code shows how to pass the Instagram app a background layer image asset and an attribution deep link.此示例代码展示了如何向 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
  }
}

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

If you download .ipa of instagram and look at their Info.plist, we can found :如果你下载 instagram 的 .ipa 并查看他们的 Info.plist,我们可以发现:

    <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.但当然它是私人的(不是官方的)并且没有被 Instagram 记录。 If anyone now how to use it/find parameters, I'm really curious to know !如果有人现在如何使用它/查找参数,我真的很想知道!

For SwiftUI对于 SwiftUI

import SwiftUI

fileprivate let wands: [Wand] = [
    Wand(name: "Dumbledore's Wand", stickerAsset: "dumbledore"),
    Wand(name: "Potter's Wand", stickerAsset: "potter"),
    Wand(name: "Granger's Wand", stickerAsset: "granger")
]

struct ContentView: View {
    
    func shareToInstagramStories(
        stickerImage: String,
        stickerLink: String,
        backgroundTopColor: String = "#7F0909",
        backgroundBottomColor: String = "#303030"
    ) {
        // 1. Get a data object of our UIImage...
        let stickerImageData = UIImage(named: stickerImage)?.pngData()
        
        // 2. Verify if we are able to open instagram-stories URL schema.
        // If we are able to, let's add our Sticker image to UIPasteboard.
        
        let urlScheme = URL(string: "instagram-stories://share?source_application=\(Bundle.main.bundleIdentifier ?? "")")
        
        if let urlScheme = urlScheme {
            if UIApplication.shared.canOpenURL(urlScheme) {
                
                var pasteboardItems: [[String : Any]]? = nil
                if let stickerImageData = stickerImageData {
                    pasteboardItems = [
                        [
                            "com.instagram.sharedSticker.stickerImage": stickerImageData,
                            "com.instagram.sharedSticker.backgroundTopColor": backgroundTopColor,
                            "com.instagram.sharedSticker.backgroundBottomColor": backgroundBottomColor,
                            "com.instagram.sharedSticker.link": stickerLink
                        ]
                    ]
                }
                
                // We'll expire these pasteboard items in 5 minutes...
                let pasteboardOptions = [
                    UIPasteboard.OptionsKey.expirationDate: Date().addingTimeInterval(60 * 5)
                ]
                
                if let pasteboardItems = pasteboardItems {
                    UIPasteboard.general.setItems(pasteboardItems, options: pasteboardOptions)
                }
                
                // 3. Try opening the URL...
                UIApplication.shared.open(urlScheme, options: [:], completionHandler: nil)
            } else {
                // App may not be installed. Handle those errors here...
                print("Something went wrong. Maybe Instagram is not installed on this device?")
            }
        }
    }
    
    var body: some View {
        NavigationView {
            List(wands, id: \.name){ wand in
                Text(wand.name).onTapGesture {
                    shareToInstagramStories(stickerImage: wand.stickerAsset, stickerLink: "www.google.com")
                }
            }
            .navigationBarTitle(Text("Ollivanders"), displayMode: .inline)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct Wand {
    let name: String
    let stickerAsset: String
}

Special thanks to [Ishan Chhabra][https://ishanchhabra.com/thoughts/sharing-to-instagram-stories]特别感谢 [Ishan Chhabra][https://ishanchhabra.com/thoughts/sharing-to-instagram-stories]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM