i need to open a link in safari browser but i have doubt, which method should i use ? openURL/open or canOpenURL.
Can anyone please help me to explain what actual difference between both function?
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: urlStr)!, options: [:], completionHandler: nil)
UIApplication.shared.canOpenURL(URL(string: urlStr)!)
} else {
UIApplication.shared.openURL(URL(string: urlStr)!) //introduced: 2.0, deprecated: 10.0,
UIApplication.shared.canOpenURL(URL(string: urlStr)!) // available(iOS 3.0, *)
}
canOpenURL(_:)
Returns a Boolean value indicating whether or not the URL’s scheme can be handled by some app installed on the device.
openURL(_:)
Attempts to open the resource at the specified URL.
openURL(_:) Deprecated - iOS 10.0
Use the open(_:options:completionHandler:) method instead.
Example:
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
})
} else {
UIApplication.shared.openURL(url)
}
}
If your app is linked on or after iOS 9.0, you must declare the URL
schemes you pass to this method by adding the
LSApplicationQueriesSchemes key to your app's Info.plist file. This
method always returns false for undeclared schemes, whether or not an
appropriate app is installed.
canOpenURL : It returns the bool, whther the url can be opened or not.
Example:
func schemeAvailable(scheme: String) -> Bool {
if let url = URL(string: scheme) {
return UIApplication.shared.canOpenURL(url)
}
return false
}
openURL : It opens the url.
As it is deprecated from ios 10. so new func is openURL:options:completionHandler:
Example
func open(scheme: String) {
if let url = URL(string: scheme) {
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
print("Open \(scheme): \(success)")
})
}
}
Related
i need to open an external app from my app, my backend give me data like com.whatsapp or com.facebook.Facebook etc .
when I look for this on the internet , it suggests answers using url scheme like whatsapp:// .. but it is not what I need.
this is my code
func openApp(app : String) {
if let url = URL(string: app),
UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler:nil)
} else {
UIApplication.shared.openURL(url)
}
} else {
customAlert(title: "error", message: "Failed to open \(app)")
}
}
i want to go to a url by clicking on button. I tried using 'UISharedapplication'and also through the method below mentioned but none works. Please help.
Thanks.
#IBAction func Displayurl(_ sender: Any) {
UIApplication.shared.canOpenURL(NSURL (string: "http://www.apple.com")! as URL)
}
The issue is that UIApplication's canOpenURL() method simply returns whether a URL can be opened, and does not actually open the URL. Once you've determined whether the URL can be opened (by calling canOpenURL(), as you have done), you must then call open() on the shared UIApplication instance to actually open the URL. This is demonstrated below:
if let url = URL(string: "http://www.apple.com") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
}
open() also takes an optional completionHandler argument with a single success parameter that you can choose to implement to determine if the URL was successfully opened.
canOpenURL(_:) method is used whether there is an installed app that can handle the url scheme. To open the resource of the specified URL use the open(_:options:completionHandler:) method. As for example
if let url = URL(string: "apple.com") {
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
For more info check the documentation here https://developer.apple.com/documentation/uikit/uiapplication/1622961-openurl
I'm trying to use:
UIApplication.shared.open(URL(string:"App-Prefs:root=NOTIFICATIONS_ID")!, options: [:], completionHandler: nil)
But it's just sending me to the Settings app, not to the Notification tab.
I have tried this without luck.
Any solution?
Apple will Reject your app submission. Because this is the private api. Use the below function in the below. ref: How do I open phone settings when a button is clicked?
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
}
After had set the scheme of queries to do ininfo.plist file:
<key>LSApplicationQueriesScheme</key>
<array>
<string>telprompt</string>
<string>fb-messenger</string>
<string>tel</string>
</array>
I would like to open the phone prompt in this way:
'let url = URL(string: "telprompt://\(self.offertaObj.callNumber)")
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!)
}else{
self.view.makeToast("Contattaci al numero: "+self.offertaObj.callNumber, duration: 3, position: .bottom)
}
I will receive always the message that the app isn't allowed to open that query scheme, meanwhile I didn't found any reference about how to ask to the user to allow it with toast messages.
Now in ios , schema has been change for perform call from application. Refer below code for perform call.
guard let url: URL = URL(string: "tel://" + "\(phoneNumber)") else {
print("Error in Making Call")
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// Fallback on earlier versions
UIApplication.shared.openURL(url)
}
Hello I've button action for call number , but when I used it don't call and nothing shows.
My codes under below.
#IBAction func callPhone(sender: AnyObject) {
UIApplication.shared().canOpenURL((NSURL(string: "tel://1234567890")! as URL))
}
Thank You !
Proper Swift 3.0 Code
if let url = URL(string: "tel://\(phoneNumber)") {
UIApplication.shared().open(url, options: [:], completionHandler: nil)
}
In Swift 3.0 NSURL have changed to URL. And sharedApplciation changed to shared. Also OpenURL changed to open, they have added a bunch other parameters to the openmethod, you can pass empty dictionary in options and nil in the completionHandler.
Please try following code it's use to solve your problem.
if let url = NSURL(string: "tel://\(1234567890)") {
UIApplication.sharedApplication().openURL(url)
}
Try this answer.
#IBAction func callPhone(sender: AnyObject) {
if let url = NSURL(string: "tel://9069118117") {
UIApplication.sharedApplication().openURL(url)
}
}
please note that:
tel:// try to call direct the phone number;
telprompt:// shows you an alert to confirm call
as of iOS 10 openUrl is deprecated;
#available(iOS, introduced: 2.0, deprecated: 10.0, message: "Please use openURL:options:completionHandler: instead")
open func openURL(_ url: URL) -> Bool
so i advice to use this code block to support also iOS < 9:
if #available(iOS 10, *) {
UIApplication.shared.open(yourURL)
// if you need completionHandler:
//UIApplication.shared.open(yourURL, completionHandler: { (aBool) in })
// if you need options too:
//UIApplication.shared.open(yourURL, options: [:], completionHandler: { (aBool) in })
} else {
UIApplication.shared.openURL(number)
}
Latest Xcode , Latest Swift working codes.
use telprompt:// not tel
let myphone = "+134345345345"
if let phone = URL(string:"telprompt://\(myphone)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}