ios - Siri Shortcut Intent API 调用说 URL 是 nil,但它不是
问题描述
这是我正在处理的代码:
let url = "https://www.host.com/url/\(intent.cardInfo!)"
print(url)
let url2 = URL(string: url)! // this is nil??
let request = NSMutableURLRequest(url: url2)
下图显示此值不为零(您可以在控制台中看到)。我在 Siri 意图之后得到了价值。这是 IntentHandler 的内部。所有的代码都在下面。
class SoldForAppIntentHandler : NSObject, SoldForAppIntentHandling {
func handle(intent: SoldForAppIntent, completion: @escaping (SoldForAppIntentResponse) -> Void) {
print(intent.sport!)
print(intent.cardInfo!)
print(intent.cardNumber!)
let url = "https://www.host.com/url/\(intent.cardInfo!)"
print(url)
let url2 = URL(string: url)!
let request = NSMutableURLRequest(url: url2)
request.httpMethod = "GET"
let postString = ""
request.httpBody = postString.data(using: String.Encoding.utf8)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
//print(json!)
let response = json!["value"] as! String
} catch {
print(" error adding tap search to db:\(error)")
}
print()
}
task.resume()
completion(SoldForAppIntentResponse.success(response: "\n\nSold For will look up \(intent.sport!) \(intent.cardInfo!) \(intent.cardNumber!)"))
}
解决方案
问题实际上是url2
本身,因为字符串https://www.host.com/url/1986 fleer 57 Michael Jordan PSA 10
无法正确转换为URL
对象。
你可以尝试这样的事情:
if let url = "https://www.host.com/url/\(intent.cardInfo!)".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) {
let url2 = URL(string: url)!
}
推荐阅读
- c# - 如何在别人的计算机上运行我的 c# 控制台应用程序
- laravel - 函数redirectTo中的角色重定向问题
- kubernetes-ingress - 允许 Azure 应用程序网关路由 AKS 中的所有子路径
- java - Assertj:如何按对象内容比较 2 个对象列表?
- python-3.x - 输出表中的数字。Python
- arrays - 通过 profile.education 数组映射时,.map 未定义
- reactjs - 如何告诉 google bot 在 React 中找不到该页面?
- microsoft-graph-api - Microsoft Graph API 在删除联系人后创建空的已删除消息
- python - 从 ubuntu docker 镜像中删除 perl 会导致 pyodbc 失败
- ansible - ansible playbook中的用户密码创建