Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a story to Instagram with a background image and a sticker - IOS Swift

I'm trying to share a story with a background image a a sticker image via URL Scheme on my ios app, i am using the attached code and it dose not work. When i'm trying to share just a background image or just a sticker it does work. But when im trying share both a background image and a sticker in top of it, it dose not work.

Any Ideas?

func shareToInstagram(deepLinkString : String){
        let url = URL(string: "instagram-stories://share")!
        if UIApplication.shared.canOpenURL(url){

            let backgroundData = UIImageJPEGRepresentation(UIImage(named: "shop_placeholder")!, 1.0)!
            let creditCardImage = UIImage(named: "share_instagram")!
            let stickerData = UIImagePNGRepresentation(creditCardImage)!
            let pasteBoardItems = [
                                    ["com.instagram.sharedSticker.backgroundImage" : backgroundData],
                                    ["com.instagram.sharedSticker.stickerImage" : stickerData],
                                  ]

            if #available(iOS 10.0, *) {

                UIPasteboard.general.setItems(pasteBoardItems, options: [.expirationDate: Date().addingTimeInterval(60 * 5)])
            } else {
                UIPasteboard.general.items = pasteBoardItems
            }
            UIApplication.shared.openURL(url)
        }
like image 730
aviv_elk Avatar asked Mar 05 '23 14:03

aviv_elk


2 Answers

I copy pasted OP's code for use in my own app (only substituting different UIImages) and found only 1 issue, pasteboard items should be contained in a single array otherwise instagram will render only the first item (in this case the background layer). To fix this, replace the declaration of pasteboard items with the following code

let pasteBoardItems = [
                ["com.instagram.sharedSticker.backgroundImage" : backgroundData,
                "com.instagram.sharedSticker.stickerImage" : stickerData]
            ]

(basically just remove the close and open bracket separating the two items)

Also as a previous answer stated, make sure "instagram-stories" is included in LSApplicationQueriesSchemes in the info.plist file

I use this exact code in my app and it now works perfect

like image 124
Alec Barton Avatar answered Apr 30 '23 20:04

Alec Barton


Everything is correct, my code is similar and it works for iOS 11+. I suggest you the following:

  1. check the image data you pass to pasteboard (jpg can't be converted with UIImagePNGRepresentation and vice versa)
  2. check the info.plist. You should enable "instagram-stories" scheme in it (LSApplicationQueriesSchemes key)
like image 32
Vyachaslav Gerchicov Avatar answered Apr 30 '23 19:04

Vyachaslav Gerchicov