【问题标题】:How can I launch Safari from an iPhone app?如何从 iPhone 应用程序启动 Safari?
【发布时间】:2010-10-23 18:47:43
【问题描述】:

这可能是一个相当明显的问题,但是您可以从 iPhone 应用程序启动 Safari 浏览器吗?

【问题讨论】:

    标签: ios safari launch


    【解决方案1】:

    应该如下:

    NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
    
    if (![[UIApplication sharedApplication] openURL:url]) {
        NSLog(@"%@%@",@"Failed to open url:",[url description]);
    }
    

    【讨论】:

    • 这会计入您应用的内存使用量吗?另外,有没有什么好方法可以让你回到你的应用(比如社交网站的登录功能)?
    • @brendan 我的猜测是否定的,因为我假设 'webview' 是在 safari 应用程序中启动的,所以它属于该过程
    • 2009 年 5 月 9 日回答的重复
    • @Barett:不完全是,因为that's a 9/21/09 answer
    • IMO API 调用非常相似,因此这个答案本来可以更好地用作对先前答案的编辑或评论。
    【解决方案2】:

    UIApplication 有一个名为 openURL 的方法:

    示例:

    NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
    
    if (![[UIApplication sharedApplication] openURL:url]) {
      NSLog(@"%@%@",@"Failed to open url:",[url description]);
    }
    

    【讨论】:

      【解决方案3】:

      你可以在 safari 中打开网址:

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

      【讨论】:

        【解决方案4】:

        在 iOS 10 中,我们有一种不同的 completion handler 方法:

        目标C:

        NSDictionary *options = [NSDictionary new];
        //options can be empty
        NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
        [[UIApplication sharedApplication] openURL:url options:options completionHandler:^(BOOL success){
        }];
        

        斯威夫特:

        let url = URL(string: "http://www.stackoverflow.com")
        UIApplication.shared.open(url, options: [:]) { (success) in
        }
        

        【讨论】:

          【解决方案5】:

          也许有人可以使用 Swift 版本:

          在 Swift 2.2 中:

          UIApplication.sharedApplication().openURL(NSURL(string: "https://www.google.com")!)
          

          和 3.0:

          UIApplication.shared().openURL(URL(string: "https://www.google.com")!)
          

          【讨论】:

            【解决方案6】:

            在 swift 4 和 5 中,随着 OpenURL 的贬值,一个简单的方法就是

            if let url = URL(string: "https://stackoverflow.com") {
                UIApplication.shared.open(url, options: [:]) 
            }
            

            您也可以使用SafariServices。类似于应用中的 Safari 窗口。

            import SafariServices
            
            ...
            
            if let url = URL(string: "https://stackoverflow.com") {
                let safariViewController = SFSafariViewController(url: url)        
                self.present(safariViewController, animated: true)
            }
            
            

            【讨论】:

            • 虽然此代码 sn-p 可以解决问题,但 including an explanation 有助于提高您的回复质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
            【解决方案7】:

            在 Swift 3.0 中,你可以使用这个类来帮助你进行交流。框架维护者已弃用或删除了以前的答案。

            导入 UIKit
            
            类 InterAppCommunication {
                静态函数 openURI(_ URI: String) {
                    UIApplication.shared.open(URL(string: URI)!, options: [:], completionHandler: { (succ: Bool) in print("Complete! Success? \(succ)") })
                }
            }

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-02-08
              • 2010-11-10
              • 2011-07-22
              • 2023-03-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多