0

我有一个 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()
4

0 回答 0