6

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!

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Jony
  • 111
  • 1
  • 1
  • 5
  • sorry there isn't API or Scheme for stories yet. https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ please +1 if it's helpful – Prakhar Singh May 31 '17 at 11:11

5 Answers5

12

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)
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • Can someone please elaborate what attributionURL: "http://your-deep-link-url" would look like? I'm trying to get the apps App Store page link appear in the top left corner, under the Facebook Story users name. – Krekin Jun 24 '21 at 15:58
  • @Krekin this was officially a "closed beta" feature and appears to have been removed from the documentation since. – b_ray Feb 21 '22 at 00:58
  • According to the documentation here https://developers.facebook.com/docs/instagram/sharing-to-stories/, the new way of attributing appears to be using this url-scheme: "instagram-stories://share?source_application=com.my.app" – b_ray Feb 21 '22 at 01:02
7

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
  }
}
knshn
  • 3,401
  • 1
  • 21
  • 22
  • This is a borderline [link-only answer](//meta.stackexchange.com/q/8231). You should expand your answer to include as much information here, and use the link only for reference. – Blue Aug 02 '18 at 17:25
  • Hi Is there a way that app can share data on Instagram when Instagram app not installed on device? – CrazyPro007 Feb 01 '19 at 09:22
0

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!

Tim Johnsen
  • 1,471
  • 14
  • 33
0

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 !

hefgi
  • 484
  • 5
  • 14
0

For 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]

Anubhav Singh
  • 905
  • 6
  • 5