{"id":204,"date":"2020-07-24T17:51:39","date_gmt":"2020-07-24T09:51:39","guid":{"rendered":"http:\/\/123.57.164.21\/?p=204"},"modified":"2020-08-21T13:05:51","modified_gmt":"2020-08-21T05:05:51","slug":"ios-gps%e5%ae%9a%e4%bd%8d-background%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=204","title":{"rendered":"iOS Gps\u5b9a\u4f4d background\u6a21\u5f0f"},"content":{"rendered":"\n<p><strong>\u6211\u4eec\u5728\u8fdb\u884ciOS App\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528Gps\u5730\u56fe\u5b9a\u4f4d\u3002\u76ee\u524diOS\u5b9a\u4f4d\u529f\u80fd\u4e3b\u8981\u7528\u4e0b\u9762\u4e24\u4e2a\u6846\u67b6\u3002  <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   CoreLocation:\uff08\u7740\u91cd\u529f\u80fd\u5b9e\u73b0\uff09\n         \u2460\u5730\u7406\u5b9a\u4f4d\u3000\n         \u2461\u8fd0\u884c\u901f\u5ea6\u53d6\u5f97\u3000\n         \u2462\u6d77\u62d4\u9ad8\u5ea6\u53d6\u5f97 \n         \u2463Gps\u4fe1\u53f7\u5f3a\u5ea6\u53d6\u5f97\n\n   MapKit\uff1a\u7528\u4e8e\u5730\u56fe\u5c55\u793a\uff08\u7740\u91cd\u754c\u9762\u5c55\u793a\uff09\n         \u2460\u81ea\u5b9a\u4e49\u5927\u5934\u9488\u3000\n         \u2461\u5730\u56fe\u753b\u8def\u7ebf  \n         \u2462userLocation\u7b49\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728App\u8fdb\u5165\u5230background\u540e\u53f0\u540e\uff0c\u4ecd\u7136\u80fd\u591f\u4f7f\u7528Gps\u6765\u8fdb\u884c\u5b9a\u4f4d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u9700\u8981\u4f7f\u7528CoreLocation\u6846\u67b6\uff08MapKit\u5728App\u8fdb\u5165\u5230background\u540e\u53f0\u540e\u5c31\u4f1a\u505c\u6b62\uff09\u90a3\u4e48\u5982\u4f55\u5728App\u8fdb\u5165\u5230background\u540e\uff0c\u4f7f\u7528CoreLocation\u6765\u5b9a\u4f4d\u5462\uff1f\u53ea\u9700\u8981\u6309\u7167\u4e0b\u9762\u6b65\u9aa4\u914d\u7f6e\u5c31\u53ef\u4ee5\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">1. \u5728 Xcode --- Signing&amp;Capabilities --- Background Modes \u5f00\u542fLocation updates<\/pre>\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\/2020\/07\/image-15.png\" alt=\"\" class=\"wp-image-214\" width=\"454\" height=\"200\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-15.png 1002w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-15-300x132.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-15-768x339.png 768w\" sizes=\"(max-width: 454px) 100vw, 454px\" \/><figcaption>\u9009\u4e2dLocation updates\uff0c\u5982\u679c\u9700\u8981\u5728\u540e\u53f0\u64ad\u653e\u58f0\u97f3\uff0cAudio\u4e5f\u9700\u8981\u9009\u4e2d\u3002<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">2. \u5728Xcode\u5de5\u7a0b\u4e2dinfo.plist\u4e2d\u8ffd\u52a0\u4e0b\u9762\u4e24\u4e2a\u9879\u76ee\uff1a\n        Privacy - Location Always and When In Use Usage Description\n        Privacy - Location When In Use Usage Description<\/pre>\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\/2020\/07\/image-16-1024x373.png\" alt=\"\" class=\"wp-image-216\" width=\"541\" height=\"197\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-16-1024x373.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-16-300x109.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-16-768x280.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-16.png 1532w\" sizes=\"(max-width: 541px) 100vw, 541px\" \/><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">3. \u5728SceneDelegate\u4e2d\u53ef\u4ee5\u901a\u8fc7CLLocationManager \u542f\u52a8Gps\u3002\n<strong><span class=\"has-inline-color has-vivid-red-color\">    \u203b\u975e\u5e38\u91cd\u8981\uff0c\u53ea\u6709\u8bbe\u5b9aallowsBackgroundLocationUpdates \u548c showsBackgroundLocationIndicator = true<\/span><\/strong> <strong><span class=\"has-inline-color has-vivid-red-color\"> <\/span><\/strong>\n<strong><span class=\"has-inline-color has-vivid-red-color\">Gps\u624d\u80fd\u540e\u53f0\u8fd0\u884c\u3002<\/span><\/strong>\n                   <\/pre>\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\nimport SwiftUI\nimport CoreLocation\n\nclass SceneDelegate: UIResponder, UIWindowSceneDelegate, CLLocationManagerDelegate {\n\n    var window: UIWindow?\n    \n    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {\n\n        Constants.locationManager.delegate = self\n        Constants.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation\n        Constants.locationManager.distanceFilter = 1\n        Constants.locationManager.requestAlwaysAuthorization()\n        \/\/ \u203b\u975e\u5e38\u91cd\u8981\uff0c\u53ea\u6709\u8bbe\u5b9aallowsBackgroundLocationUpdates\uff0cshowsBackgroundLocationIndicator Gps\u624d\u80fd\u540e\u53f0\u8fd0\u884c\u3002\n        Constants.locationManager.allowsBackgroundLocationUpdates = true\n        Constants.locationManager.showsBackgroundLocationIndicator = true\n        setCurrentLocationFlag = true\n\n        if (CLLocationManager.locationServicesEnabled()) {\n            Constants.locationManager.startUpdatingLocation()\n        }\n\n        let contentView = ContentView()\n\n        \/\/ Use a UIHostingController as window root view controller.\n        if let windowScene = scene as? UIWindowScene {\n            let window = UIWindow(windowScene: windowScene)\n            window.rootViewController = UIHostingController(rootView: contentView)\n            self.window = window\n            window.makeKeyAndVisible()\n        }\n    }\n    \n    \/\/ \u5f53Gps\u53d6\u5f97\u65b0Location\u65f6\uff0c\u4f1aactive locationManager\u8fd9\u4e2a\u65b9\u6cd5\n    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {\n        \/\/ \u83b7\u53d6\u6700\u65b0\u7684\u5750\u6807\n        let currLocation: CLLocation = locations.last! \n    }\n\n    \/\/ App\u4ece\u540e\u53f0\u8fdb\u5165\u524d\u53f0\u65f6\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\n    func sceneWillEnterForeground(_ scene: UIScene) {\n        print(\"sceneWillEnterForeground\")\n        \/\/ Called as the scene transitions from the background to the foreground.\n        \/\/ Use this method to undo the changes made on entering the background.\n    }\n   \n    \/\/ App\u8fdb\u5165\u540e\u53f0\u65f6\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\n    func sceneDidEnterBackground(_ scene: UIScene) {\n        print(\"sceneDidEnterBackground\" )\n    }<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">4. \u5728\u8bbe\u5907\u4e2d\uff0c App\u4f4d\u7f6e\u60c5\u62a5\u8bbe\u5b9a\u4e2d\u3002\u9009\u62e9\u300c\u4f7f\u7528\u4e2d\u8bb8\u53ef\u300d \u6216\u8005 \u300c\u4e00\u76f4\u4f7f\u7528\u300d\u3002<\/pre>\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\/2020\/07\/image-17-1024x467.png\" alt=\"\" class=\"wp-image-222\" width=\"455\" height=\"207\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-17-1024x467.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-17-300x137.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-17-768x350.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-17-1536x700.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-17-1600x730.png 1600w, https:\/\/92it.top\/wp-content\/uploads\/2020\/07\/image-17.png 1750w\" sizes=\"(max-width: 455px) 100vw, 455px\" \/><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"wp-block-preformatted\">5.  \u5982\u679c\u60f3\u4f7f\u7528AVAudioPlayer\uff0c \u5728background\u540e\u53f0\u64ad\u653e\u58f0\u97f3\u3002\u9700\u8981\u6ce8\u610f options: [.mixWithOthers] \u548c setActive(true)<\/pre>\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=\"\">  try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [.mixWithOthers])\n                        \n  try AVAudioSession.sharedInstance().setActive(true)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5728\u8fdb\u884ciOS App\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528Gps\u5730\u56fe\u5b9a\u4f4d\u3002\u76ee\u524diOS\u5b9a\u4f4d\u529f\u80fd\u4e3b\u8981\u7528\u4e0b\u9762\u4e24\u4e2a\u6846\u67b6\u3002 Cor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/204"}],"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=204"}],"version-history":[{"count":22,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":923,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions\/923"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}