openURL: deprecated in iOS 10 - ios

Apple with iOS 10 has deprecated openURL: for openURL:option:completionHandler
If I have:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
How it will become? options:<#(nonnull NSDictionary<NSString *,id> *)#> in detail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"] options:<#(nonnull NSDictionary<NSString *,id> *)#> completionHandler:nil];
Thanks
Update
options:#{} For empty dictionary with no key and value
http://useyourloaf.com/blog/querying-url-schemes-with-canopenurl/

Write like this.
Handle completionHandler
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:#"http://www.google.com"];
[application openURL:URL options:#{} completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened url");
}
}];
Without handling completionHandler
[application openURL:URL options:#{} completionHandler:nil];
Swift Equivalent:- open(_:options:completionHandler:)
UIApplication.shared.open(url)

Apple introduced the openURL: method as a way to open external links with iOS 2. The related function canOpenURL: got some privacy controls in iOS 9 to stop you from querying devices for installed apps. Now with iOS 10 Apple has deprecated the plain old openURL for openURL:options:completionHandler:.
Here is my quick guide to what you need to know to open an external link with iOS 10.
The now deprecated method has a single parameter for the URL to open and returns a boolean to report success or failure:
// Objective-C
- (BOOL)openURL:(NSURL*)url
// Swift
open func canOpenURL(_ url: URL) -> Bool
The new method in iOS 10:
// Objective-C
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options
completionHandler:(void (^ __nullable)(BOOL success))completion
// Swift
open func open(_ url: URL, options: [String : Any] = [:],
completionHandler completion: (#escaping (Bool) -> Swift.Void)? = nil)
There are now three parameters:
The URL to open
An options dictionary (see below for valid
entries). Use an empty dictionary for the same behaviour as
openURL:.
A completion handler called on the main queue with the
success. Nullable if you are not interested in the status.
Opening a URL with iOS 10
What does this mean if you have an iOS 10 only app, don’t care about options and completion status and just want to stop Xcode complaining:
// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:#{} completionHandler:nil];
// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)
In practise as long as you are still supporting iOS 9 or earlier you will want to fallback to the plain old openURL method. Let’s look at an example where do that and also use the completion handler to check the status of the open:
First with Objective-C:
- (void)openScheme:(NSString *)scheme {
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
if ([application respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:#{}
completionHandler:^(BOOL success) {
NSLog(#"Open %#: %d",scheme,success);
}];
} else {
BOOL success = [application openURL:URL];
NSLog(#"Open %#: %d",scheme,success);
}
}
// Typical usage
[self openScheme:#"tweetbot://timeline"];
I am passing an empty dictionary for the options and I don’t do anything useful in the completion handler other than log the success. The Swift version:
func open(scheme: String) {
if let url = URL(string: scheme) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(scheme): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(scheme): \(success)")
}
}
}
// Typical usage
open(scheme: "tweetbot://timeline")
Options
The UIApplication header file lists a single key for the options dictionary:
UIApplicationOpenURLOptionUniversalLinksOnly: Use a boolean value set to true (YES) to only open the URL if it is a valid universal link with an application configured to open it. If there is no application configured or the user disabled using it to open the link the completion handler is called with false (NO).
To override the default behaviour create a dictionary with the key set to true (YES) and pass it as the options parameter:
// Objective-C
NSDictionary *options = #{UIApplicationOpenURLOptionUniversalLinksOnly : #YES};
[application openURL:URL options:options completionHandler:nil];
// Swift
let options = [UIApplicationOpenURLOptionUniversalLinksOnly : true]
UIApplication.shared.open(url, options: options, completionHandler: nil)
So for example if I set this to true and try to open the URL https://twitter.com/kharrison it will fail if I do not have the Twitter app installed instead of opening the link in Safari.
Refrence : openURL: deprecated in iOS 10

// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:#{} completionHandler:nil];
// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)

Swift 5.0.1 and above
UIApplication.shared.open(URL.init(string: UIApplication.openSettingsURLString)!)

// In Xcode 9 and iOS 11
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:#"http://facebook.com"];
[application openURL:URL options:#{} completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened url");
}
}];

Open Application Setting (Objective-c)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]
options:#{}
completionHandler:^(BOOL success) {
}];
Tested in iOS 12

