Have successfully integrated the universal link into app , please guide how can I redirect to store link if app is not installed , please suggest the correct approach to do it thanks
please try this:
guard let link = URL(string: sahareUrl) else { return }
let dynamicLinksDomain = "name.page.link"
let linkBuilder = FIRDynamicLinkComponents(link: link, domain: dynamicLinksDomain)
linkBuilder.socialMetaTagParameters = FIRDynamicLinkSocialMetaTagParameters()
linkBuilder.options = FIRDynamicLinkComponentsOptions()
linkBuilder.options?.pathLength = .short
linkBuilder.socialMetaTagParameters?.imageURL = URL(string: imagename)
linkBuilder.iOSParameters = FIRDynamicLinkIOSParameters(bundleID: "yourBundleId")
linkBuilder.iOSParameters?.appStoreID = "storeId"
linkBuilder.iOSParameters?.iPadBundleID = "yourBundleId"
linkBuilder.androidParameters = FIRDynamicLinkAndroidParameters(packageName: "packageNameOfAndroid")
linkBuilder.socialMetaTagParameters?.title = title
linkBuilder.socialMetaTagParameters?.descriptionText = description
guard let longDynamicLink = linkBuilder.url else { return }
print(longDynamicLink)
FIRDynamicLinkComponents.shortenURL(longDynamicLink, options: nil) { url, warnings, error in
self.runOnUIThread {
if url != nil{
self.shareAds(item: [url!])
}else{
NVActivityIndicatorHelper.hideIndicatorInView()
}
}
}
It may helps you.Thank you
Related
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!)
}
}
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
}
I am using Firebase Dynamic links to send informations via Mail.
The links work when i try to open them when the app is installed.
However they do not work correctly when the app is not installed.
When i open a link when the app is not installed, they open my homepage, more precise the actual link.
My code is here:
static func generateDynamicLinkFromProduct(product: Product, completion: #escaping (URL?) -> Void) {
let domain = "https://my-homepage.com/"
let bundleID = Bundle.main.bundleIdentifier
var package = "my.app.on.android"
let meta = product.metaJson!
guard let link = URL(string: domain + "product=" + meta) else {
completion(nil)
return
}
let dynamicLinksDomainURIPrefix = "https://myapp.page.link"
let linkBuilder = DynamicLinkComponents(link: link, domainURIPrefix: dynamicLinksDomainURIPrefix)
linkBuilder?.iOSParameters = DynamicLinkIOSParameters(bundleID: bundleID!)
linkBuilder?.iOSParameters?.appStoreID = "APP_STORE_ID"
linkBuilder?.navigationInfoParameters = DynamicLinkNavigationInfoParameters()
linkBuilder?.navigationInfoParameters?.isForcedRedirectEnabled = true
linkBuilder?.androidParameters = DynamicLinkAndroidParameters(packageName: package)
let options = DynamicLinkComponentsOptions()
options.pathLength = .short
linkBuilder?.options = options
let longLink = linkBuilder?.url
print(longLink)
linkBuilder?.shorten() { url, warnings, error in
print(url)
if (error == nil) {
completion(url)
} else {
completion(nil)
}
}
}
(domain, bundle,package and prefix are replaced by the data of the actual app)
The dynamic link opens the actual content of link, i.e. it opens https//my-homepage.com/product=
the output of the debug shows this:
€dit:
The generated long link contains isi and ibi parameters
Check your appStoreId.
I had the same problem and it turned out I switched my developer team ID with the appStoreId of my application.
I followed all the steps from documentation and integrated SnapSDK in my iOS app but when I click the share button in my app it directs me to the snapchat but ends up with an error saying
"Something went wrong please try again later".
private func didTapSnapchatShare(cell: FeedTableViewCell){
print("Share button tapped ")
cell.pauseVideoAndAnimation()
showProgressIndicator(view: self.view)
var watermarkStr = ""
if let userName = cell.cellDataSource?.user?.name {
watermarkStr = userName
}
let promptImage = cell.promptView?.asImage()
cell.slPlayer?.exportVideo(withWatermarkString: watermarkStr, promptImage: promptImage, completionHandler: { status, filePathURL in
DispatchQueue.main.async {
hideProgressIndicator(view: self.view)
if status, let filePathURL = filePathURL {
let url = URL(string: "snapchat://")!
if(!UIApplication.shared.canOpenURL(url)){
if let reviewURL = URL(string: "https://itunes.apple.com/us/app/snapchat/id447188370?mt=8"), UIApplication.shared.canOpenURL(reviewURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(reviewURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(reviewURL)
}
return
}
}
let video = SCSDKSnapVideo(videoUrl:filePathURL)
let videoContent = SCSDKVideoSnapContent.init(snapVideo: video)
let api = SCSDKSnapAPI(content: videoContent)
api.startSnapping(completionHandler: { (error: Error?) in
print(error?.localizedDescription)
})
}
}
})
}
Check if your file path url is correct or not this happens when we are not using the correct file path sdk will not be able to find the image.
facing some issue to generate shortenURL from firebase dynamic-links , I am able to get longDynamicLink url . but
here is my code , I am using https://firebase.google.com/docs/dynamic-links/ios/create following steps DynamicLinkComponents.shortenURL completion not getting call and there is no error also
guard let longDynamicLink = linkBuilder.url else { return "test" }
print("The long URL is: \(longDynamicLink)")
DynamicLinkComponents.shortenURL(longDynamicLink, options: nil) { url, warnings, error in
guard let url = url, error != nil else { return }
print("The short URL is: \(url)")
}
DynamicLinkComponents.shortenURL this part is not executing
Try This Code. This Code Working Fine For Me.
let shareLink:String = "http://YourURL"
guard let newSharelink = URL(string: shareLink) else { return }
let components = DynamicLinkComponents.init(link: newSharelink, domain: "Your Domin From Genrated By Google Account(EX. = napu4u.app.goo.gl)")
let iOSParams = DynamicLinkIOSParameters(bundleID: "YourBundle ID")
iOSParams.appStoreID = "Your AppStore ID (Optional)"
components.iOSParameters = iOSParams
let options = DynamicLinkComponentsOptions()
options.pathLength = .short
components.options = options
components.shorten { (shortURL, warnings, error) in
if let error = error {
print(error.localizedDescription)
return
}
let shortLink = shortURL
print(shortLink)
}
Add this to to App Capabilities - Associated Domains and enter - applinks:yourdomain.com
In your ViewController add
guard let link = URL(string: "https://www.yourdomain.com/share_location.html?Id=\(RandomID)&uid=\(uid)") else { return }
let dynamicLinksDomain = "yourdomain.page.link"
let components = DynamicLinkComponents(link: link, domain: dynamicLinksDomain)
// [START shortLinkOptions]
let options = DynamicLinkComponentsOptions()
options.pathLength = .unguessable
components.options = options
// [END shortLinkOptions]
// [START shortenLink]
components.shorten { (shortURL, warnings, error) in
// Handle shortURL.
if let error = error {
print(error.localizedDescription)
return
}
print(shortURL?.absoluteString ?? "")
self.shortLink = shortURL
}
For one of my app, I wanted to share data to WhatsApp contacts. I tried few solutions overs the StackOverflow but couldn't get exact solution. After some trials could achieve what I was looking for, so sharing here for anyone's future reference.
var url = NSURL(string: "whatsapp://send?text=Hello%20Friends%2C%20Sharing%20some%20data%20here...%20!")
//Text which will be shared on WhatsApp is: "Hello Friends, Sharing some data here... !"
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().open(url as URL, options: [:]) { (success) in
if success {
print("WhatsApp accessed successfully")
} else {
print("Error accessing WhatsApp")
}
}
}
Note: text needs to be URL encoded. You can get it using any of the open source tools over internet or using addingPercentEncoding(withAllowedCharacters:) function in iOS.
e.g.
var urlString = "Hello Friends, Sharing some data here... !"
var urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
var url = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")
Swift 5
Code
let urlWhats = "whatsapp://send?text=\("Hello World")"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.open(whatsappURL as URL)
}
else {
print("please install watsapp")
}
}
}
Swift 3.0
Try with this code for access watsapp in your application. Its working perfectly for me.
#IBAction func sendButtonAction(_ sender: Any)
{
let date = Date()
let msg = "Hi my dear friends\(date)"
let urlWhats = "whatsapp://send?text=\(msg)"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.openURL(whatsappURL as URL)
} else {
print("please install watsapp")
}
}
}
}
Addition to above solutions, starting from iOS 9, we need to add whatsapp to LSApplicationQueriesSchemes key in info.plist. After this only it worked for me.
My code is Looking Like this
let encodeQuizStr = "Check Out The Quiz With link \n http://www.proprofs.com "
let urlQuizStringEncoded = encodeQuizStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
guard let whatsAppUrl = NSURL(string: "whatsapp://send?text="+urlQuizStringEncoded!) else { return }
if UIApplication.shared.canOpenURL(whatsAppUrl as URL) {
if #available(iOS 10.0, *) {
print(urlQuizStringEncoded!)
UIApplication.shared.open(whatsAppUrl as URL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(whatsAppUrl as URL)
}
}
else{
ProjectUtility.AlertWith(self, message: " What's App is Not Available.", Title: "Sorry")
}
working fine But When I put This URL
("http://www.proprofs.com/quiz-school/story.php?title=pq-find-out-which-ice-age-character-you-are ")
Then Its Not Working Please Check Thanks.HelpWill Be Appriciated.
Swift 5
Please follow the below steps for sharing on WhatsApp through URL Schemes
Add this code in your app "info.plist"
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
For sharing text and URL
Code
#IBAction func whatsappShareText(_ sender: AnyObject) {
let message = "First Whatsapp Share & https://www.google.co.in"
var queryCharSet = NSCharacterSet.urlQueryAllowed
// if your text message contains special characters like **+ and &** then add this line
queryCharSet.remove(charactersIn: "+&")
if let escapedString = message.addingPercentEncoding(withAllowedCharacters: queryCharSet) {
if let whatsappURL = URL(string: "whatsapp://send?text=\(escapedString)") {
if UIApplication.shared.canOpenURL(whatsappURL) {
UIApplication.shared.open(whatsappURL, options: [: ], completionHandler: nil)
} else {
debugPrint("please install WhatsApp")
}
}
}
}
Happy Coding!
As per their FAQ, you should be using the universal links instead:
https://wa.me/1234567890
Reference: https://faq.whatsapp.com/563219570998715/?locale=en_US