ios - 带有 dataTaskWithRequest 的 NSURLSession 仅在应用程序启动时触发,而不是在应用程序未运行时触发

标签 ios voip nsurlsession nsurlsessiondatatask nsurlsessionuploadtask

如题所述,远程通知来了,我可以用文字回复。如果我点击主页按钮一次,我的 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证书, -后台 session 配置, - dispatch 东西, 或者 -上传任务?

谁能帮帮我?

上传任务的UPDATE

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:) 并放置 completionHandler() 而不是 uploadTask(with:from:)在完成处理程序 block 中。

关于ios - 带有 dataTaskWithRequest 的 NSURLSession 仅在应用程序启动时触发,而不是在应用程序未运行时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39596487/

相关文章:

ios - iOS 8 扩展的 AFNetworking 后台 session 配置

ios - NSURLSession 忽略 NSURLSessionConfiguration 缓存策略

ios - 显示拖动的 MKAnnotationPin 的位置

iphone - 没有互联网时的 NSURLConnection 行为

ios - Flutter-带有搜索栏的自定义条形应用栏

voip - H.264 over RTP 流在播放时损坏

c# - C#、asp.net 或 Java 中的 VOIP

iphone - iOS 中的 vtable 错误

Asterisk:将 Asterisk 系统连接到 SIP 提供商

swift - URLSession 数据任务超时错误