Swift - open web page in app, button doesn't react - ios

I created a button to make a call from app:
#IBAction func callButton(sender: AnyObject) {
if (PhoneNumber != ""){
UIApplication.sharedApplication().openURL(NSURL(string: "telprompt://\(PhoneNumber)")!)
}
}
and it works perfectly fine. Strange thing happens when I want to open a web page. I use nearly exactly same code
#IBAction func openWeb(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string: "www.google.com")!)
}
But this time button doesn't react and nothing happens. Wherever I was looking for some information about opening web pages in safari from the app, the code was written exactly this way. Do you have any idea where the problem is?
Thanks in advance!

missing url scheme
UIApplication.shared.open(URL(string: "http://www.google.com")!, options: [:], completionHandler: nil)

Swift 4, safely unwrapped optional, check if canOpenURL
if let url = URL(string: "https://www.google.com"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}

Swift 3 Version
UIApplication.shared.openURL(NSURL(string: "http://google.com")! as URL)

Updated Swift 3.0 version as of iOS 10
let googleURL = NSURL(string: "www.google.com")! as URL
UIApplication.shared.open(googleURL, options: [:], completionHandler: nil)

Swift 4.2.1, iOS 10 and higher
UIApplication.shared.open(URL(string: "https://google.com")!, options: [:], completionHandler: nil)

Related

Is there a way to make a hidden call from an iOS app through the telprompt URL scheme?

I can make a phone call with this code:
if let url = URL(string: "telprompt://\(number)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Now I want to make a phone call with hidden phone number, so I would add #31# but then url is not, and the canOpenURL function is not even called.
if let url = URL(string: "telprompt://#31#\(number)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
tel:// scheme responds the same, and I don't want to go in the system settings and change it there, as it would hide my number on every call I make.
Any idea?
Xcode 11.5 - macOS Catalina - Swift 5
Have you tried url-encoding the # ?
"telprompt://%2331%23\(number)"

How to know particular shortcut is added in our shortcuts app or not using our iOS app?

I run the shortcuts with the x-callback url given below code:
let url = URL(string: "shortcuts://x-callback-url/run-shortcut?name=Airplane&x-success=shortcutsdemo://")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
When I open the URL in UIApplication.shared.open, it runs the shortcut and after its done, return back to our iOS app.
My question is if my shortcut is not added in shortcuts app, how I manage?
Is there any way to first find our shortcut is added or not in shortcuts app and then we run the shortcut.
Like :
if (Shortcut.isInstalled) {
let url = URL(string: "shortcuts://x-callback-url/run-shortcut?name=Airplane&x-success=shortcutsdemo://")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
print("Not installed")
}
Summary :
If the shortcut is added in our shortcuts app, Then it's run the shortcuts using our iOS app. Otherwise its not run. and give an error for that shortcut.
I think you will need to use func canOpenURL(_ url: URL) -> Bool
let url = URL(string: "shortcuts://x-callback-url/run-shortcut?name=Airplane&x-success=shortcutsdemo://")
if UIApplication.shared.canOpenURL(ur!l) {
print("Short uts is installed")
} else {
print("Shortcuts not installed")
}
I am not 100% sure, but I think it redirects you automatically to appstore if you don't have it, check that out.

Phone Call - Swift 3 with xCode 8.1

How can I to make a phone call with swift 3.0
I used the following code in Swift 3.0
UIApplication.shared.open(openURL, options: [:], completionHandler: nil)
But its not running on xCode 8.1
How can I make a call in swift 3.0?
Use this:
guard let url = URL(string: "telprompt://" + number) else { return }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
For popup use telprompt://\(number) and for direct call use tel://\(number)

Open URL from UIButton in CollectionViewCell

I have a UIButton in my UICollectionViewCell and it's getting data from JSON. Now I need to open a URL from each button (each button have a different url that also comes from JSON).
I managed to open the URL with:
let weburl = "http://example.com"
UIApplication.shared.openURL(URL(string: weburl)!)
But now I need to kinda pass an url to each button. Any ideas of how can i achieve this?
You can have an array of urls:
let urls = [url1, url2, ...]
And then assign the tag property of each button to the index of its corresponding url. Now you can easily manage what you want:
#IBAction func handleTouch(_ sender: UIButton) {
// assumes that the buttons' tags start at 0, which isn't a good idea.
// see #rmaddy comment bellow
let url = urls[sender.tag]
// use the version of the open method shown bellow because the other one becomes deprecated in iOS 10
UIApplication.shared.open(URL(string: url)!, options: [:], completionHandler: nil)
}
EDIT
Other solution would be to just store the url in the cell itself, and in the button handler open the url corresponding to its cell.
FYI openURL is deprecated in iOS 10. I suggest the following if you need to support older versions of ios:
let url = URL(string: "alexa://")!
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
guard success else {
//Error here
}
//Success here
})
} else {
if let success = UIApplication.shared.openURL(url) {
//Success here
} else {
//Error here
}
}
Otherwise just use UIApplication.shared.open. Also I would add a URL field to the data model you are passing to your tableViewCell and just look up the URL from the model.

Call phone action don't work in swift

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)
}

Resources