gpt4 book ai didi

ios - 从我的应用程序中的 Today Extension(小部件)打开 Safari

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:17 24 4
gpt4 key购买 nike

我有一个带文本字段的 Today Extension。我想使用文本字段的内容作为 URL 在我的应用程序中打开浏览器。

这是我的小部件的 TodayViewController.swift

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")
}
})
}

最佳答案

您可以使用@Giuseppe_Lanza 解决方案并解析您从 Today Extension Widget 收到的 url。但是,我会展示一个示例,其中您的 url 具有静态组件并寻找诸如 https:/www.apple.com/homepodhttps:/www.apple.com 之类的路径/iphone 基于用户在 textField 中的输入:

1- URL 方案:myAppName

2- 添加这个以使用小部件打开您的应用

@IBAction func goButton(_ sender: Any) {
openApp(widgetText: "\(textBox.text!)")
}

func openApp(widgetText:String) {

let str = "myAppName://https://www.apple.com/\(widgetText)"
let url = URL(string: str)!
if textBox.hasText == true {

extensionContext?.open(url, completionHandler: { (success) in
if (!success) {
print("error: 🧐")
}
})
}
}

3- AppDelegate

定义一个变量并将接收到的 url 传递给 webViewController,将在那里解析 url。

open var receivedUrl:URL?

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool{

receivedUrl = url
//You need to alter this navigation to match your app requirement so that you get a reference to your previous view..
window?.rootViewController?.performSegue(withIdentifier: "toDeepLink", sender: nil)
}

Make sure to make add an identifier for this segue under the attributes inspector as toDeepLink.

4- WebView & 解析url现在你可以这样获取receivedUrl

    override func viewDidLoad() {
super.viewDidLoad()

let myAppDelegate = UIApplication.shared.delegate as! AppDelegate
print("receivedUrl \(myAppDelegate.receivedUrl!)")
//url Parsing & getting rid off urlScheme
let urlToLoad = URL(string: "\(myAppDelegate.receivedUrl!.host! + ":" + myAppDelegate.receivedUrl!.path)")!
print(urlToLoad)
let urlRequest = URLRequest(url: urlToLoad)
webView.load(urlRequest)
}

否则,您需要像字典一样以适当的方式解析它,以便为字典中的各个键分配动态值,从而为您的 url 或附加 "?" 到您的 urlToLoad 就在您尝试附加 url.query 之前,就像我在 webView Controller 中所做的那样。

关于ios - 从我的应用程序中的 Today Extension(小部件)打开 Safari,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48401758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com