Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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!

like image 471
Jony Avatar asked May 31 '17 11:05

Jony


1 Answers

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)
}
like image 165
Tamás Sengel Avatar answered Oct 03 '22 12:10

Tamás Sengel