Open WhatsApp conversation using Abid doesn't work - ios

I'm trying to open whatsapp conversation/chat for particular contact.
Instead of opening the desired chat it only opens the app.
No idea whats wrong :
let URLString = "whatsapp://send?abid=\(ID);text=lOL;"
UIApplication.sharedApplication().openURL(NSURL(string: URLString)!)
URLString value : whatsapp://send?abid=414;text=lOL
Any suggestions?

Update your URL like this:
whatsapp://send?abid=\(ID)&text=lOL
Source from HERE.

Try this and check if the UIApplication and open the URL.
let whatsAppURL: NSURL = NSURL(string: "whatsapp://send?abid=\(ID)&text=lOL")
if UIApplication.sharedApplication().canOpenURL(whatsAppURL){
UIApplication.sharedApplication().openURL(whatsAppURL)
}

Related

Can a URL from Firebase turn to enum Media? - Swift

In my Firebase database, I have a 'link' with some URL of a video as a value.
I'm trying to call it it via:
enum Media {
static let videoURL = "linkLabel.text = posts[selectedIndexPath].link"
}
Without success. If I call it just as a label text, it is fine. However, I also try the following:
static let videoURL = URL(string: "linkLabel.text = posts[selectedIndexPath].link")
As well as trying to call from my super.viewDidload() like:
videoVRView.load(from: URL(string: "\(linkLabel.text = posts[selectedIndexPath].link)"))
Anybody would know how to achieve this correctly? It will be amazing! Thanks!
----- EDIT:
I got it work by videoVRView.load(from: URL(string: "\(posts[selectedIndexPath].link)"))

OpenURL fails to open some very specific URL and shows error "Download Failed"

I want to open some URLs with Safari browser of apple device. And my openURL function works fine most of the time. However when I tried to open this URL, it failed with the following error.
Download Failed Safari cannot download file.
Here is the URL that fails:
https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1
Here is my code:
let url = "https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1"
if let nsurl = NSURL(string: url){
if UIApplication.sharedApplication().canOpenURL(nsurl){
UIApplication.sharedApplication().openURL(nsurl)
}else{
print("Cannot open this NSURL.")
}
}else{
print("Cannot convert String to NSURL.")
}
The URL you mentioned
https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1
contains a SWF file i.e. Shockwave Flash file.
Flash cannot played on an iOS based device (link).
1) Either you need to show appropriate error message to user.
2) Or you can redirect user to YouTube app (instead of Safari), where user would be able stream and watch the video.
See this Apple doc link to understand how to redirect user to YouTube app.
You need to encode URL before use it like:
let urlString = "https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1"
var escapedString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
if let nsurl = NSURL(string: escapedString){
if UIApplication.sharedApplication().canOpenURL(nsurl){
UIApplication.sharedApplication().openURL(nsurl)
}else{
print("Cannot open this NSURL.")
}
}else{
print("Cannot convert String to NSURL.")
}

Zattoo App url scheme

I need to open Zattoo app from my app (on a button click event)
what I have done is
let url:NSURL? = NSURL(string: "zattoo://")
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
} else {
print("App not installed")
//redirect to safari because the user doesn't have Zattoo App installed
UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/de/app/zattoo-tv-app-sports-news/id423779936?l=en")!)
}
canOpenURL() always returns me the false (even Zattoo app is installed on my device) hence the code in else is executed always.
but if removing the check and just executing the
let url:NSURL? = NSURL(string: "zattoo://")
UIApplication.sharedApplication().openURL(url!)
It's opening the the Zattoo app perfectly. Strange!
What I am doing wrong?
In iOS9 you should register custom schemes you want to use to open other apps. It should be stored as array of strings (custom schemes) with LSApplicationQueriesSchemes key in Info.plist file. From official documentation ( https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html#//apple_ref/doc/uid/TP40009250-SW14 ) :
LSApplicationQueriesSchemes (Array - iOS) Specifies the URL schemes
you want the app to be able to use with the canOpenURL: method of the
UIApplication class. For each URL scheme you want your app to use with
the canOpenURL: method, add it as a string in this array. Read the
canOpenURL: method description for important information about
declaring supported schemes and using that method
There is an example how it can be used: https://github.com/gatzsche/LSApplicationQueriesSchemes-Working-Example
Try this :-
if let url = NSURL(string: "zattoo://") {
let canOpen = UIApplication.sharedApplication().canOpenURL(url)
}
Hope it helps.

going to settings programmatically from local notification iOS 9

I wanna go to settings if the user clicks on "go to settings", can someone please help?
heres what the notification looks like
Try this:
if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(appSettings)
}

How to check if Link is broken with WKWebview?

I use WKWebview to load a URL.
let webView = WKWebview()
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
webView.loadRequest(request)
How can I detect if the link the webView should load is broken?
You can use canOpenUrl method:
UIApplication.sharedApplication().canOpenURL(url)
It will do the url validation and if the link is ok it returns true.
It's mostly use before you call:
UIApplication.sharedApplication().openURL(url)
to make sure this link can be open in safari but it should help you here too.
Make sure the link starts with http:// or https://.
Edited:
It will just check is the link is a correct url.
If you want to see the page is offline, authorisation issues, etc. you can implement WKNavigationDelegate protocol and check out this method:
- webView:didFailNavigation:withError:
this should give you more info.
It's always good idea to use: str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAl‌​lowedCharacterSet())!
it make sure that you don't pass a character which are not allowed in URL.
Edited 2:
To detect the status code you can try to implement:
- webView:decidePolicyForNavigationResponse:decisionHandler:
the navigation response is an NSURLResponse instance but
whenever you make an HTTP request, the NSURLResponse object you get back is actually an instance of the NSHTTPURLResponse class so you should cast it to NSHTTPURLResponse. That should give you a statusCode.
In the last line in the method you should call handler, for example decisionHandler(WKNavigationResponsePolicyAllow).
Ref: Answer exists here
if let url = NSURL(string: yourUrlString) {
var canOpen = UIApplication.sharedApplication().canOpenURL(url)
}
If you want to check if url string is correct and valid - just create NSURL object, if path contains error it will cast to nil:
let string = "http://google.com"
let url = NSURL(string: string)
let brokenString = "http:\\brokenurl.12"
let brokenUrl = NSURL(string: brokenString) // nil - it's not valid!
If you have implemented NSURLConnectionDelegate then below solution can be used.
didFailWithError method of NSURLConnectionDelegate can be used for this.
This method get called if an error occurs during the loading of a resource.
Can refer to below link
https://developer.apple.com/documentation/foundation/nsurlconnectiondelegate/1418443-connection

Resources