相关疑难解决方法(0)

URLSession.datatask,后台没有调用请求块

URLSession当应用程序处于后台时,数据任务块没有调用,并且它仍然处于dataTask请求状态.
当我打开应用程序时,块被调用.顺便说一句,我正在使用https请求.
这是我的代码:

    let request = NSMutableURLRequest(url: URL(string: url as String)!,

                                      cachePolicy: .reloadIgnoringCacheData,

                                      timeoutInterval:20)

    request.httpMethod = method as String

    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

    let session = URLSession.shared

    let data = params.data(using: String.Encoding.utf8.rawValue)

    request.httpBody = data

    session.dataTask(with: request as URLRequest,completionHandler:

        {(data, response, error) -> Void in

         if error == nil

            {

                do {

                    let result = try JSONSerialization.jsonObject(with: data!, options:

                        JSONSerialization.ReadingOptions.mutableContainers)

                    print(result)

                     completionHandler(result as AnyObject?,nil)

                }

                catch let JSONError as NSError{

                    completionHandler(nil,JSONError.localizedDescription as NSString?)

                }

            }

            else{ …
Run Code Online (Sandbox Code Playgroud)

xcode ios nsurlsession swift xcode8

6
推荐指数
1
解决办法
6931
查看次数

标签 统计

ios ×1

nsurlsession ×1

swift ×1

xcode ×1

xcode8 ×1