openURLha quedado en desuso en Swift3. ¿Alguien puede proporcionar algunos ejemplos de cómo funciona el reemplazo openURL:options:completionHandler:al intentar abrir una url?
openURLha quedado en desuso en Swift3. ¿Alguien puede proporcionar algunos ejemplos de cómo funciona el reemplazo openURL:options:completionHandler:al intentar abrir una url?
Respuestas:
Todo lo que necesitas es:
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)
}
Stringlugar en elURL
La respuesta anterior es correcta, pero si desea comprobarlo canOpenUrlo no, intente así.
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)")
})
}
Nota: Si no desea manejar la finalización, también puede escribir así.
UIApplication.shared.open(url, options: [:])
No es necesario escribir, completionHandlerya que contiene el valor predeterminado nil, consulte la documentación de Apple para obtener más detalles.
Si desea abrir dentro de la aplicación en lugar de salir de la aplicación, puede importar SafariServices y resolverlo.
import UIKit
import SafariServices
let url = URL(string: "https://www.google.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)
Versión 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.
Estoy usando macOS Sierra (v10.12.1) Xcode v8.1 Swift 3.0.1 y esto es lo que funcionó para mí en 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.
}
};
import UIKit
import SafariServices
let url = URL(string: "https://sprotechs.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)