Abra o Safari na minha Extensão Hoje (widget) no meu aplicativo
Eu tenho uma extensão Today com um campo de texto. Quero usar o conteúdo do campo de texto como um URL para abrir um navegador no meu aplicativo.
Este é o meu TodayViewController.swift para o meu 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")
}
})
}