2

I am integrating sharing options from my app to Snapchat. I have a dynamic URL obtained in an object and clicking the Snapchat's share button directly opens the app if Snapchat is there on the device and show the text with the link. I am using the below code to share which gives an error on Snapchat. Below is my Code.

func shareTextOnSnapchat(obj:VideoData) {
    let shaUrl = URL(string: obj.share_url ?? "")
    if let myURL:URL = shaUrl{
        let promoText = "Check out this great new video from \(obj.name ?? ""), I found on talent app"
        let shareString = "snapchat://text=\(promoText)&url=\(myURL)"
        let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
        let url = URL(string: escapedShareString)
        UIApplication.shared.openURL(url!)
    }
}

enter image description here

sandpat
  • 1,478
  • 12
  • 30
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25

1 Answers1

0

I have used this to post video to snapchat. You have option to either post text or a video.

Pod used

pod 'SnapSDK', :subspecs => ['SCSDKCreativeKit']

import SCSDKCreativeKit

var scInstalled = false

override func viewDidLoad() {
    super.viewDidLoad()
    scInstalled = schemeAvailable(scheme: "snapchat://")
}

func ShowSnapchat(){
    if scInstalled {
        //shareTextOnSnapchat(obj:videoObj ?? VideoData())
        shareFileOnSnapchat(obj:videoObj ?? VideoData())
    }else{
        downloadSharingAppAlert(appName:"Snapchat")
    }
}


func shareTextOnSnapchat(obj:VideoData) {
    let shaUrl = URL(string: obj.share_url ?? "")
    if let myURL:URL = shaUrl{
        let originalString = "\(myURL)"
        let escapedString = originalString.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed)
        //let url  = URL(string: "snapchat://snap?text=\(escapedString!)")
        
        let url  = URL(string: "https://www.snapchat.com/send?text=\(escapedString!)")
        
        if UIApplication.shared.canOpenURL(url! as URL)
        {
            UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
        }
    }
}


func shareFileOnSnapchat(obj:VideoData){
    //// SHARE VIDEO
    LoadingOverlay.shared.showLoaderView(view: self.view)
    let shaUrl = URL(string: obj.output_vid ?? "")
    if let myURL:URL = shaUrl{
        let snapVideo = SCSDKSnapVideo(videoUrl: myURL)
        let snapContent = SCSDKVideoSnapContent(snapVideo: snapVideo)
        // Send it over to Snapchat
        snapAPI.startSending(snapContent) { (error) in
            if let error = error {
                print(error.localizedDescription)
                LoadingOverlay.shared.hideLoaderView()
                MyCustomAlert.sharedInstance.ShowAlert(vc: self, myTitle: "", myMessage: StringClass.sharedInstance.lcStr_oopsSomethingWentwrong)
            } else {
                // success
                print("Posted to snapchat")
                LoadingOverlay.shared.hideLoaderView()
                MyCustomAlert.sharedInstance.ShowAlert(vc: self, myTitle: "", myMessage: StringClass.sharedInstance.lcStr_postedToSnapchat)
            }
        }
    }
}
}

func downloadSharingAppAlert(appName:String){
    var appStoreURL = "https://apps.apple.com/in/app/snapchat/id447188370"
    //Open Appstore for Download
}
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25