openURLSwift3'te kullanımdan kaldırıldı. openURL:options:completionHandler:Bir URL açmaya çalışırken değiştirmenin nasıl çalıştığına dair bazı örnekler verebilir misiniz?
openURLSwift3'te kullanımdan kaldırıldı. openURL:options:completionHandler:Bir URL açmaya çalışırken değiştirmenin nasıl çalıştığına dair bazı örnekler verebilir misiniz?
Yanıtlar:
Tum ihtiyacin olan sey:
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)
}
StringBunun yerine + operatörünü kullanmanız gerekirURL
Yukarıdaki cevap doğrudur, ancak sizi kontrol etmek canOpenUrlya da istememek istiyorsanız böyle deneyin.
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)")
})
}
Not: Tamamlamayı işlemek istemiyorsanız, bu şekilde de yazabilirsiniz.
UIApplication.shared.open(url, options: [:])
completionHandlerVarsayılan değer içerdiğinden yazmaya gerek yok nil, daha fazla ayrıntı için elma belgelerine bakın.
Uygulamadan çıkmak yerine uygulamanın içinde açmak isterseniz SafariServices'i içe aktarabilir ve çalışabilirsiniz.
import UIKit
import SafariServices
let url = URL(string: "https://www.google.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)
Swift 3 sürümü
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.
MacOS Sierra (v10.12.1) Xcode v8.1 Swift 3.0.1 kullanıyorum ve işte ViewController.swift'te benim için ne işe yaradı:
//
// 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.
}
};
import UIKit
import SafariServices
let url = URL(string: "https://sprotechs.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)