OpenURL in iOS10
Asked Answered
T

8

47

So apparently OpenURL has been deprecated in iOS 10. Does anyone have any documentation on why or can explain what to do next? I looked on the Apple site already and found a few things pertaining to OpenURL and this is what they say to use now:

UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?)

Does anyone have any evidence that this is the new way to use OpenURL in Swift 3.0? In addition what values are to be used in the options: and completionHandler: parameters respectively?

Thoron answered 15/8, 2016 at 23:1 Comment(2)
@LeoDabus that is saying "open is unavailable"Strive
@TomRoggero https://mcmap.net/q/121377/-swift-3-open-link/…Lappet
P
63

Swift 3+:

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

Usage:

open(scheme: "tweetbot://timeline")

Source

Preter answered 30/9, 2016 at 10:35 Comment(3)
This sample is just for Swift 3.0. It's great!Squire
Thanks, I've adapted this and am now using it.Tonguelashing
use this let success = UIApplication.shared().openURL(url) instead of this let success = UIApplication.shared.openURL(url)Dough
S
44

A quick fix:

// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];

// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)

A complete answer:

http://useyourloaf.com/blog/openurl-deprecated-in-ios10/

Credits: Keith Harrison (useyourloaf.com)

Shamrock answered 22/10, 2016 at 4:56 Comment(0)
W
13

An empty options dictionary will result in the same behaviour as openUrl.

Otherwise:

+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| UIApplicationOpenURLOptionsSourceApplicationKey | NSString containing the bundle ID of the originating application                                                                             |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| UIApplicationOpenURLOptionsAnnotationKey        | property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| UIApplicationOpenURLOptionsOpenInPlaceKey       | bool NSNumber, set to YES if the file needs to be copied before use                                                                          |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+

From UIApplication.h

// 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.
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion NS_AVAILABLE_IOS(10_0) NS_EXTENSION_UNAVAILABLE_IOS("");

UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsSourceApplicationKey NS_SWIFT_NAME(sourceApplication) NS_AVAILABLE_IOS(9_0);   // value is an NSString containing the bundle ID of the originating application
UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsAnnotationKey NS_SWIFT_NAME(annotation) NS_AVAILABLE_IOS(9_0);   // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property
UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsOpenInPlaceKey NS_SWIFT_NAME(openInPlace) NS_AVAILABLE_IOS(9_0);   // value is a bool NSNumber, set to YES if the file needs to be copied before use
Whoever answered 12/9, 2016 at 9:25 Comment(0)
S
5

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

Separate answered 16/8, 2016 at 0:18 Comment(3)
That kind of answers the question but not entirely. is openURL:options:completionHandler: a new function that we are supposed to use or is that what I was suggesting in my question? where you still use UIApplication.shared()...Thoron
From what I read, it definitely suggests that the openURL:options:com‌​pletionHandler: function is a replacement for openUrl. Not all the documentation has been updated yet, and some still point to deprecated code.Separate
this is the link you wanted to post: developer.apple.com/library/prerelease/content/releasenotes/…Caerleon
A
3

You need to do some checking first before loading the url. Please check the codes below.

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"https://www.gmail.com"]]){
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.gmail.com"] options:@{} completionHandler:^(BOOL success) {
                                //completion codes here
                            }];
}

I hope this helps.

Allrud answered 19/7, 2017 at 2:11 Comment(0)
A
1

If your app still support iOS 9 or lower, just keep using the old openURL. You should only move to the new one if your Deployment Target is iOS 10.

Abloom answered 4/10, 2016 at 18:23 Comment(0)
B
1

let actual:

[String: AnyObject] = ["xxx key": "xxx value" as AnyObject, "yyy key": "yyy value" as AnyObject]

UIApplication.shared.open(URL(string: "http:google.com")!, options: actual, completionHandler: {(true) -> Swift.Void in
print("Refresh")
})

Where xxx and yyy are any string you want to print or leave them blank .

Biological answered 23/1, 2017 at 14:9 Comment(0)
C
0

you can use function to open settings:

func showAlert(title:String, message:String){
        let alert = UIAlertController(title: title,
                                      message: message,
                                      preferredStyle: UIAlertController.Style.alert)

        let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alert.addAction(okAction)

        let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { _ in
            // Take the user to Settings app to possibly change permission.
            guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return }
            if UIApplication.shared.canOpenURL(settingsUrl) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                        // Finished opening URL
                    })
                } else {
                    // Fallback on earlier versions
                    UIApplication.shared.openURL(settingsUrl)
                }
            }
        })
        alert.addAction(settingsAction)
        present(alert, animated: true, completion: nil)
    }

Call this function like this given below

showAlert(title: "Unable to access the Photos", message: "To enable access, go to Settings > Privacy > Photos and turn on Photos access for this app.")
Carlos answered 3/12, 2018 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.