Abrir Safari desde mi extensión Today (widget) dentro de mi aplicación
Tengo una extensión Today con un campo de texto. Quiero usar el contenido del campo de texto como una URL para abrir un navegador dentro de mi aplicación.
Este es mi TodayViewController.swift para mi widget
import UIKit
import SafariServices
import NotificationCenter
// This extension to remove the white spaces from what pasteed
extension String {
func replace(string:String, replacement:String) -> String {
return self.replacingOccurrences(of: string, with: replacement,
options: NSString.CompareOptions.literal, range: nil)
}
func removeWhitespace() -> String {
return self.replace(string: " ", replacement: "")
}
}
class TodayViewController: UIViewController, NCWidgetProviding {
var clearNumber: String?
override func viewDidLoad() {
super.viewDidLoad()
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
completionHandler(NCUpdateResult.newData)
}
@IBOutlet weak var textBox: UITextField!
@IBAction func clearNumber(_ sender: Any) {
if textBox.hasText == true {
textBox.text = ""
}else{
return
}
}
@IBAction func pasteNumber(_ sender: Any) {
if let myString = UIPasteboard.general.string {
let pasteNumber = myString.removeWhitespace()
textBox.insertText(pasteNumber)
}else{
return
}
}
@IBAction func goButton(_ sender: Any) {
let myAppUrl = URL(string: "main-screen:")!
extensionContext?.open(myAppUrl, completionHandler: { (success) in
if (!success) {
print("error: failed to open app from Today Extension")
}
})
}