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)
}
Related
I want to intent a call , this is my code :
if let urlMobile = NSURL(string: "tel://076938483"), UIApplication.shared.canOpenURL(urlMobile as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(urlMobile as URL, options: [:], completionHandler: nil)
}
else {
UIApplication.shared.openURL(urlMobile as URL)
}
}
I'm using swift 3 to do so but I get this error:
-canOpenURL: failed for URL: "tel://09178883828" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
any idea to do so ?
Your code works perfectly fine. Run it on an actual device if your using Simulator. You can't simulate a call on a Mac/MacBook.
Please have a look at Simulator Hardware Actions in Apple documentation.
Some LSApplicationQueriesScheme do not work on Simulator. Error Code -10814 is for kLSApplicationNotFoundErr. Simulator can't launch Dial Pad for Telephone. So run it on iPhone device.
This worked for me!!!
Code Should be
if let url = NSURL(string: "tel://\(yourNumber)"), UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
the url should be:
if let urlMobile = NSURL(string: "tel:///076938483"), UIApplication.shared.canOpenURL(urlMobile as URL) {
let phonenumber = "076938483"
guard let url = URL(string: "tel://\(phonenumber )") else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
In the else part of the URL generation, print anything, if it is getting printed, you need to check the format of the phone number.
#IBAction func Call(_ sender: Any) {
let busPhone = "7355535586"
if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
#objc func callBtn() {
let userPhone = String((phoneNum.filter {!" \n\t\r".contains($0)}))
if let url = URL(string: "tel://\(phoneNum)"), UIApplication.shared.canOpenURL(url) {
DispatchQueue.main.async {
UIApplication.shared.open(url)
}
}
}
Note: This code can work when you run with the real device not simulator. And don't forget add LSApplicationQueriesSchemes in your info.plist.
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.
It seems I can't open the second app using my method. Nothing happened. Is there any silly mistakes here?
My second app .plist file
My first app code
#IBAction func btnCRM(sender: AnyObject) {
var customURL: NSString = "CRM://"
if (UIApplication.sharedApplication().canOpenURL(NSURL(fileURLWithPath: customURL as String)!)){
UIApplication.sharedApplication().openURL(NSURL(fileURLWithPath: customURL as String)!)
}
}
In addition to the URL Schemes under Item 0, you need to add URL identifier which is CFBundleURLName, as outlined here.
try this code:
let url = NSURL(string: "CRM://")
if (UIApplication.sharedApplication().canOpenURL(url!)) {
UIApplication.sharedApplication().openURL(url!)
}
'openURL' was deprecated in iOS 10.0
Updated version:
guard let url = URL(string: "CRM://"), UIApplication.shared.canOpenURL(url) else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Swift 5.7 2023
The code below opens the main application
private func openMainApp() {
self.extensionContext?.completeRequest(returningItems: nil, completionHandler: { _ in
guard let url = URL(string: self.appURL) else {
return
}
_ = self.openURL(url)
})
}
// Courtesy: https://stackoverflow.com/a/44499222/13363449 👇🏾
// Function must be named exactly like this so a selector can be found by the compiler!
// Anyway - it's another selector in another instance that would be "performed" instead.
#objc private func openURL(_ url: URL) -> Bool {
var responder: UIResponder? = self
while responder != nil {
if let application = responder as? UIApplication {
return application.perform(#selector(openURL(_:)), with: url) != nil
}
responder = responder?.next
}
return false
}
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)
I am creating an app, and I have a banner which promotes my other app. This is my code:
var barsButton : UIButton = UIButton(frame: CGRectMake((self.view.bounds.width / 2) - 51, self.view.bounds.height - 100, 102, 30))
barsButton.setImage(UIImage(named: "Bars Icon 2.png"), forState: .Normal)
barsButton.addTarget(self, action: "openBarsLink", forControlEvents: UIControlEvents.TouchUpInside)
func openBarsLink() {
var barsLink : String = "itms-apps:https://itunes.apple.com/app/bars/id706081574?mt=8"
UIApplication.sharedApplication().openURL(NSURL.URLWithString(barsLink))
}
However, when the user presses the button, it just takes them to the App Store, and not the specific page for my app. What am I doing wrong?
You have too many protocols in your URL. Get rid of https: so the URL reads
itms-apps://itunes.apple.com/app/bars/id706081574
Just by following older answers I couldn't make it work, so here I post my complete solution:
if let url = NSURL(string: "itms-apps://itunes.apple.com/app/id1234567890"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}
Use just the short "itms://".
For Swift 3 this is the snippet:
UIApplication.shared.openURL(URL(string: "itms://itunes.apple.com/app/id" + appStoreAppID)!)
I hope this helps someone.
Cheers.
P.S. #Eric Aya was ahead of the time :)
I had this problem but this code just works on the phone not simulator. So check this code:
if let url = URL(string: "itms-apps://itunes.apple.com/app/id" + APP_ID ),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.openURL(url)
}else{
//Just check it on phone not simulator!
print("Can not open")
}
As openURL is deprecated from iOS 10 use below code:
UIApplication.shared.open((URL(string: "itms://itunes.apple.com/app/" + appStoreAppID)!), options:[:], completionHandler: nil)
Simply you can use these functions in a utility struct to goto app page in app store also you can goto rate app view directly:
static func gotoApp(appID: String, completion: ((_ success: Bool)->())? = nil) {
let appUrl = "itms-apps://itunes.apple.com/app/id\(appID)"
gotoURL(string: appUrl, completion: completion)
}
static func rateApp(appId: String, completion: ((_ success: Bool)->())? = nil) {
//let appUrl = "itms-apps://itunes.apple.com/app/" + appId
let appUrl = "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appId)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"
//TODO: use &action=write-review for opening review directly
print("app review URL: ", appUrl)
gotoURL(string: appUrl, completion: completion)
}
static func gotoURL(string: String, completion: ((_ success: Bool)->())? = nil) {
print("gotoURL: ", string)
guard let url = URL(string: string) else {
print("gotoURL: invalid url", string)
completion?(false)
return
}
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: completion)
} else {
completion?(UIApplication.shared.openURL(url))
}
}
Swift 3 - XCode 8.2.1
UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id" + appStoreAppID)!)
Link you are trying to open is not valid - remove https: schema from it (or itms: - but I suggest first option, to avoid redirects)
I use this and it works.
let locale: String = Locale.current.regionCode ?? "US"
UIApplication.shared.open(URL(string: "https://apps.apple.com/\(locale)/developer/{developer-name}/{idXXXXXXXXXX}")!, options: [:], completionHandler: nil)