ios UI应用程序.shared. background剩余时间始终小于2秒

yjghlzjz  于 2023-01-18  发布在  iOS
关注(0)|答案(1)|浏览(205)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
    return true
}


 func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        let configuration = URLSessionConfiguration.background(withIdentifier: "smat.Tracker")
        switch UIApplication.shared.applicationState {
        case .active:
            print("App is active.")
        case .background:
            print("App is in background.")
            print("Background time remaining = \(UIApplication.shared.backgroundTimeRemaining) seconds")
        case .inactive:
            break
        }
        print(UIApplication.shared.applicationState.rawValue)
        print(configuration.allowsCellularAccess)
        print(configuration.timeoutIntervalForResource)

        let headers: HTTPHeaders = [

            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "en-US,en;q=0.9",
            "referer": "https://frys.com/",
            "host":"frys.com",
            "upgrade-insecure-requests": "1",
            "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
        ]
        configuration.httpAdditionalHeaders = headers
        let base_url: String = "https://frys.com/search?search_type=regular&sqxts=1&cat=&query_string=" + "vivomove" + "&nearbyStoreName=false"
        let sessionManager = Alamofire.SessionManager(configuration: configuration)
        print("came here")
        if(Trackers.urls.count > 0 ){
            print(Trackers.urls.count)
        }

        Alamofire.request(base_url , headers: headers).responseString { response in
            //print(response.result.value)
            do {
                print("response12 ")
                guard let doc = try! response.result.value else{
                    completionHandler(.noData)
                    throw NSError(domain: "ERROR", code: 42, userInfo: ["BKGROUND":"err"])
                }
                completionHandler(.newData)
                //self.endBackgroundTask()
            }
            catch {
                print("error")
            }
        }

    }

我在AppDelegate. swift中有上面的代码,我在几个文档/教程中读到,完成后台任务所需的最短时间是30秒或更长,为什么我总是让上面的时间小于2秒?
控制台的输出示例如下:
应用程序处于后台。后台剩余时间= 1.79769313486232e+308秒0.0 1.79769313486232e+308 UI应用程序状态

hwamh0ep

hwamh0ep1#

这意味着您正在XCode调试器下运行它。
在使用调试器时,您会收到bacgroundRemainginTime = 1.79769313486232e+308,这是一个很大的数字(1.79 * 10 ^ 308),它是双精度浮点格式中最大的可表示正数。

相关问题