Related

can you force ios to launch safari other than any other browser or even give an option?

Is there a way to launch safari only? I know in order to send an intent and have ios to handle it we can do [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://"]];, but if user has other browser installed (i.e. chrome), there's no guarantee safari will be used.
Reason I want to use safari is that I'm trying to have safari to handle certificate authentication for me, and according to here, only system app has permission to do so
try this
//initially we need to check safari is installed or not in our device
NSURL *url = [NSURL URLWithString:#"safari://"];
UIApplication *application = [UIApplication sharedApplication];
if ([application canOpenURL:url]) {
// if success again need to validate the our calling URL.
NSURL *linkURL = [NSURL URLWithString:#"https://iostree.wordpress.com/2017/07/29/launch-safari-from-ios-app/"];
if ([application canOpenURL:linkURL]) {
if ([application respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[application openURL:linkURL options:#{}
completionHandler:^(BOOL success) {
NSLog(#"success");
}];
}
}
}else{
NSLog(#"safari is not installed");
}

Open Setting app in iOS 10.1 via custom application

I am trying to open the Settings app from my own iOS app.
My code is in Objective-C.
UIApplication *app=[UIApplication sharedApplication];
NSURL *url=[NSURL URLWithString:UIApplicationOpenSettingsURLString];
NSDictionary *dict=[[NSDictionary alloc] initWithObjectsAndKeys:[[NSNumber alloc] initWithBool:YES],UIApplicationOpenURLOptionUniversalLinksOnly, nil];
[app openURL:url options:dict completionHandler:^(BOOL success) {
NSLog(#"in open Url");
}];
This open URL method is the new method given by Apple. What should I pass in the options dictionary?
As you can see in iOS SDK:
Options are specified in the section below for openURL options. An empty options dictionary will result in the same behavior as the older openURL call, aside from the fact that this is asynchronous and calls the completion handler rather than returning a result. The completion handler is called on the main queue.
So you can just send nil to get behaviour of old openURL: method.
Try this to open settings app
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]

Method openURL:options:completionHandler compatibility in objective c

I'm using the method openURL:options:completionHandler:, it turns out that in iOS 10 works fine, but I'm also interested in my app is compatible with the old iOS 9, but xcode gives me a
NSException:
-[UIApplication openURL:options:completionHandler:]:
Unrecognized selector send to instance
There any way make it work in iOS 9 also?
Thank for the possible response!
The new UIApplication method openURL:options:completionHandler:, which
is executed asynchronously and calls the specified completion handler
on the main queue (this method replaces openURL:)
This is under Additional Framework Changes > UIKit at: https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html
you need use it like this:-
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
New method in iOS 10:
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options
completionHandler:(void (^ __nullable)(BOOL success))completion
Read Doc here:
https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html
The new UIApplication method openURL:options:completionHandler:, which
is executed asynchronously and calls the specified completion handler
on the main queue (this method replaces openURL:).
For below iOS 10:
[[UIApplication sharedApplication] openURL:URL];//URL is NSURL
You can use below code:
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:strUrl];
if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
if ([application respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:#{}
completionHandler:^(BOOL success) {
NSLog(#"Open %#: %d",scheme,success);
}];
} else {
BOOL success = [application openURL:URL];
NSLog(#"Open %#: %d",scheme,success);
}
}
else{
bool can = [application canOpenURL:URL];
if(can){
[application openURL:URL];
}
}
Also need to set LSApplicationQueriesSchemes in plist if not set:
Like,
<key>LSApplicationQueriesSchemes</key>
<array>
<string>urlscheme1</string>
<string>urlscheme2</string>
</array>
Also read answer here: https://stackoverflow.com/a/40042291/5575752
You can also use it like as below for obj-c.
[[UIApplication sharedApplication] openURL: url options:#{} completionHandler:nil];

Using openScheme with POST

I have an iOS app that need to switch to Safari and open a link using a POST command. Since iOS 10 we are supposed to use 'openScheme', but where do a specify http method POST?
This is where I am so far;
- (void)openScheme:(NSString *)scheme
options:(NSDictionary *)options
{
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
[application openURL:URL options:options completionHandler:^(BOOL success) {
if (success) {
NSLog(#"Opened %#",scheme);
}
}];
}
You can achieve it by adding URLSchemes.
Below is the steps.
(i) Open your info tab in targets.
(ii) At the below, you will find URL Types option.
(iii)Fill data as shown in below image.
(iv) Now, open your Safari browser in iPhone/iPad, type YourApp:// and enter. This should open your app.
Hope this helps!

How to jump to system setting's location service on iOS10?

Before I asked this question I had try:
Use [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=Privacy&path=LOCATION"]];It's work fine on iOS8 and iOS9,but there is nothing happen on iOS10.
Use [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];It's work fine on iOS8 and iOS9,too.However,on iOS10,when the app jump to system setting, the system setting exit immediately.
Use [[UIApplication sharedApplication]openURL:url options:#{}completionHandler:nil];It's crashed on iOS8 and iOS9,also,exit immediately on iOS10.
The question is can our app jump to system setting on iOS10? If yes.How?And for [[UIApplication sharedApplication]openURL:url options:#{}completionHandler:nil];what's the optionsmeans?We must code something for the options?
For some time now, apps have only been permitted to open their own settings pane in the settings app. There have been various settings URLs that have worked in the past, but recently Apple has been rejecting apps that use these URLS.
You can open your own application's settings:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Or in Objective-C
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if (url != nil) {
[[UIApplication sharedApplication] openURL:url options:[NSDictionary new] completionHandler:nil];
}
If you are targeting version of iOS earlier than 10 then you may prefer to use the older, deprecated, but still functional method:
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if (url != nil) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] openURL:url];
#pragma clang diagnostic pop
}
Note:I use this method for a long time and everyting goes well,but today(2018-9-14),I had been rejected.
Here is my previous answer,do not use this method forever:
CGFloat systemVersion = [[UIDevice currentDevice].systemVersion floatValue];
if (systemVersion < 10) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"App-Prefs:root=Privacy&path=LOCATION"]];
}else{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"App-Prefs:root=Privacy&path=LOCATION"]
options:[NSDictionary dictionary]
completionHandler:nil];
}
Now I use this way:
if (#available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:[NSDictionary dictionary] completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
Note :- this solution will not be useful for ios10 onwards
Dont forget to add URL schemes :-
Go to Project settings --> Info --> URL Types --> Add New URL Schemes-->URL Schemes = prefs
after that Use this url :-
let settingUrl = URL(string: "App-Prefs:root=Privacy&path=LOCATION")
And open using :-
if #available(iOS 10.0, *) {
UIApplication.shared.open(settingUrl) {
(isOpen:Bool) in
if !isOpen {
debugPrint("Error opening:\(settingUrl.absoluteString)")
// show error
}
}
}else{
if UIApplication.shared.canOpenURL(settingUrl) {
UIApplication.shared.open(settingUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
}
}
Enjoy :)..this worked for me.
Thanks to this guy. I figure out this URL Scheme Prefs:root=Privacy&path=LOCATION is only available in Today Widget, but no use in containing app.
In Today Widget, you can try this:
[self.extensionContext openURL:[NSURL URLWithString:#"Prefs:root=Privacy&path=LOCATION"] completionHandler:nil];
More about system URL Schemes, you can see here.
This all I got. Hope it will help you.
You can also open your app's setting center by opening"App-Prefs:root=your app bundle id". This will be easy for user to change setting for your app.
This works for me. iOS7 ~ iOS11
But if you are uing the iOS11, you can only jump to the app's setting page 😥
NSURL *url1 = [NSURL URLWithString:#"App-Prefs:root=Privacy&path=LOCATION"];
NSURL *url2 = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if (#available(iOS 11.0, *)) {
if ([[UIApplication sharedApplication] canOpenURL:url2]){
[[UIApplication sharedApplication] openURL:url2 options:#{} completionHandler:nil];
}
} else {
if ([[UIApplication sharedApplication] canOpenURL:url1]){
if (#available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url1 options:#{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:url1];
}
}
}

Resources