ios - 带有 dataTaskWithRequest 的 NSURLSession 仅在应用启动时触发,而不是在应用未启动时触发 运行
ios - NSURLSession with dataTaskWithRequest fires only when app launch, not while the app is not running
如题所述,远程通知来了我可以短信回复。如果我点击主页按钮一次,我的 http 请求运行良好,但当应用程序不是 运行 时不起作用,它会等待。当应用程序启动时,它也能正常工作。
// Please work
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
print(userInfo["personB"])
if identifier == "comment-reply" {
if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey],
responseText = response as? String {
let request = NSMutableURLRequest(URL: NSURL(string: "https://example.com/post-ios.php")!)
request.HTTPMethod = "POST"
let p = "a=123&b=\(responseText)"
request.HTTPBody = p.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
task.resume()
}
}
completionHandler()
}
现在,我需要:
-VoIP证书,
-后台会话配置,
-派遣东西,
要么
-上传任务?
谁能帮帮我?
更新 上传任务
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
let id = userInfo["personB"]!
if identifier == "comment-reply" {
if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey],
responseText = response as? String {
let conf = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("replyPost")
let requ = NSMutableURLRequest(URL: NSURL(string: "https://example.com/post-ios.php")!)
requ.HTTPMethod = "POST"
let data = "a=123&b=\(responseText)".dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession(configuration: conf, delegate: self, delegateQueue: NSOperationQueue.mainQueue()).uploadTaskWithRequest(requ, fromData: data!)
task.resume()
}
}
completionHandler()
}
也不行。我要添加额外的处理程序吗?
当您调用 completionHandler() 时,您的应用程序将再次暂停。这将在您的任务完成之前发生。
你应该调用 uploadTask(with:from:completionHandler:)
而不是 uploadTask(with:from:)
并将 completionHandler()
放入完成处理程序块中。
如题所述,远程通知来了我可以短信回复。如果我点击主页按钮一次,我的 http 请求运行良好,但当应用程序不是 运行 时不起作用,它会等待。当应用程序启动时,它也能正常工作。
// Please work
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
print(userInfo["personB"])
if identifier == "comment-reply" {
if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey],
responseText = response as? String {
let request = NSMutableURLRequest(URL: NSURL(string: "https://example.com/post-ios.php")!)
request.HTTPMethod = "POST"
let p = "a=123&b=\(responseText)"
request.HTTPBody = p.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
task.resume()
}
}
completionHandler()
}
现在,我需要: -VoIP证书, -后台会话配置, -派遣东西, 要么 -上传任务?
谁能帮帮我?
更新 上传任务
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
let id = userInfo["personB"]!
if identifier == "comment-reply" {
if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey],
responseText = response as? String {
let conf = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("replyPost")
let requ = NSMutableURLRequest(URL: NSURL(string: "https://example.com/post-ios.php")!)
requ.HTTPMethod = "POST"
let data = "a=123&b=\(responseText)".dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession(configuration: conf, delegate: self, delegateQueue: NSOperationQueue.mainQueue()).uploadTaskWithRequest(requ, fromData: data!)
task.resume()
}
}
completionHandler()
}
也不行。我要添加额外的处理程序吗?
当您调用 completionHandler() 时,您的应用程序将再次暂停。这将在您的任务完成之前发生。
你应该调用 uploadTask(with:from:completionHandler:)
而不是 uploadTask(with:from:)
并将 completionHandler()
放入完成处理程序块中。