如何在Swift3中打开URL


149

openURL已在Swift3中弃用。任何人都可以提供一些示例来说明openURL:options:completionHandler:尝试打开URL时替换的工作方式吗?

Answers:


385

所有你需要的是:

guard let url = URL(string: "http://www.google.com") else {
  return //be safe
}

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}

如果我在网址中使用'+'运算符怎么办?例如:“ xxxxx.com./… ”就是这样。该字符串给了我一个错误“没有'+'候选者会产生预期的上下文结果类型'URL”
Ibrahim BOLAT

你必须使用+操作上的String,而不是上URL
Devran科斯莫Uenal

旁注:请勿尝试执行以下操作:UIApplication.shared.openURL(URL(string:“在此处插入url”)!)。XCode 8上的编译器会感到困惑,并且无法正确构建。因此,请按原样使用此解决方案。很棒!谢谢。
乔尔(Joel)

在不实际打开Safari的情况下如何打开URL?如何获得在后台“打开”的网址?请在以下位置回答我的问题:stackoverflow.com/questions/43686252/…
Christian Kreiter '17

1
您是说Swift不会让您爬墙以完成打开URL这样复杂的事情吗?[下颌跌落]
Daniel Springer

36

上面的答案是正确的,但是如果您想检查一下canOpenUrl还是不要这样尝试。

let url = URL(string: "http://www.facebook.com")!
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    //If you want handle the completion block than 
    UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
         print("Open url : \(success)")
    })
}

注意:如果您不想处理完成,也可以这样编写。

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

无需编写,completionHandler因为它包含默认值nil,有关更多详细信息,请查阅Apple文档


28

如果您想在应用程序内部打开而不是离开应用程序,则可以导入SafariServices并将其解决。

import UIKit
import SafariServices

let url = URL(string: "https://www.google.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)

1
根据iOS指南,此方法是最佳做法
gtrujillos

8

Swift 3版本

import UIKit

protocol PhoneCalling {
    func call(phoneNumber: String)
}

extension PhoneCalling {
    func call(phoneNumber: String) {
        let cleanNumber = phoneNumber.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "-", with: "")
        guard let number = URL(string: "telprompt://" + cleanNumber) else { return }

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

您可以在中使用正则表达式replacingOccurrences
苏珊(Sulthan)

2

我正在使用macOS Sierra(v10.12.1)Xcode v8.1 Swift 3.0.1,这是在ViewController.swift中对我有用的东西:

//
//  ViewController.swift
//  UIWebViewExample
//
//  Created by Scott Maretick on 1/2/17.
//  Copyright © 2017 Scott Maretick. All rights reserved.
//

import UIKit
import WebKit

class ViewController: UIViewController {

    //added this code
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Your webView code goes here
        let url = URL(string: "https://www.google.com")
        if UIApplication.shared.canOpenURL(url!) {
            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
            //If you want handle the completion block than
            UIApplication.shared.open(url!, options: [:], completionHandler: { (success) in
                print("Open url : \(success)")
            })
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


};

2
import UIKit 
import SafariServices 

let url = URL(string: "https://sprotechs.com")
let vc = SFSafariViewController(url: url!) 
present(vc, animated: true, completion: nil)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.