{"id":7234,"date":"2025-06-04T12:32:42","date_gmt":"2025-06-05T04:39:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/uncategorized\/https-badgameshow-com-steven-290\/"},"modified":"2025-06-05T12:32:42","modified_gmt":"2025-06-05T04:39:42","slug":"https-badgameshow-com-steven-290","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-290\/","title":{"rendered":"Swift \u53d6\u5f97\u4f7f\u7528\u8005\u4f4d\u7f6e\uff1a2025 \u6700\u65b0\u7d93\u7def\u5ea6\u7372\u53d6\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u53d6\u5f97\u4f7f\u7528\u8005\u4f4d\u7f6e, \u7d93\u7def\u5ea6, CoreLocation, iOS\u958b\u767c\"><\/p>\n<h2>Swift \u53d6\u5f97\u4f7f\u7528\u8005\u4f4d\u7f6e\uff1a2025 \u6700\u65b0\u7d93\u7def\u5ea6\u7372\u53d6\u6559\u5b78<\/h2>\n<p>\u5728\u958b\u767c iOS App \u6642\uff0c\u7d93\u5e38\u9700\u8981\u7372\u53d6\u4f7f\u7528\u8005\u7684\u4f4d\u7f6e\u4ee5\u53ca\u7d93\u7def\u5ea6\u8cc7\u8a0a\uff0c\u4ee5\u4fbf\u61c9\u7528\u7a0b\u5f0f\u80fd\u5920\u63d0\u4f9b\u66f4\u597d\u7684\u670d\u52d9\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u7684 <strong>CoreLocation<\/strong> \u6846\u67b6\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b2025\u5e74\u7684\u6700\u4f73\u5be6\u8e10\u8207\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u914d\u7f6e Info.plist<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 <strong>info.plist<\/strong> \u4e2d\u6dfb\u52a0 <strong>NSLocationWhenInUseUsageDescription<\/strong> \u7684 key\uff0c\u4e26\u8a2d\u7f6e\u4e00\u6bb5\u8aaa\u660e\u6587\u5b57\uff0c\u544a\u8a34\u4f7f\u7528\u8005\u70ba\u4ec0\u9ebc\u9700\u8981\u7372\u53d6\u4ed6\u5011\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<br \/>\n&#8220;`xml<br \/>\n<key>NSLocationWhenInUseUsageDescription<\/key><br \/>\n<string>\u70ba\u4e86\u63d0\u4f9b\u66f4\u597d\u7684\u670d\u52d9\uff0c\u6211\u5011\u9700\u8981\u7372\u53d6\u60a8\u7684\u4f4d\u7f6e\u3002<\/string><br \/>\n&#8220;`<\/p>\n<h3>\u6b65\u9a5f 2\uff1a\u5728 ViewController \u4e2d\u5be6\u4f5c\u7a0b\u5f0f\u78bc<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5728 ViewController \u4e2d\u52a0\u5165\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff0c\u4f86\u7372\u53d6\u4f7f\u7528\u8005\u7684\u4f4d\u7f6e\uff1a<br \/>\n&#8220;`swift<br \/>\nimport UIKit<br \/>\nimport CoreLocation<\/p>\n<p>class ViewController: UIViewController, CLLocationManagerDelegate {<br \/>\n    let locationManager = CLLocationManager()<\/p>\n<p>    override func viewDidLoad() {<br \/>\n        super.viewDidLoad()<br \/>\n        locationManager.delegate = self<br \/>\n        locationManager.requestWhenInUseAuthorization()<br \/>\n        locationManager.startUpdatingLocation()<br \/>\n    }<\/p>\n<p>    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {<br \/>\n        guard let location = locations.last else { return }<br \/>\n        let latitude = location.coordinate.latitude<br \/>\n        let longitude = location.coordinate.longitude<br \/>\n        print(&#8220;latitude: \\(latitude), longitude: \\(longitude)&#8221;)<br \/>\n    }<\/p>\n<p>    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {<br \/>\n        print(&#8220;Error while trying to find user&#8217;s location: \\(error.localizedDescription)&#8221;)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<br \/>\n\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u5728\u4f7f\u7528\u8005\u6388\u6b0a\u5f8c\u958b\u59cb\u7372\u53d6\u4f4d\u7f6e\uff0c\u4e26\u5c07\u7d93\u7def\u5ea6\u5370\u51fa\u3002\u5982\u679c\u7372\u53d6\u4f4d\u7f6e\u5931\u6557\uff0c\u5247\u6703\u986f\u793a\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u7576\u7372\u53d6\u4f7f\u7528\u8005\u4f4d\u7f6e\u6642\uff0c\u53ef\u80fd\u6703\u51fa\u73fe\u8a31\u591a\u554f\u984c\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; \u4f7f\u7528\u8005\u672a\u6388\u4e88\u4f4d\u7f6e\u6b0a\u9650\u3002<br \/>\n&#8211; \u8a2d\u5099\u7684\u5b9a\u4f4d\u670d\u52d9\u672a\u958b\u555f\u3002<br \/>\n&#8211; App \u5728\u80cc\u666f\u57f7\u884c\u6642\u7121\u6cd5\u7372\u53d6\u4f4d\u7f6e\u3002<\/p>\n<p>\u70ba\u4e86\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\uff0c\u8acb\u78ba\u4fdd\u5728 Info.plist \u4e2d\u6b63\u78ba\u914d\u7f6e\u6b0a\u9650\uff0c\u4e26\u5728 App \u8a2d\u5b9a\u4e2d\u555f\u7528\u5b9a\u4f4d\u670d\u52d9\u3002<\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u638c\u63e1\u4e86\u57fa\u672c\u7684\u7d93\u7def\u5ea6\u7372\u53d6\u5f8c\uff0c\u60a8\u53ef\u4ee5\u9032\u4e00\u6b65\u5c07\u9019\u9805\u6280\u8853\u61c9\u7528\u65bc\u5730\u5716\u986f\u793a\u3001\u9644\u8fd1\u5546\u5bb6\u641c\u5c0b\u6216\u5373\u6642\u4ea4\u901a\u66f4\u65b0\u7b49\u529f\u80fd\uff0c\u589e\u5f37\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e\u4f7f\u7528 <strong>CoreLocation<\/strong> \u6846\u67b6\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u7372\u53d6\u4f7f\u7528\u8005\u7684\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f\uff0c\u9032\u800c\u6539\u5584 iOS App \u7684\u4f7f\u7528\u9ad4\u9a57\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u6709\u6548\u5be6\u4f5c\u5b9a\u4f4d\u529f\u80fd\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u53d6\u5f97\u4f7f\u7528\u8005\u4f4d\u7f6e-\ud83d\uddfa\ufe0f-\u53d6\u5f97\u7d93\u7def\u5ea6_1675409438.23911.webp\" alt=\"Swift \u53d6\u5f97\u4f7f\u7528\u8005\u4f4d\u7f6e \ud83d\uddfa\ufe0f \u53d6\u5f97\u7d93\u7def\u5ea6\" title=\"Swift \u4f4d\u7f6e\u53d6\u5f97\u3001Swift \u7d93\u7def\u5ea6\u53d6\u5f97\u3001Swift \u5b9a\u4f4d\u529f\u80fd\u3001Swift \u5b9a\u4f4d\u7a0b\u5f0f\u78bc\u3001Swift \u5b9a\u4f4d\u7bc4\u4f8b\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u8655\u7406\u4f7f\u7528\u8005\u62d2\u7d55\u6388\u6b0a\u7684\u60c5\u6cc1\uff1f<\/h3>\n<p>A1: \u6709\u5fc5\u8981\u5728 App \u4e2d\u63d0\u4f9b\u4e00\u500b\u754c\u9762\uff0c\u544a\u77e5\u4f7f\u7528\u8005\u70ba\u4f55\u9700\u8981\u4f4d\u7f6e\u6b0a\u9650\uff0c\u4e26\u5f15\u5c0e\u4ed6\u5011\u66f4\u65b0\u8a2d\u5b9a\u3002<\/p>\n<h3>Q2: \u4f4d\u7f6e\u66f4\u65b0\u7684\u983b\u7387\u5982\u4f55\u8a2d\u7f6e\uff1f<\/h3>\n<p>A2: \u60a8\u53ef\u4ee5\u4f7f\u7528 <code>locationManager.distanceFilter<\/code> \u6216 <code>locationManager.desiredAccuracy<\/code> \u4f86\u8abf\u6574\u4f4d\u7f6e\u66f4\u65b0\u7684\u983b\u7387\u8207\u6e96\u78ba\u5ea6\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5728\u80cc\u666f\u6a21\u5f0f\u4e0b\u7372\u53d6\u4f4d\u7f6e\uff1f<\/h3>\n<p>A3: \u60a8\u9700\u5728 Info.plist \u4e2d\u6dfb\u52a0 <strong>UIBackgroundModes<\/strong> \u4e26\u8a2d\u7f6e <strong>location<\/strong>\uff0c\u540c\u6642\u78ba\u4fdd\u9069\u7576\u7ba1\u7406\u96fb\u6c60\u4f7f\u7528\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u53d6\u5f97\u4f7f\u7528\u8005\u4f4d\u7f6e\u53ca\u7d93\u7def\u5ea6\uff0c\u4e26\u63d0\u4f9b\u7c21\u55ae\u6613\u61c2\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n","protected":false},"author":1,"featured_media":7233,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u53d6\u5f97\u4f7f\u7528\u8005\u4f4d\u7f6e-\ud83d\uddfa\ufe0f-\u53d6\u5f97\u7d93\u7def\u5ea6_1675409438.23911.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1SG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=7234"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7234\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7233"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}