从 Block 获取 WebService 响应到 WatchKit App
Getting WebService response from Block to WatchKit App
我目前正在努力让一款应用与 AppleWatch 兼容,
为此我正在调用一个 WebService,问题是我在 Block 中得到 WebService 响应,而 reply() 块没有被调用并显示错误。
错误,
The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]
我的 Appdelegate,
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
getInfo(userInfo, reply: reply)
}
func getInfo(userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) ->Void)!){
let dicParam:NSDictionary = [:]
let wsfr:WSFrameWork = WSFrameWork(URLAndParams:WS_GET_DATA, dicParams: dicParam as [NSObject : AnyObject])
wsfr.isLogging = false
wsfr.onError = { (error : NSError!) -> Void in
reply(nil)
}
wsfr.onSuccess = { (dicResponse : Dictionary!) -> Void in
if dicResponse["data"] != nil{
let returnData = ["Returned NSData":dicResponse]
reply(returnData)
}
}
}
WSFrameWork 是我们的 WebService 框架。
问题是方法 handleWatchKitExtensionRequest
在 OS 终止之前没有完成。因此:
- 显式创建后台任务。
- 如果要异步获取数据,请确保方法
handleWatchKitExtensionRequest
在后台获取完成之前没有离开。
这里有一个 展示了如何实现上面的内容。
所以最后我通过同步 WS 请求得到了答案,
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
getCurrentDataInfo(userInfo, reply: reply)
}
func getCurrentDataInfo(userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) ->Void)!){
var data = parseJSON(getJSON("https://My_JSON_URL"))
println(data)
reply(["myData":data])
}
func getJSON(urlToRequest: String) -> NSData{
return NSData(contentsOfURL: NSURL(string: urlToRequest)!)!
}
func parseJSON(inputData: NSData) -> NSDictionary{
var error: NSError?
var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary
return boardsDictionary
}
我目前正在努力让一款应用与 AppleWatch 兼容,
为此我正在调用一个 WebService,问题是我在 Block 中得到 WebService 响应,而 reply() 块没有被调用并显示错误。
错误,
The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]
我的 Appdelegate,
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
getInfo(userInfo, reply: reply)
}
func getInfo(userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) ->Void)!){
let dicParam:NSDictionary = [:]
let wsfr:WSFrameWork = WSFrameWork(URLAndParams:WS_GET_DATA, dicParams: dicParam as [NSObject : AnyObject])
wsfr.isLogging = false
wsfr.onError = { (error : NSError!) -> Void in
reply(nil)
}
wsfr.onSuccess = { (dicResponse : Dictionary!) -> Void in
if dicResponse["data"] != nil{
let returnData = ["Returned NSData":dicResponse]
reply(returnData)
}
}
}
WSFrameWork 是我们的 WebService 框架。
问题是方法 handleWatchKitExtensionRequest
在 OS 终止之前没有完成。因此:
- 显式创建后台任务。
- 如果要异步获取数据,请确保方法
handleWatchKitExtensionRequest
在后台获取完成之前没有离开。
这里有一个
所以最后我通过同步 WS 请求得到了答案,
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
getCurrentDataInfo(userInfo, reply: reply)
}
func getCurrentDataInfo(userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) ->Void)!){
var data = parseJSON(getJSON("https://My_JSON_URL"))
println(data)
reply(["myData":data])
}
func getJSON(urlToRequest: String) -> NSData{
return NSData(contentsOfURL: NSURL(string: urlToRequest)!)!
}
func parseJSON(inputData: NSData) -> NSDictionary{
var error: NSError?
var boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary
return boardsDictionary
}