openURL:在 iOS 10 中已弃用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39548010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:12:07  来源:igfitidea点击:

openURL: deprecated in iOS 10

iosobjective-cnsurlios10

提问by Joannes

Apple with iOS 10 has deprecated openURL: for openURL:option:completionHandler If I have:

带有 iOS 10 的 Apple 已弃用 openURL: for openURL:option:completionHandler 如果我有:

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];

How it will become? options:<#(nonnull NSDictionary *)#> in detail

会变成怎样?选项:<#(nonnull NSDictionary *)#> 详细说明

[[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/

更新选项:@{} 对于没有键和值的空字典 http://useyourloaf.com/blog/querying-url-schemes-with-canopenurl/

回答by Nirav D

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

不处理completionHandler

[application openURL:URL options:@{} completionHandler:nil];

回答by zeeshan Dar

// Objective-C

// 目标-C

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

// Swift

// 斯威夫特

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

回答by Disha

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 openURLfor openURL:options:completionHandler:.

Apple 在openURL:iOS 2 中引入了该方法作为打开外部链接的一种方式。相关功能canOpenURL:在 iOS 9 中获得了一些隐私控制,以阻止您查询设备中已安装的应用程序。现在与iOS苹果10已否决普通的旧openURLopenURL:options:completionHandler:

Here is my quick guide to what you need to know to open an external link with iOS 10.

这是我的快速指南,介绍在 iOS 10 中打开外部链接需要了解的内容。

The now deprecated method has a single parameter for the URL to open and returns a boolean to report success or failure:

现在已弃用的方法有一个用于打开 URL 的参数,并返回一个布尔值来报告成功或失败:

// Objective-C
    - (BOOL)openURL:(NSURL*)url

// Swift
    open func canOpenURL(_ url: URL) -> Bool

The new method in iOS 10:

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.
  • 要打开的网址
  • 选项字典(有关有效条目,请参见下文)。使用空字典实现与 openURL:.
  • 成功调用主队列的完成处理程序。如果您对状态不感兴趣,则可为 Null。

Opening a URL with iOS 10

使用 iOS 10 打开 URL

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:

如果您只有 iOS 10 的应用程序,不关心选项和完成状态,只想停止 Xcode 抱怨,这意味着什么:

// 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 openURLmethod. Let's look at an example where do that and also use the completion handler to check the status of the open:

在实践中,只要您仍然支持 iOS 9 或更早版本,您就会希望回退到普通的旧openURL方法。让我们看一个例子,在那里这样做,并使用完成处理程序来检查打开的状态:

First with Objective-C:

首先是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 UIApplicationheader file lists a single key for the options dictionary:

选项
UIApplication头文件列出了选择一个密钥词典:

  • 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).
  • UIApplicationOpenURLOptionUniversalLinksOnly:使用设置为 true (YES) 的布尔值仅在 URL 是有效的通用链接且应用程序配置为打开它时才打开它。如果没有配置应用程序或用户禁用它来打开链接,则完成处理程序将使用 false (NO) 调用。

To override the default behaviour create a dictionary with the key set to true (YES) and pass it as the options parameter:

要覆盖默认行为,请创建一个键设置为 true (YES) 的字典,并将其作为 options 参数传递:

// 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/kharrisonit will fail if I do not have the Twitter app installed instead of opening the link in Safari.

因此,例如,如果我将此设置为 true 并尝试打开 URL https://twitter.com/kharrison,如果我没有安装 Twitter 应用程序而不是在 Safari 中打开链接,它将失败。

Refrence: openURL: deprecated in iOS 10

参考openURL:在 iOS 10 中已弃用

回答by Dnyaneshwar Shinde

 // 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");
        }
    }];

回答by Vivek

Open Application Setting (Objective-c)

开放申请设置(Objective-c)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]
                                           options:@{}
                                 completionHandler:^(BOOL success) {
        }];
  • Tested in iOS 12
  • 在 iOS 12 中测试

回答by vikas kumar

Swift 5.0.1 and above

Swift 5.0.1 及以上

UIApplication.shared.open(URL.init(string: UIApplication.openSettingsURLString)!)