openURL
在Swift3中已被棄用。任何人都可以提供一些替代openURL:options:completionHandler:
試圖打開一個網址時如何工作的例子嗎?如何在Swift3中打開一個URL
77
A
回答
233
所有你需要的是:
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)
}
19
以上的答案是正確的,但如果你想查詢你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 documentation更多細節。
6
斯威夫特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)
}
}
+0
您可以使用'replacesOccurrences'的正則表達式。 – Sulthan
2
我使用MacOS的塞拉利昂(v10.12.1)的Xcode V8.1斯威夫特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
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.
}
};
6
如果你想打開應用程序本身,而不是離開應用程序,你可以導入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. 如何從終端打開一個URL
- 2. 在新標籤中打開一個URL
- 3. 在applescript中打開一個url
- 4. 在eclipse中如何讓一個打開的url在
- 5. 如何在一個標籤中一個接一個地打開不同的url?
- 6. 如何打開一個URL並在Python中提取信息
- 7. 如何在另一個窗口中檢查打開的URL?
- 8. 如何在Django的後臺進程中打開一個url?
- 9. 如何打開一個URL在瀏覽器中#token
- 10. 如何在標籤中打印uiview(swift3)
- 11. 如何在JavaScript中的同一個窗口中打開一個URL?
- 12. 如何在android中打開RSS feed url?
- 13. 如何在div中打開url?
- 14. 如何在python中打開url
- 15. 如何在rails中打開URL?
- 16. 如何從settings.bundle在Safari中打開URL
- 17. 一個按鈕來打開一個URL
- 18. 從ibooks中打開一個url崩潰
- 19. 從servlet中打開一個URL
- 20. 打開一個URL,在jQuery的PHP
- 21. 在golang中打開一個url並閱讀重定向的url
- 22. 安全地通過一個URL通過URL在iframe中打開
- 23. Django url唯一url打開
- 24. 如何在使用intent時使用firefox focus打開一個url?
- 25. 微小的URL,從URL列表中隨機打開一個URL?
- 26. 如何在新窗口中打開一個URL不在新窗口中
- 27. 如何在android中使用uiautomation在chrome瀏覽器中打開一個url
- 28. Swift3 iOS - 如何搜索URL數組中的URL [URL?]
- 29. aframevr如何打開url
- 30. 使用Ant,如何打開一個URL與多個參數
如果我在我的網址中使用「+」運算符,該怎麼辦?例如:「https://xxxxx.com./xxxxxxxxxxxxxx="+userName+"xxxxxxx="+userPass+"&xxxxxxxxx」就是這樣。該字符串給了我一個錯誤「否'+'候選人產生預期的上下文結果類型'網址' –
你必須在'字符串'上使用+運算符而不是'URL' –
注意:不要嘗試執行這個:UIApplication.shared.openURL(URL(string:「insert url here」)!)。 XCode 8上的編譯器會感到困惑,無法正確構建。因此,請按原樣使用此解決方案。很棒!謝謝。 – Joel