{"id":142,"date":"2018-10-03T09:05:41","date_gmt":"2018-10-03T01:05:41","guid":{"rendered":"http:\/\/www.daoyeer.win\/?p=142"},"modified":"2020-10-27T10:55:42","modified_gmt":"2020-10-27T02:55:42","slug":"ios%e5%90%8e%e5%8f%b0%e5%ae%9a%e4%bd%8d%e4%b8%8a%e4%bc%a0%e5%ae%9e%e8%b7%b5%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"http:\/\/www.daoyeer.win\/?p=142","title":{"rendered":"iOS\u540e\u53f0\u5b9a\u4f4d\u4e0a\u4f20\u8bb0\u5f55"},"content":{"rendered":"<p><!-- wp:heading {\"level\":4} --><\/p>\n<h4>1\u3001\u524d\u8a00<\/h4>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>        \u76ee\u524d\u5728\u505a\u7684\u7269\u6d41\u9879\u76ee\uff0c\u53f8\u673a\u7aef\u7684<strong>\u540e\u53f0\u5b9e\u65f6\u4e0a\u4f20\u4f4d\u7f6e<\/strong>\u5219\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u6280\u672f\u70b9 \u63a5\u4e0b\u6765\u5c31\u6765\u8bf4\u8bf4\u6211\u5173\u4e8e\u8fd9\u65b9\u9762\u7684\u5b9e\u8df5\u7ecf\u9a8c\u3002<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:heading {\"level\":4} --><\/p>\n<h4>2\u3001\u9700\u6c42<\/h4>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:quote --><\/p>\n<blockquote class=\"wp-block-quote\">\n<p><strong>1. \u5982\u679c\u7528\u6237\u7684\u4f4d\u7f6e\u5728\u6301\u7eed\u53d8\u5316 \u5219\u9694\u4e00\u6bb5\u65f6\u95f4\u4e0a\u62a5\u4e00\u6b21<\/strong><br \/>\u3000\u3000\u7531\u4e8e\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5b9e\u65f6\u7684\u5c06\u7528\u6237\u7684\u4f4d\u7f6e\u53d8\u5316\u53cd\u9988\u5728APP\u91cc \u6240\u4ee5\u5b9a\u65f6\u7684\u4e0a\u62a5\u662f\u521a\u9700<\/p>\n<p><strong>2. \u5982\u679c\u7528\u6237\u7684\u79fb\u52a8\u901f\u5ea6\u5f88\u6162 \u5219\u9694\u4e00\u6bb5\u8ddd\u79bb\u4e0a\u62a5\u4e00\u6b21<\/strong><br \/>\u3000\u3000\u5982\u679c\u7528\u6237\u662f\u4f4e\u901f\u7387\u7684\u72b6\u6001(\u6bd4\u5982\u6b65\u884c\u7684\u79fb\u52a8\u901f\u5ea6\u5927\u6982\u5c31\u662f1m\/s\u5de6\u53f3) \u8fd9\u4e2a\u65f6\u5019\u5982\u679c\u8fd8\u6309(1)\u4e2d\u7684\u65b9\u5f0f\u6765\u4e0a\u62a5\u7684\u8bdd \u7531\u4e8e\u53d8\u5316\u592a\u5c0f \u5730\u56fe\u4e0a\u7684\u70b9\u4f1a\u975e\u5e38\u7684\u5bc6\u96c6 \u8fd9\u79cd\u6570\u636e\u7684\u610f\u4e49\u4e0d\u5927(\u800c\u4e14\u5982\u679c\u8981\u505a\u8f68\u8ff9\u670d\u52a1\u7684\u8bdd \u8fd9\u4e9b\u5bc6\u96c6\u70b9\u90fd\u662f\u5fc5\u987b\u4f18\u5316\u6389\u7684) \u6240\u4ee5\u8fd9\u65f6\u5019\u6211\u4eec\u6309\u7167\u8ddd\u79bb\u95f4\u9694\u6765\u4e0a\u62a5<\/p>\n<p><strong>3. \u5982\u679c\u7528\u6237\u7684\u4f4d\u7f6e\u5728\u5230\u8fbe\u67d0\u5904\u540e\u6ca1\u6709\u53d8\u5316 \u5219\u4e0d\u7ee7\u7eed\u4e0a\u62a5<\/strong><br \/>\u3000\u3000\u6211\u4eec\u53ea\u5173\u5fc3\u4f4d\u7f6e\u7684\u53d8\u5316 \u5982\u679c\u7528\u6237\u7684\u4f4d\u7f6e\u6ca1\u6709\u53d8\u5316\u6216\u8005\u53d8\u5316\u5f88\u5c0f \u5176\u5b9e\u662f\u4e0d\u9700\u8981\u4e0a\u62a5\u5176\u4f4d\u7f6e\u7684(\u6bd4\u5982\u8fdb\u5165\u7684\u516c\u53f8 \u6216\u8005\u7b49\u4e00\u4e2a\u5f88\u957f\u65f6\u95f4\u7684\u7ea2\u706f) \u8fd9\u65f6\u5019\u6211\u4eec\u5c31\u4e0d\u4e0a\u62a5(\u4ee5\u8fbe\u5230\u7701\u7535\u7684\u76ee\u7684)<\/p>\n<p><strong>4. \u5207\u6362\u5230\u540e\u53f0\u4e5f\u8981\u80fd\u5b9a\u4f4d\u4e0a\u62a5<\/strong><br \/>\u3000\u3000\u540e\u53f0\u4e0a\u62a5\u662f\u5fc5\u987b\u7684 \u7528\u6237\u4e0d\u53ef\u80fd\u4e00\u76f4\u8fd0\u884c\u7740\u6211\u4eec\u7684APP (iOS4\u5f00\u59cb\u5c31\u652f\u6301\u4e86)<\/p>\n<p><strong>5. APP\u56e0\u5404\u79cd\u539f\u56e0\u7ec8\u6b62\u8fd0\u884c\u540e(\u7528\u6237\u4e3b\u52a8\u5173\u95ed, \u7cfb\u7edf\u6740\u6389) \u4e5f\u8981\u80fd\u5b9a\u4f4d\u4e0a\u62a5<\/strong><br \/>\u3000\u3000\u7528\u6237\u4e3b\u52a8\u5173\u95edAPP\u7684\u51e0\u7387\u4e0d\u5927 \u4f46\u662f\u56e0\u7cfb\u7edf\u8c03\u5ea6\u88ab\u6740\u6389\u7684\u60c5\u51b5\u662f\u5f88\u666e\u904d\u7684 \u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u4e5f\u8981\u80fd\u591f\u4e0a\u62a5 (iOS7\u5f00\u59cb\u5df2\u652f\u6301\u88ab\u6740\u6389\u540e\u5524\u9192)<\/p>\n<\/blockquote>\n<p><!-- \/wp:quote --><\/p>\n<p><!-- wp:heading {\"level\":4} --><\/p>\n<h4>3\u3001\u51c6\u5907<\/h4>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u9996\u5148\u505a\u4e00\u4e9b\u51c6\u5907\u5de5\u4f5c<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u5728target\u7684<strong>Capabilities<\/strong>\u9009\u9879\u4e2d\u6253\u5f00<strong>Background Modes<\/strong>&nbsp;\u5e76\u52fe\u9009<strong>Location updates<\/strong><\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u7136\u540e\u5728plist\u4e2d\u6dfb\u52a0<strong>NSLocationAlawaysUsageDescription<\/strong>\u7684\u952e \u5728value\u4e2d\u968f\u4fbf\u952e\u5165\u4efb\u4f55\u5185\u5bb9<br \/><img decoding=\"async\" src=\"http:\/\/7xibqv.com1.z0.glb.clouddn.com\/2015-07-21-how-to-deal-with-background-location-update2.png\" alt=\"\"><br \/>\u5b8c\u6210\u8fd9\u4e24\u6b65 \u6211\u4eec\u7684\u524d\u671f\u5de5\u4f5c\u5c31\u5b8c\u6210\u4e86&nbsp;<strong>Background Modes<\/strong>\u662fiOS7\u5e26\u5165\u7684\u65b0\u529f\u80fd \u800c<strong>NSLocationAlawaysUsageDescription<\/strong>\u4e3a\u4e86\u589e\u5f3a\u6743\u9650\u673a\u5236\u5f15\u5165\u7684\u63d0\u793a\u63cf\u8ff0 \u4e0d\u6dfb\u52a0\u8fd9\u4e2a\u7684\u8bdd \u5b9a\u4f4d\u529f\u80fd\u53ef\u662f\u4f7f\u7528\u4e0d\u4e86\u7684 \u4e4b\u540e\u6211\u4eec\u8981\u8c03\u7528<code>requestAlwaysAuthorization<\/code>&nbsp;\u63d0\u793a\u4e2d\u5c31\u4f1a\u663e\u793a\u8fd9\u4e2a\u63cf\u8ff0<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528<code>requestAlwaysAuthorization<\/code>\u800c\u4e0d\u4f7f\u7528<code>requestWhenInUseAuthorization<\/code>\u5462<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u7b80\u5355\u6765\u8bf4 \u4e24\u8005\u7684\u533a\u522b\u5728\u4e8e\u524d\u8005\u652f\u6301<strong>Region Monitoring<\/strong>\u548c<strong>Significant Location Changes<\/strong>\u800c\u540e\u8005\u4e0d\u652f\u6301<br \/>\u4f46\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u533a\u522b \u5c31\u662f\u524d\u8005<strong>\u652f\u6301\u7a0b\u5e8f\u88ab\u6740\u6389\u540e\u7684\u7cfb\u7edf\u81ea\u52a8\u5524\u9192<\/strong>&nbsp;\u800c\u540e\u8005\u4e0d\u652f\u6301 \u8fd9\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u533a\u522b<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:heading {\"level\":4} --><\/p>\n<h4>4\u3001\u4ee3\u7801<\/h4>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u5b9a\u4f4d\u80af\u5b9a\u8981\u8ddf<strong>CLLocationManager<\/strong>\u6253\u4ea4\u9053 \u6240\u4ee5\u6211\u4eec\u5148\u5b9a\u4e49\u4e00\u4e2a<strong>CLLocationManager<\/strong>\u7684\u5b50\u7c7b \u5e76\u6839\u636e\u9700\u6c42\u4e2d\u7684\u51e0\u70b9\u5b9a\u4e49\u4e09\u4e2a\u53d8\u91cf<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:html --><\/p>\n<pre class=\"prettyprint linenums\">@interface MMLocationManager : CLLocationManager\n\n+ (instancetype)sharedManager;\n\n@property (nonatomic, assign) CGFloat minSpeed;     \/\/\u6700\u5c0f\u901f\u5ea6\n@property (nonatomic, assign) CGFloat minFilter;    \/\/\u6700\u5c0f\u8303\u56f4\n@property (nonatomic, assign) CGFloat minInteval;   \/\/\u66f4\u65b0\u95f4\u9694\n\n@end\n<\/pre>\n<p><!-- \/wp:html --><\/p>\n<p><!-- wp:list --><\/p>\n<ul>\n<li><strong>minSpeed<\/strong>&nbsp;\u5982\u679c\u5f53\u524d\u8fd0\u52a8\u901f\u5ea6\u5927\u4e8e\u6b64\u503c \u5219\u6ee1\u8db3\u9700\u6c42(1) \u4ee5\u65f6\u95f4\u4e3a\u66f4\u65b0\u4f9d\u636e(minFilter) \u5982\u679c\u5f53\u524d\u8fd0\u52a8\u901f\u5ea6\u5c0f\u4e8e\u6b64\u503c \u5219\u6ee1\u8db3\u9700\u6c42(2) \u4ee5\u8303\u56f4\u4e3a\u66f4\u65b0\u4f9d\u636e(minInteval)<\/li>\n<li><strong>minFilter<\/strong>&nbsp;\u6700\u5c0f\u7684\u89e6\u53d1\u8303\u56f4 \u7528\u4e8e\u9700\u6c42(1)<\/li>\n<li><strong>minInteval<\/strong>&nbsp;\u66f4\u65b0\u95f4\u9694 \u7528\u4e8e\u9700\u6c42(2)<\/li>\n<\/ul>\n<p><!-- \/wp:list --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u63a5\u4e0b\u6765\u662f\u521d\u59cb\u5316\u51fd\u6570<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:html --><\/p>\n<pre class=\"prettyprint linenums\">- (instancetype)init\n{\n    self = [super init];\n    if ( self )\n    {\n        self.minSpeed = 3;\n        self.minFilter = 50;\n        self.minInteval = 10;\n\n        self.delegate = self;\n        self.distanceFilter  = self.minFilter;\n        self.desiredAccuracy = kCLLocationAccuracyBest;\n    }\n    return self;\n}\n<\/pre>\n<p><!-- \/wp:html --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u8fd9\u91cc\u7684\u9ed8\u8ba4\u503c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6765\u8c03\u6574<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u7136\u540e\u662f\u4f4d\u7f6e\u66f4\u65b0\u540e\u7684\u5904\u7406\u903b\u8f91 \u5176\u5b9e\u4e5f\u975e\u5e38\u7684\u7b80\u5355<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:html --><\/p>\n<pre class=\"prettyprint linenums\">- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations\n{\n    CLLocation *location = locations[0];\n\n    NSLog(@&quot;%@&quot;,location);\n\n    \/\/\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u6765\u8c03\u6574\u89e6\u53d1\u8303\u56f4\n    [self adjustDistanceFilter:location];\n\n    \/\/\u4e0a\u4f20\u6570\u636e\n    [self uploadLocation:location];\n}\n<\/pre>\n<p><!-- \/wp:html --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u800c\u8fd9\u4e2a<strong>adjustDistanceFilter<\/strong>\u51fd\u6570 \u5c31\u662f\u6574\u4e2a\u4ee3\u7801\u7684\u6838\u5fc3 \u4f1a\u6839\u636e\u5f53\u524d\u901f\u5ea6\u6765\u52a8\u6001\u7684\u8c03\u6574<strong>distanceFilter<\/strong>\u8fd9\u4e2a\u53c2\u6570 \u4ee5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:html --><\/p>\n<pre class=\"prettyprint linenums\">\n\/**\n*  \u89c4\u5219: \u5982\u679c\u901f\u5ea6\u5c0f\u4e8eminSpeed m\/s \u5219\u628a\u89e6\u53d1\u8303\u56f4\u8bbe\u5b9a\u4e3a50m\n*  \u5426\u5219\u5c06\u89e6\u53d1\u8303\u56f4\u8bbe\u5b9a\u4e3aminSpeed*minInteval\n*  \u6b64\u65f6\u82e5\u901f\u5ea6\u53d8\u5316\u8d85\u8fc710% \u5219\u66f4\u65b0\u5f53\u524d\u7684\u89e6\u53d1\u8303\u56f4(\u8fd9\u91cc\u9650\u5236\u662f\u56e0\u4e3a\u4e0d\u80fd\u4e0d\u505c\u7684\u8bbe\u7f6edistanceFilter,\n*  \u5426\u5219uploadLocation\u4f1a\u4e0d\u505c\u88ab\u89e6\u53d1)\n*\/\n- (void)adjustDistanceFilter:(CLLocation*)location\n{\n\/\/    NSLog(@&quot;adjust:%f&quot;,location.speed);\n    \n    if ( location.speed &lt; self.minSpeed )\n    {\n        if ( fabs(self.distanceFilter-self.minFilter) &gt; 0.1f )\n        {\n            self.distanceFilter = self.minFilter;\n        }\n    }\n    else\n    {\n        CGFloat lastSpeed = self.distanceFilter\/self.minInteval;\n        \n        if ( (fabs(lastSpeed-location.speed)\/lastSpeed &gt; 0.1f) || (lastSpeed &lt; 0) )\n        {\n            CGFloat newSpeed  = (int)(location.speed+0.5f);\n            CGFloat newFilter = newSpeed*self.minInteval;\n            \n            self.distanceFilter = newFilter;\n        }\n    }\n}\n<\/pre>\n<p><!-- \/wp:html --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u8fd9\u91cc\u8981\u6ce8\u610f\u5230\u7684\u662f<strong>distanceFilter<\/strong>\u8fd9\u4e2a\u53c2\u6570\u4e0d\u80fd\u4e00\u76f4\u8fdb\u884c\u8bbe\u7f6e \u56e0\u4e3a\u6bcf\u6b21\u8bbe\u7f6e\u5b8c\u4ee5\u540e \u518d\u63a5\u4e0b\u6765\u7684\u4e00\u79d2\u4ee5\u540e \u4f1a\u7acb\u5373\u89e6\u53d1<code>didUpdateLocations<\/code>\u56de\u8c03(\u7cfb\u7edf\u7684\u6807\u51c6\u6700\u77ed\u66f4\u65b0\u95f4\u9694\u662f1\u79d2 \u5373\u66f4\u65b0\u9891\u7387\u4e3a1hz) \u6240\u4ee5\u8fd9\u91cc\u53ea\u6709\u5f53\u53d8\u5316\u8d85\u8fc710%\u7684\u65f6\u5019\u624d\u4f1a\u91cd\u7f6e<strong>distanceFilter<\/strong><\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u63a5\u4e0b\u6765 \u4e3a\u4e86\u80fd\u591f\u6b63\u786e\u7684\u5728\u88ab\u6740\u6389\u7684\u60c5\u51b5\u4e0b\u88ab\u5524\u9192 \u6211\u4eec\u8fd8\u8981\u505a\u6700\u540e\u4e00\u6b65\u64cd\u4f5c \u5728<strong>AppDelegate<\/strong>\u7684<code>didFinishLaunchingWithOptions<\/code>\u4e2d\u52a0\u5165\u4e0b\u9762\u7684\u4ee3\u7801<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:html --><\/p>\n<pre class=\"prettyprint linenums\">if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {\n    \n#if __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= 80000\n        [[MMLocationManager sharedManager] requestAlwaysAuthorization];\n#endif\n        \n\n    \/\/\u8fd9\u662fiOS9\u4e2d\u9488\u5bf9\u540e\u53f0\u5b9a\u4f4d\u63a8\u51fa\u7684\u65b0\u5c5e\u6027 \u4e0d\u8bbe\u7f6e\u7684\u8bdd \u53ef\u662f\u4f1a\u51fa\u73b0\u9876\u90e8\u84dd\u6761\u7684\u54e6(\u7c7b\u4f3c\u70ed\u70b9\u8fde\u63a5)\n#if __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= 90000\n        [[MMLocationManager sharedManager] setAllowsBackgroundLocationUpdates:YES];\n#endif\n\n    [[MMLocationManager sharedManager] startMonitoringSignificantLocationChanges];\n}\n<\/pre>\n<p><!-- \/wp:html --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u88ab\u6740\u6389\u7684APP \u5728\u540e\u53f0\u88ab\u7cfb\u7edf\u5524\u9192\u65f6&nbsp;<strong>launchOptions<\/strong>\u4f1a\u5305\u542b<strong>UIApplicationLaunchOptionsLocationKey<\/strong>\u5b57\u6bb5\u6765\u8fdb\u884c\u6807\u8bc6 \u8fd9\u65f6\u6211\u4eec\u518d\u91cd\u65b0\u542f\u52a8\u5b9a\u4f4d\u529f\u80fd\u5373\u53ef<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u800c\u4e3a\u4ec0\u4e48\u4f7f\u7528<code>startMonitoringSignificantLocationChanges<\/code>\u800c\u4e0d\u662f<code>startUpdatingLocation<\/code>\u5462 \u5b98\u65b9\u6587\u6863\u4e2d\u6709\u4e00\u53e5\u8bdd<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:html --><\/p>\n<pre class=\"prettyprint linenums\">If your app is terminated either by a user or by the system, the system doesn\u2019t automatically restart your app when new location updates arrive. The only way to have your app relaunched automatically is to use region monitoring or the significant-change location service.\n<\/pre>\n<p><!-- \/wp:html --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>APP\u88ab\u6740\u6389\u540e \u6211\u4eec\u53ea\u80fd\u7528<code>startMonitoringSignificantLocationChanges<\/code>\u4ee3\u66ff<code>startUpdatingLocation<\/code>\u6765\u5b9e\u73b0\u5b9a\u4f4d\u670d\u52a1<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u81f3\u6b64 \u6ee1\u8db3\u6211\u4eec\u9700\u6c42\u7684\u5b9a\u4f4d\u529f\u80fd\u5c31\u5b8c\u6210\u4e86<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:heading {\"level\":4} --><\/p>\n<h4>5\u3001\u8003\u8651<\/h4>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3 id=\"\u4e3a\u4ec0\u4e48\u4e0d\u7528\u5b9a\u65f6\u5668\u6765\u63a7\u5236\u5b9a\u4f4d\u95f4\u9694\">\u4e3a\u4ec0\u4e48\u4e0d\u7528\u5b9a\u65f6\u5668\u6765\u63a7\u5236\u5b9a\u4f4d\u95f4\u9694<\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u7f51\u4e0a\u6709\u5f88\u591a\u6559\u7a0b\u662f\u7528NSTimer\u6765\u5b9e\u73b0\u7684 \u4f46\u662f\u5176\u5b9e\u8fd9\u6837\u4e0d\u662f\u5f88\u597d \u867d\u7136\u5b9a\u4f4d\u7684\u95f4\u9694\u662f\u56fa\u5b9a\u7684 \u4f46\u662f\u8017\u7535\u7684\u95ee\u9898\u65e0\u6cd5\u89e3\u51b3 \u540e\u53f0\u4f1a\u6301\u7eed\u7684\u66f4\u65b0\u5b9a\u4f4d \u65e0\u8bba\u5f53\u524d\u7684\u4f4d\u7f6e\u662f\u5426\u5728\u66f4\u65b0 \u5f53\u7136 \u5982\u679c\u4f60\u7684\u4f7f\u7528\u573a\u666f\u5c31\u662f\u8981\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u6765\u4e0a\u4f20 \u5c31\u53ef\u4ee5\u4f7f\u7528\u5b9a\u65f6\u5668\u6765\u5904\u7406<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3 id=\"\u4f7f\u7528distanceFilter\u6765\u5904\u7406_\u4f1a\u6709\u4e9b\u4ec0\u4e48\u95ee\u9898\">\u4f7f\u7528distanceFilter\u6765\u5904\u7406 \u4f1a\u6709\u4e9b\u4ec0\u4e48\u95ee\u9898<\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u7531\u4e8edistanceFilter=currentSpeed*minInteval \u90a3\u4e48\u95f4\u9694\u7684\u65f6\u95f4\u56e0\u4e3a\u901f\u5ea6\u7684\u53d8\u5316\u800c\u4f1a\u6709\u6ce2\u52a8 \u4f46\u662f\u8fd9\u4e2a\u6ce2\u52a8\u662f\u5728\u53ef\u63a5\u53d7\u8303\u56f4\u7684 \u5982\u679c\u901f\u5ea6\u52a0\u5feb\u6216\u8005\u53d8\u6162 \u90a3\u4e48\u4e0b\u4e00\u6b21\u7684\u66f4\u65b0\u65f6\u95f4\u5219\u4f1a\u76f8\u5e94\u7684\u7f29\u77ed\u6216\u8005\u53d8\u957f \u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u662f\u5728\u771f\u5b9e\u751f\u6d3b\u73af\u5883\u4e2d \u901f\u5ea6\u7684\u53d8\u5316\u4e0d\u53ef\u80fd\u90a3\u4e48\u5feb \u6240\u4ee5\u8fd9\u4e2a\u8bef\u5dee\u662f\u53ef\u4ee5\u63a5\u53d7\u7684 \u53e6\u5916\u6211\u4eec\u5bf9distanceFilter\u9488\u5bf9\u901f\u5ea6\u8fdb\u884c\u77eb\u6b63 \u56e0\u800c\u603b\u4f53\u6765\u8bf4 \u95f4\u9694\u8fd8\u662f\u4f1a\u4fdd\u6301\u5728\u6211\u4eec\u4e0e\u5176\u7684\u8303\u56f4\u5185\u7684<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:heading {\"level\":3} --><\/p>\n<h3 id=\"\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528allowDeferredLocationUpdatesUntilTraveled:timeout:\">\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528<strong>allowDeferredLocationUpdatesUntilTraveled:timeout:<\/strong><\/h3>\n<p><!-- \/wp:heading --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/CoreLocation\/Reference\/CLLocationManager_Class\/index.html#\/\/apple_ref\/occ\/instm\/CLLocationManager\/allowDeferredLocationUpdatesUntilTraveled:timeout:\" target=\"_blank\">allowDeferredLocationUpdatesUntilTraveled<\/a>\u662fiOS6\u63a8\u51fa\u7684\u4e00\u4e2a\u65b0\u7684API \u770b\u540d\u5b57\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5ef6\u8fdf\u4f4d\u7f6e\u66f4\u65b0 \u76f4\u5230\u79fb\u52a8\u4e86xx\u7c73\u6216\u8005\u65f6\u95f4\u8d85\u8fc7\u4e86xx\u79d2 \u90a3\u4e48\u8fd9\u4e2a\u51fd\u6570\u4e0d\u6b63\u597d\u6ee1\u8db3\u4e86\u6211\u4eec\u7684\u6240\u6709\u8981\u6c42\u4e48? \u53ef\u662f\u4e07\u4e07\u6ca1\u60f3\u5230 \u4e8b\u60c5\u5e76\u4e0d\u662f\u8fd9\u6837\u7684 \u8fd9\u4e2a\u51fd\u6570\u5e76\u4e0d\u597d\u7528<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u4e3a\u4ec0\u4e48\u8bf4\u8fd9\u4e2a\u51fd\u6570\u4e0d\u597d\u7528\u5462? \u9996\u5148 \u8fd9\u4e2a\u51fd\u6570\u7684\u8981\u6c42\u5f88\u591a \u6211\u4eec\u6765\u770b\u770b\u8981\u8fd9\u4e2a\u51fd\u6570\u8d77\u4f5c\u7528\u8981\u6ee1\u8db3\u54ea\u4e9b\u6761\u4ef6<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:list --><\/p>\n<ul>\n<li>\u5fc5\u987biPhone5\u4ee5\u53ca\u4e4b\u540e\u7684\u786c\u4ef6\u8bbe\u5907\u624d\u652f\u6301<\/li>\n<li>desiredAccuracy\u5fc5\u987b\u8bbe\u7f6e\u4e3a<code>kCLLocationAccuracyBest<\/code>\u6216\u8005<code>kCLLocationAccuracyBestForNavigation<\/code><\/li>\n<li>distanceFilter\u5fc5\u987b\u8bbe\u7f6e\u4e3a<code>kCLDistanceFilterNone<\/code><\/li>\n<li>\u53ea\u5728APP\u8fd0\u884c\u5728\u540e\u53f0\u65f6\u751f\u6548 \u524d\u53f0\u8fd0\u884c\u65f6\u662f\u4e0d\u4f1a\u8fdb\u884c\u5ef6\u8fdf\u5904\u7406\u7684<\/li>\n<li>\u53ea\u6709\u7cfb\u7edf\u5728<strong>\u4f4e\u529f\u8017(Low Power State)<\/strong>\u7684\u65f6\u5019<strong>\u624d\u6709\u53ef\u80fd<\/strong>\u751f\u6548<\/li>\n<\/ul>\n<p><!-- \/wp:list --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u5173\u4e8e<strong>Low Power State<\/strong>\u5728iOS\u4e2d\u7684\u63cf\u8ff0 \u6211\u53ea\u5728\u82f9\u679c\u5b98\u7f51\u7684\u6587\u6863\u4e2d\u627e\u5230<a href=\"https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/Performance\/Conceptual\/EnergyGuide-iOS\/FundamentalConcepts.html#\/\/apple_ref\/doc\/uid\/TP40015243-CH4-SW1\" target=\"_blank\" rel=\"noreferrer noopener\">\u90e8\u5206\u5b9a\u4e49<\/a><\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:quote --><\/p>\n<blockquote class=\"wp-block-quote\">\n<p>iOS is very good at getting a device into a low power state when it\u2019s not being used. At idle, very little power is drawn and energy impact is low. When tasks are actively occurring, system resources are being used and those resources require energy. However, sporadic tasks can cause the device to enter an intermediate state\u2014neither idle nor active\u2014when the device isn\u2019t doing anything. There may not be enough time during these intermediate states for the device to reach absolute idle before the next task executes. When this occurs, energy is wasted and the user\u2019s battery drains faster.<\/p>\n<\/blockquote>\n<p><!-- \/wp:quote --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u636e\u6211\u7b80\u5355\u7684\u4e86\u89e3 \u8fd9\u4e2a**Low Power State\u201d\u53ea\u6709\u5728\u9ed1\u5c4f\u7684\u72b6\u6001\u4e0b(\u4e0d\u53ea\u662f\u9501\u5c4f)\u624d\u6709\u53ef\u80fd\u89e6\u53d1 \u53ea\u8981\u6709\u4efb\u4f55\u7535\u91cf\u5c4f\u5e55\u7684\u64cd\u4f5c(\u5c31\u8fde\u63a8\u9001\u4e5f\u7b97) \u90fd\u4f1a\u4f7fAPP\u9000\u51fa\u8fd9\u4e2a\u72b6\u6001 \u540c\u65f6 \u5982\u679c\u5728\u5145\u7535\u72b6\u6001\u4e0b \u4e5f\u662f\u65e0\u6cd5\u8fdb\u5165\u7684<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u6211\u5c1d\u8bd5\u5728\u771f\u673a\u548c\u6a21\u62df\u5668\u4e0a\u4f7f\u7528\u8fd9\u4e2aAPI \u4f46\u7ed3\u679cAPP\u8fd8\u662f\u4ee51HZ\u7684\u9891\u7387\u5728\u5b9a\u4f4d(\u8bbe\u7f6e\u4e86kCLDistanceFilterNone\u7684\u539f\u56e0)<br \/>\u867d\u7136<code>locationManager:didFinishDeferredUpdatesWithError:<\/code>\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u6210\u529f\u7684\u56de\u8c03\u4e86 \u4f46\u662f\u7ed3\u679c\u8fd8\u662f\u6ca1\u6709deffer \u4e8e\u662f\u6211\u67e5\u4e86\u4e00\u4e0b \u539f\u6765\u8fd9\u4e2a\u51fd\u6570\u65e0\u6cd5\u76f4\u63a5\u8fdb\u884c\u8c03\u8bd5\u7684 \u56e0\u4e3a:<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n<p><!-- wp:list --><\/p>\n<ul>\n<li>\u4e0d\u652f\u6301\u6a21\u62df\u5668&nbsp;<code>deferredLocationUpdatesAvailable<\/code>\u7528\u4e8e\u68c0\u6d4b\u8bbe\u5907\u662f\u5426\u652f\u6301 \u6a21\u62df\u5668\u4f1a\u8fd4\u56deNO<\/li>\n<li>\u4e0d\u652f\u6301\u771f\u673a\u8c03\u8bd5 \u56e0\u4e3a\u8c03\u8bd5\u65f6Xcode\u4f1a\u963b\u6b62\u7a0b\u5e8f\u4f11\u7720 \u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u8fdb\u5165\u4f4e\u529f\u8017\u72b6\u6001<\/li>\n<\/ul>\n<p><!-- \/wp:list --><\/p>\n<p><!-- wp:paragraph --><\/p>\n<p>\u7ed3\u8bba\u5c31\u662f\u2026\u8fd9\u4e2a\u4e1c\u897f\u8fde\u8c03\u8bd5\u90fd\u6ca1\u529e\u6cd5 \u6240\u4ee5\u6211\u4e5f\u6ca1\u6709\u90a3\u4e48\u591a\u65f6\u95f4\u8dd1\u5230\u5916\u9762\u53bb\u6d4b\u8bd5\u8fd9\u4e2a\u4e1c\u897f\u2026 \u51b5\u4e14\u4f7f\u7528\u6211\u4e0a\u8ff0\u7684\u65b9\u6cd5\u5df2\u7ecf\u57fa\u672c\u53ef\u4ee5\u6ee1\u8db3\u9700\u6c42\u2026\u6240\u4ee5\u6211\u5df2\u653e\u5f03\u7ee7\u7eed\u7814\u7a76\u8fd9\u4e2aAPI\u4e86\u2026<\/p>\n<p><!-- \/wp:paragraph --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u524d\u8a00 \u76ee\u524d\u5728\u505a\u7684\u7269\u6d41\u9879\u76ee\uff0c\u53f8\u673a\u7aef\u7684\u540e\u53f0\u5b9e\u65f6\u4e0a\u4f20\u4f4d\u7f6e\u5219\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u6280\u672f\u70b9 \u63a5\u4e0b\u6765\u5c31\u6765\u8bf4\u8bf4\u6211\u5173\u4e8e\u8fd9\u65b9\u9762\u7684\u5b9e\u8df5\u7ecf\u9a8c\u3002 2\u3001\u9700\u6c42 1. \u5982\u679c\u7528\u6237\u7684\u4f4d\u7f6e\u5728\u6301\u7eed\u53d8\u5316 \u5219\u9694\u4e00\u6bb5\u65f6\u95f4\u4e0a\u62a5\u4e00\u6b21\u3000\u3000\u7531\u4e8e\u6211\u4eec\u5e0c\u671b\u80fd\u591f\u5b9e\u65f6\u7684\u5c06\u7528\u6237\u7684\u4f4d\u7f6e\u53d8\u5316\u53cd\u9988\u5728APP\u91cc \u6240\u4ee5\u5b9a\u65f6\u7684\u4e0a\u62a5\u662f\u521a\u9700 2. \u5982\u679c\u7528\u6237\u7684\u79fb\u52a8\u901f\u5ea6\u5f88\u6162 \u5219\u9694\u4e00\u6bb5\u8ddd\u79bb\u4e0a\u62a5\u4e00\u6b21\u3000\u3000\u5982\u679c\u7528\u6237\u662f\u4f4e\u901f\u7387\u7684\u72b6\u6001(\u6bd4\u5982\u6b65\u884c\u7684\u79fb\u52a8\u901f\u5ea6\u5927\u6982\u5c31\u662f1m\/s\u5de6\u53f3) \u8fd9\u4e2a\u65f6\u5019\u5982\u679c\u8fd8\u6309( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":144,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13,14],"_links":{"self":[{"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=\/wp\/v2\/posts\/142"}],"collection":[{"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":3,"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":147,"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions\/147"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=\/wp\/v2\/media\/144"}],"wp:attachment":[{"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.daoyeer.win\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}