应用程序进入后台模式后无法立即获得响应

时间:2019-07-10 07:52:38

标签: ios urlsession background-fetch info-plist

我有一个api调用,每当应用程序在后台运行或激活时都需要调用。我已经在响应处理程序中放置了断点。当应用程序处于活动状态时,控制进入断点,但在应用程序进入后台时则永远不会执行。似乎没有调用api。我在项目功能中启用了后台获取功能,并在 Info.plist 中使用以下后台模式键:

<key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>remote-notification</string>
</array>  

Api通话:

let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
    if error != nil {
        print("Error:...")
    } else {
        let responseString = String(data: data!, encoding: .utf8)
        let data = responseString?.data(using: .utf8)!                            
        do {
            if let responseDict = try JSONSerialization.jsonObject(with: data!, options : .allowFragments) as? [String: Any] {
                self.delegate.apiClientDidFinishWithResponse(response: responseDict, forRequest: self.serviceCallType)
            } else {
                //....
            }
         } catch let error as NSError {
            print(error)                
         }
     }
 })

 dataTask.resume()

0 个答案:

没有答案