0

I want to enable a feature in my iOS app to share image on Instagram directly. While the conventional way is to present a share sheet provided by Apple, I prefer to have an Instagram button that allows users to share image easily without opening the share sheet. I have found two URLs that can be used for this purpose: "instagram://share" for posts and "instagram-stories://share" for stories. However, I have seen other apps that display a 'Share to Instagram' sheet with three default options as shown in figure. I am interested in implementing this feature in my app. figure3

My code is given below:

func shareOnInstagram(image: UIImage) {
    if let url = URL(string: "instagram://share") {
        if UIApplication.shared.canOpenURL(url) {
            guard let imageData = image.jpegData(compressionQuality: 1.0) else { return }
            let pasteboardItem : [String: Any] = ["com.instagram.sharedSticker.stickerImage": imageData]
            ]
            let pasteboardOptions = [
                UIPasteboard.OptionsKey.expirationDate : Date().addingTimeInterval(300)
            ]
            UIPasteboard.general.setItems([pasteboardItem], options: pasteboardOptions)
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
        else {
            print("User doesn't have IG on their device.")
        }
    }
}

I have been trying to find a way to integrate this sheet, but unfortunately, not been successful yet. Therefore, I am seeking guidance on how to integrate this sheet into my app to allow for direct sharing to Instagram.

Jabed Dhali
  • 167
  • 6

1 Answers1

0

To implement custom sharing of an image to Instagram without presenting a share sheet, you can use the Instagram URL scheme in your code. Here are the steps to achieve this:

  1. First, you need to check if the Instagram app is installed on the device. You can do this using the canOpenURL method of the UIApplication class.
guard let instagramURL = URL(string: "instagram://app") else { return }
if UIApplication.shared.canOpenURL(instagramURL) {
   // Instagram app is installed on the device
} else {
   // Instagram app is not installed on the device
}
  1. If the Instagram app is installed, you can create a URL with the image to be shared using the instagram://share URL scheme.
let instagramShareURL = URL(string: "instagram://share?media=yourImageURL")

Replace "yourImageURL" with the URL of the image you want to share on Instagram.

  1. Once you have the Instagram share URL, you can open it using the open method of the UIApplication class.
UIApplication.shared.open(instagramShareURL, options: [:], completionHandler: nil)

This will open the Instagram app and allow the user to share the image directly without presenting a share sheet.

Note: Instagram's URL scheme and API are subject to change without notice, so it's a good idea to check the Instagram Developer Documentation for the latest information.

Luca Petra
  • 146
  • 6
  • This custom sharing is also for just instagram posting, not for story or messages. I want to present 'Share to Instagram' page as shown above. – Jabed Dhali May 05 '23 at 18:02