{"id":3483,"date":"2021-07-16T14:00:49","date_gmt":"2021-07-16T06:00:49","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3483"},"modified":"2021-07-16T14:00:49","modified_gmt":"2021-07-16T06:00:49","slug":"swift-%e5%90%8e%e5%8f%b0%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%ef%bc%88background-fetch%ef%bc%89%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3483","title":{"rendered":"Swift &#8211; \u540e\u53f0\u83b7\u53d6\u6570\u636e\uff08Background Fetch\uff09\u7684\u5b9e\u73b0"},"content":{"rendered":"\n<p>\u524d\u9762\u8bb2\u4e86\u5982\u4f55\u8ba9\u7a0b\u5e8f\u7533\u8bf7\u540e\u53f0\u77ed\u65f6\u8fd0\u884c\u3002\u4f46\u8fd9\u4e2a\u989d\u5916\u5ef6\u957f\u7684\u65f6\u95f4\u6bd5\u7adf\u6709\u9650\u3002\u6240\u4ee5\u4eceiOS7\u8d77\u53c8\u5f15\u5165\u4e24\u79cd\u5728\u540e\u53f0\u8fd0\u884c\u4efb\u52a1\u7684\u65b9\u5f0f\uff1a\u540e\u53f0\u83b7\u53d6\u548c\u540e\u53f0\u901a\u77e5\u3002<br><\/p>\n\n\n\n<p><strong>1\uff0c\u540e\u53f0\u83b7\u53d6\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<p>\u540e\u53f0\u83b7\u53d6\uff08Background Fetch\uff09\u662f\u4e3a\u4e86\u90a3\u4e9b\u9700\u8981\u5b9a\u671f\u66f4\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u7684\uff0c\u6bd4\u5982\u5929\u6c14\u5e94\u7528\uff0c\u65b0\u95fb\u5ba2\u6237\u7aef\uff0c\u793e\u4ea4\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7b49\u3002\u5728\u542f\u52a8\u540e\u53f0\u83b7\u53d6\u4e4b\u540e\uff0c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5728\u540e\u53f0\u88ab\u5524\u9192\uff0c\u5728\u540e\u53f0\u83b7\u53d6\u6700\u65b0\u4fe1\u606f\uff0c\u4ee5\u4fbf\u5728\u7528\u6237\u5c06\u5e94\u7528\u7a0b\u5e8f\u8f6c\u5230\u524d\u53f0\u65f6\u80fd\u591f\u7acb\u5373\u663e\u793a\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n\n\n\n<p><strong>2\uff0c\u540e\u53f0\u83b7\u53d6\u529f\u80fd\u5f00\u542f<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u5728\u9009\u4e2d\u9879\u76ee\uff0c\u5728Capabilities\uff08\u529f\u80fd\uff09\u9009\u9879\u5361\uff0c\u542f\u7528Background Modes\uff08\u540e\u53f0\u6a21\u5f0f\uff09\u4e2d\u7684Background Fetch\uff08\u540e\u53f0\u83b7\u53d6\uff09<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/07\/image-64-1024x437.png\" alt=\"\" class=\"wp-image-3485\" width=\"603\" height=\"257\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64-1024x437.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64-300x128.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64-768x327.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64-830x354.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64-230x98.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64-350x149.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64-480x205.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-64.png 1370w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3\uff0c\u540e\u53f0\u83b7\u53d6\u65f6\u95f4\u95f4\u9694\u8bbe\u7f6e<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u5f00\u542f\u540e\u53f0\u83b7\u53d6\u529f\u80fd\u540e\uff0c\u8fd8\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528setMinimumBackgroundFetchInterval()\u65b9\u6cd5\u8bbe\u7f6e\u6700\u77ed\u5524\u9192\u7a0b\u5e8f\u7684\u65f6\u95f4\u95f4\u9694\uff08\u79d2\uff09\u3002\u4f46\u5177\u4f53\u4ec0\u4e48\u65f6\u5019\u7a0b\u5e8f\u4f1a\u88ab\u5524\u9192\u83b7\u53d6\u6570\u636e\uff0c\u5c31\u4e0d\u53ef\u63a7\u4e86\u3002\u8fd9\u4e2a\u7531\u7cfb\u7edf\u81ea\u5df1\u51b3\u5b9a\u3002<br>\uff082\uff09\u5982\u679c\u60f3\u8981\u544a\u8bc9\u5c3d\u53ef\u80fd\u9891\u7e41\u8c03\u7528\uff0c\u5c31\u76f4\u63a5\u8bbe\u6210UIApplicationBackgroundFetchIntervalMinimum\u3002<\/p>\n\n\n\n<p><strong>4\uff0c\u4f7f\u7528\u6837\u4f8b<\/strong><\/p>\n\n\n\n<p>\u7a0b\u5e8f\u6302\u8d77\u540e\uff0c\u81ea\u52a8\u4ece\u540e\u53f0\u83b7\u53d6\u5317\u4eac\u65f6\u95f4\uff0c\u83b7\u53d6\u540e\u4f7f\u7528\u672c\u5730\u901a\u77e5\u63d0\u9192\u7528\u6237\u3002<\/p>\n\n\n\n<p>\uff08\u6ce8\u610f\uff1a\u540e\u53f0\u83b7\u53d6\u6267\u884c\u540e\u8981\u8c03\u7528completionHandler\uff0c\u544a\u8bc9\u7cfb\u7edf\u83b7\u53d6\u6570\u636e\u662f\u5426\u6210\u529f\uff09<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/07\/image-66.png\" alt=\"\" class=\"wp-image-3487\" width=\"550\" height=\"408\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-66.png 914w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-66-300x223.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-66-768x571.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-66-230x171.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-66-350x260.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/07\/image-66-480x357.png 480w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\n \n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n     \n    var window: UIWindow?\n     \n    func application(application: UIApplication,\n        didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {\n            \/\/\u5f00\u542f\u901a\u77e5\n            let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound],\n                categories: nil)\n            application.registerUserNotificationSettings(settings)\n            return true\n    }\n     \n    func applicationWillResignActive(application: UIApplication) {\n    }\n     \n    func applicationDidEnterBackground(application: UIApplication) {\n        \/\/\u867d\u7136\u5b9a\u4e49\u4e86\u540e\u53f0\u83b7\u53d6\u7684\u6700\u77ed\u65f6\u95f4\uff0c\u4f46iOS\u4f1a\u81ea\u884c\u4ee5\u5b83\u8ba4\u5b9a\u7684\u6700\u4f73\u65f6\u95f4\u6765\u5524\u9192\u7a0b\u5e8f\uff0c\u8fd9\u4e2a\u6211\u4eec\u65e0\u6cd5\u63a7\u5236\n        \/\/UIApplicationBackgroundFetchIntervalMinimum \u5c3d\u53ef\u80fd\u9891\u7e41\u7684\u8c03\u7528\u6211\u4eec\u7684Fetch\u65b9\u6cd5\n        application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)\n    }\n     \n    func applicationWillEnterForeground(application: UIApplication) {\n    }\n     \n    func applicationDidBecomeActive(application: UIApplication) {\n    }\n     \n    func applicationWillTerminate(application: UIApplication) {\n    }\n     \n    \/\/\u540e\u53f0\u83b7\u53d6\u6570\u636e\n    func application(application: UIApplication,\n        performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {\n            \/\/\u521b\u5efaNSURL\u5bf9\u8c61\n            let url:NSURL! = NSURL(string:\"http:\/\/api.k780.com:88\/?app=life.time&amp;appkey=10003&amp;sign=b59bc3ef6191eb9f747dd4e83c99f2a4&amp;format=json\")\n            \/\/\u521b\u5efa\u8bf7\u6c42\u5bf9\u8c61\n            let request:NSURLRequest = NSURLRequest(URL: url)\n             \n            let session = NSURLSession.sharedSession()\n             \n            let dataTask = session.dataTaskWithRequest(request,\n                completionHandler: {(data, response, error) -> Void in\n                    if error != nil{\n                        print(error?.code)\n                        print(error?.description)\n                         \n                        \/\/\u8ba9OS\u77e5\u9053\u83b7\u53d6\u6570\u636e\u5931\u8d25\n                        completionHandler(UIBackgroundFetchResult.Failed)\n                    }else{\n                        let str = NSString(data: data!, encoding: NSUTF8StringEncoding)\n                        print(str)\n                        \/\/\u6e05\u9664\u6240\u6709\u672c\u5730\u63a8\u9001\n                        \/\/UIApplication.sharedApplication().cancelAllLocalNotifications()\n                        \/\/\u521b\u5efaUILocalNotification\u6765\u8fdb\u884c\u672c\u5730\u6d88\u606f\u901a\u77e5\n                        let localNotification = UILocalNotification()\n                        \/\/\u63a8\u9001\u65f6\u95f4\uff08\u7acb\u523b\u63a8\u9001\uff09\n                        localNotification.fireDate = NSDate(timeIntervalSinceNow: 0)\n                        \/\/\u65f6\u533a\n                        localNotification.timeZone = NSTimeZone.defaultTimeZone()\n                        \/\/\u63a8\u9001\u5185\u5bb9\n                        localNotification.alertBody = \"\u83b7\u53d6\u65f6\u95f4\u6210\u529f\uff1a\\(str)\"\n                        \/\/\u58f0\u97f3\n                        localNotification.soundName = UILocalNotificationDefaultSoundName\n                        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)\n                         \n                        \/\/\u8ba9OS\u77e5\u9053\u5df2\u7ecf\u83b7\u53d6\u5230\u65b0\u6570\u636e\n                        completionHandler(UIBackgroundFetchResult.NewData)\n                        \/\/completionHandler(UIBackgroundFetchResult.NoData)\n                    }\n            }) as NSURLSessionTask\n             \n            \/\/\u4f7f\u7528resume\u65b9\u6cd5\u542f\u52a8\u4efb\u52a1\n            dataTask.resume()\n    }\n}<\/pre>\n\n\n\n<p><strong>5\uff0c\u540e\u53f0\u83b7\u53d6\u6d4b\u8bd5\u00a0<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u6a21\u62df\u5668\u6d4b\u8bd5\u7684\u8bdd\uff0c\u53ea\u8981\u9009\u62e9Xcode \u83dc\u5355\u680f\u7684 Debug\u4e0b\u7684 Simulate Background Fetch\u5373\u53ef\u00a0<br>\uff082\uff09\u771f\u673a\u8c03\u8bd5\uff0c\u5982\u679c\u6ca1\u6709\u8fde\u7740\u7535\u8111\u6709\u65f6\u5019\u7b49\u4e86\u4e00\u5929\u624d\u53d1\u751f\u540e\u53f0\u83b7\u53d6\u3002\u8fde\u7740\u7535\u8111\u53ef\u80fd\u7b49\u4e2a\u51e0\u5206\u949f\u5c31\u80fd\u83b7\u53d6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u8bb2\u4e86\u5982\u4f55\u8ba9\u7a0b\u5e8f\u7533\u8bf7\u540e\u53f0\u77ed\u65f6\u8fd0\u884c\u3002\u4f46\u8fd9\u4e2a\u989d\u5916\u5ef6\u957f\u7684\u65f6\u95f4\u6bd5\u7adf\u6709\u9650\u3002\u6240\u4ee5\u4eceiOS7\u8d77\u53c8\u5f15\u5165\u4e24\u79cd\u5728\u540e\u53f0\u8fd0\u884c\u4efb\u52a1\u7684\u65b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3483"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3483"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3483\/revisions"}],"predecessor-version":[{"id":3488,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3483\/revisions\/3488"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}