Search code examples
iosswiftinstagramsharing

share videoURL on instagram app iOS


func shareVideoToInstagram() {
    let strURL = "http://mobmp4.org/files/data/2480/Tutak%20Tutak%20Tutiya%20Title%20Song%20-%20Remix%20-%20Drunx%20-%20Mp4.mp4"

    let caption = "Some Preloaded Caption"
    let captionStr = caption.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! as String

    let videoURL = URL(fileURLWithPath: strURL, isDirectory: false)
    let library = ALAssetsLibrary()

    library.writeVideoAtPath(toSavedPhotosAlbum: videoURL) { (newURL, error) in

        if let instagramURL = NSURL(string: "instagram://library?AssetPath=\(videoURL.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!)&InstagramCaption=\(captionStr)") {
        print(instagramURL)

        if UIApplication.shared.canOpenURL(instagramURL as URL) {
            UIApplication.shared.openURL(instagramURL as URL)
        }

        } else {
            print("NO")
        }
    }
}

I got instagramURL like this:

instagram://library?AssetPath=file:%2F%2F%2Fhttp:%2Fmobmp4.org%2Ffiles%2Fdata%2F2480%2FTutak%252520Tutak%252520Tutiya%252520Title%252520Song%252520-%252520Remix%252520-%252520Drunx%252520-%252520Mp4.mp4&InstagramCaption=Some%20Preloaded%20Caption

And I successfully openURL but I can not found my video which I want to share on instagram.


Solution

  • func shareVideoToInstagram() {
    
       let videoFilePath = "http://mobmp4.org/files/data/2480/Tutak%20Tutak%20Tutiya%20Title%20Song%20-%20Remix%20-%20Drunx%20-%20Mp4.mp4"
    
    
       let instagramURL = NSURL(string: "instagram://app")
    
        if (UIApplication.shared.canOpenURL(instagramURL! as URL)) {
    
          let url = URL(string: ("instagram://library?AssetPath="+videoFilePath))
    
            if UIApplication.shared.canOpenURL(url!) {
                UIApplication.shared.open(url!, options: [:], completionHandler:nil)
            }
    
        } else {
            print(" Instagram isn't installed ")
        }
    
      }