Drashti
Drashti

Reputation: 21

How to post image to instagram story in Swift?

is there any way to share image to instagram story directly. I can Only Share image using default share functionality i am using swift 5 and xcode 11.

Upvotes: 0

Views: 994

Answers (1)

Sahdevsinh Chavda
Sahdevsinh Chavda

Reputation: 526

iOS Swift

Import Below Module.

import AVKit
import Photos

Add Below function for share image on Instagram.

 func shareOnInstagram() {

        let instagramURL = URL(string: "instagram://app")

        PHPhotoLibrary.shared().performChanges({
            let request = PHAssetChangeRequest.creationRequestForAsset(from: yourImage)

            let assetID = request.placeholderForCreatedAsset?.localIdentifier ?? ""
            let shareURL = "instagram://library?LocalIdentifier=" + assetID

            DispatchQueue.main.async {
                if UIApplication.shared.canOpenURL(instagramURL!) {
                    if let urlForRedirect = URL(string: shareURL) {
                        UIApplication.shared.open(urlForRedirect, options: [:], completionHandler: nil)
                    }
                } else {
                    //show alert for Instagram is not installed in your device.
                }
            }
        }, completionHandler: { (bSuccess, error) in

        })
    }                   

Upvotes: 1

Related Questions