How to use openURL in iOS 10? - ios10

I'm working on iOS 10 app and since openURL is deprecated I need some help using the new method. Problem I'm facing is not knowing what to pass in the options parameter.
Here's my code:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"] options:nil completionHandler:nil];
Compiler gives warning: "Null passed to a callee that requires a non-null argument."
Confused what I should pass in...?

You should write it like this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"] options:#{} completionHandler:nil];

For Swift 3 you should use this:
UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?)
for example I used in my project:
let url = URL(string: "http://kaznews.kz")
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
this is simplest way without options and handler.

For iOS 10.2 and swift 3.1
private var urlString:String = "https://google.com"
#IBAction func openInSafari(sender: AnyObject) {
let url = NSURL(string: self.urlString)!
UIApplication.shared.open(url as URL, options: [ : ]) { (success) in
if success{
print("Its working fine")
}else{
print("You ran into problem")
}
}
}

Related

Can't construct URL for phone call in Swift

I'm trying to ask for phone call programmatically but I'm not able to construct URL from my nine-digit phone number. When I try it with for example 999999999 phone number, it works, it asks for call
#IBAction func callButtonPressed(_ sender: UIButton) {
askForCall(to: "999999999")
}
func askForCall(to number: String) {
guard let url = URL(string: "tel:\(number)"), UIApplication.shared.canOpenURL(url) else { return }
UIApplication.shared.open(url)
}
but when I use real phone number 736XXXXXX it shows nothing.
Note: when I try it without canOpenUrl it doesn’t work so I guess problem is with constructing URL from my real number
Any ideas?
You should type "tel://" + number and not tel:\(number)
EDIT 2
Try something like this
func call(phoneNumber: String) {
if let url = URL(string: phoneNumber) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(phoneNumber): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(phoneNumber): \(success)")
}
}
}
let number = "736XXXXXX"
let phoneNumber = "tel://\(number)"
call(phoneNumber: phoneNumber)
Try with that number to see if it's a bigger problem than the simple code :)
You need to add the scheme 'tel' into your info.plist
<key>LSApplicationQueriesSchemes</key>
<string>tel</string>
Then normal use:
guard let url = URL(string: "tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) else {return}
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
Goodluck

How to open your app's settings (inside the Settings app) with Swift (iOS 11)?

I would like to open my app's settings page inside the Settings app with Swift 4 in iOS 11. Just like the picture shows:
The following codes doesn't work, it will only open the Settings app:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
The app shows in the above picture is able to do this. So I wonder if there is any way to custom the URL Scheme to make it happen?
Oops, it seems it works in iOS 11.4.1:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Just an update because UIApplicationOpenSettingsURLString changed.
guard let url = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
You can open your app settings screen using it's bundle id, for example for CAMERA permission, you can use
if let bundleId = /* your app's bundle identifier */
let url = URL(string: "\(UIApplication.openSettingsURLString)&path=CAMERA/\(bundleId)"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Reference: https://stackoverflow.com/a/61383270/4439983

How to open your app in Settings iOS 11

It seems that Apple has moved a lot of the app configurations to the App path with iOS 11, how to open the app path programmatically in Settings? I tried "App-Prefs:root=\(Bundle.main.bundleIdentifier!)" but this doesn't seem to work.
Please note that my question is specific to: How to open the app path in settings: NOT how to open the settings
Here is the code you're looking for, I guess:
if let url = URL(string: UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
And in addition, the updated version for swift 5 :
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Swift 4.2, iOS 12
Opening just the settings is possible with the function below:
extension UIApplication {
...
#discardableResult
static func openAppSettings() -> Bool {
guard
let settingsURL = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(settingsURL)
else {
return false
}
UIApplication.shared.open(settingsURL)
return true
}
}
Usage: UIApplication.openAppSettings()
But be careful to NOT use "non-public URL scheme", such as: prefs:root= or App-Prefs:root, because otherwise your app will be rejected. This happened to me recently since I was trying to have a deeplink to the wifi section in the settings.
And if you want to make it work for both, older and newer iOS-versions, then do:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
openURL has been deprecated since iOS 10, so I would advise you to use:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: { success in
log.debug("Open app settings success: \(success)")
})
}
}
UIApplicationOpenSettingsURLString has been renamed to UIApplication.openSettingsURLString.
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
SWift 5
In some case we can not open App's setting after trying all the above. To solve this problem 100% just make sure these two step are followed
Step 1.
Right click on the project file -> Add New File -> Add Settings.Bundle in project and edit according to your requirements.
Step 2. Now add some code in your buttons action.
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Note: Using "prefs:Root" is forbidden by apple and your app will be rejected. So, avoid using this api.

How can I launch yandex maps application from external app in swift?

I have latitude and longitude information for source and destination, I want to send this information to yandex maps and yandex maps should show directions. How can I do this in Swift 3?
You should use Yandex's URL-Scheme (yandexmaps://build_route_on_map/?params)
Example in swift 3:
let url = URL(string: "yandexmaps://build_route_on_map/?lat_from=XXXXX&lon_from=XXXXX&lat_to=XXXXX&lon_to=XXXXX")!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
Remember to check if the app can be opened.
if UIApplication.shared.canOpenURL(url) {
// Open the URL here
}

Swift: NSData(contentsOfURL) crashing on XCode 6.1

Before upgrading to XCode6.1 I was using the method NSData.dataWithContents() and it was working perfectly all the images were downloading. Today I have updated to XCode 6.1 and it forces me to use the function like this:
NSData(contentsOfURL: NSURL(string: completeUrl)!)!
and when I run the application it crashes on it with message:
fatal error: unexpectedly found nil while unwrapping an Optional value
I have tried many things but nothing works. I am looking for any simple alternative for this to download images from a given URL.
Since the initalization of NSURL may fail due to several reasons you should better use it this way:
if let url = NSURL(string: completeUrl) {
if let data = NSData(contentsOfURL: url) { // may return nil, too
// do something with data
}
}
More better way to download files is:
let request:NSURLRequest = NSURLRequest(URL: NSURL(string: completeUrl)!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response:NSURLResponse!, imageData:NSData!, error:NSError!) -> Void in
var filePath:String = pathString + "/" + fileName
imageData.writeToFile(filePath, atomically: true)
})
It is working very nicely and also it gives you more control on the request.