Swift: NSURL 会话不适用于 watchOS
Swift: NSURL Session not working with watchOS
我正在使用 NSURL 会话获取特定网站的 HTML 代码。以下代码在 iOS(在 ViewController.swift 中)中运行得很好,但在 watchOS(在 InterfaceController.swift 中)中总是失败。代码总是打印 else 语句:print("apple watch: error with loading nsurlsession").
任何人都可以解释一下(为什么失败以及)我该如何改变它?我很感激每一个回复!
(我将 swift 4.0 与 iOS 11 和 watchOS 4.0 一起使用 - 在模拟器和 iPhone 7 上与配对的 Apple Watch 系列 2 进行了测试)
代码:
func authorizeBase() {
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.data(using: String.Encoding.utf8.rawValue)! as NSData
let base64LoginString = loginData.base64EncodedString(options: [])
let url: NSURL = NSURL(string: urlPath)!
let request: NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
request.httpMethod = "GET"
let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData //deactivate cache
config.urlCache = nil
let authString = "Basic \(base64LoginString)"
config.httpAdditionalHeaders = ["Authorization" : authString]
let session = URLSession(configuration: config)
session.dataTask(with: url as URL) {
( data, response, error) in
if (response as? HTTPURLResponse) != nil {
_ = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
let contents = String(data: data!, encoding: .ascii)
print("apple watch: Content:", contents!)
self.parseHTMLOverview(content: contents!)
self.updateTable() //!!!
}
else {
print("apple watch: error with loading nsurlsession")
}
}.resume()
}
非常感谢您的提示 Srstka!
经过几个小时的思考,我想通了。我只是忘了将 "Allow Arbitrary Loads" 添加到 WatchKit 扩展中的 Info.plist。
我正在使用 NSURL 会话获取特定网站的 HTML 代码。以下代码在 iOS(在 ViewController.swift 中)中运行得很好,但在 watchOS(在 InterfaceController.swift 中)中总是失败。代码总是打印 else 语句:print("apple watch: error with loading nsurlsession").
任何人都可以解释一下(为什么失败以及)我该如何改变它?我很感激每一个回复!
(我将 swift 4.0 与 iOS 11 和 watchOS 4.0 一起使用 - 在模拟器和 iPhone 7 上与配对的 Apple Watch 系列 2 进行了测试)
代码:
func authorizeBase() {
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.data(using: String.Encoding.utf8.rawValue)! as NSData
let base64LoginString = loginData.base64EncodedString(options: [])
let url: NSURL = NSURL(string: urlPath)!
let request: NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
request.httpMethod = "GET"
let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData //deactivate cache
config.urlCache = nil
let authString = "Basic \(base64LoginString)"
config.httpAdditionalHeaders = ["Authorization" : authString]
let session = URLSession(configuration: config)
session.dataTask(with: url as URL) {
( data, response, error) in
if (response as? HTTPURLResponse) != nil {
_ = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
let contents = String(data: data!, encoding: .ascii)
print("apple watch: Content:", contents!)
self.parseHTMLOverview(content: contents!)
self.updateTable() //!!!
}
else {
print("apple watch: error with loading nsurlsession")
}
}.resume()
}
非常感谢您的提示 Srstka!
经过几个小时的思考,我想通了。我只是忘了将 "Allow Arbitrary Loads" 添加到 WatchKit 扩展中的 Info.plist。