菜鸟教程小白 发表于 2022-12-13 14:03:40

ios - CLLocationManager 委托(delegate)方法未在后台调用


                                            <p><p>我正在构建一个应用程序来检查用户是否在我客户的商店附近,如果是,它会向他发送通知。</p>

<p>我也希望应用在后台检查它。</p>

<p>我已将此行添加到我的 <code>info.plist</code> 文件中:</p>

<p> <a href="http://i.stack.imgur.com/UfyS1.png" rel="noreferrer noopener nofollow">image</a> </p>

<p>这是我的代码:</p>

<p><strong>AppDelegate.m</strong>:</p>

<pre><code>-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
;
;

return YES;
}
-(void)configureLocationManager
{
    //Initializing locationManager
    self.locationManager=[ init];
    //setting &#34;locationManager&#34;&#39;s(CLLocationManager) delegate to &#34;self&#34;
    self.locationManager.delegate=self.monitorLocationVC;
    //Setting &#34;locationManager&#34;&#39;s(CLLocationManager)&#39;s distance filter to none
    //self.locationManager.distanceFilter=kCLDistanceFilterNone;
    //Setting &#34;locationManager&#34;&#39;s(CLLocationManager)&#39;s activityType to navigation
    self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;
    //setting &#34;locationManager&#34;&#39;s(CLLocationManager) desiredAccuracy to &#34;best&#34;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    //If OS version is 9 or above - setting &#34;allowsBackgroundLocationUpdates&#34; to YES
    if ([[ systemVersion] floatValue] &gt;= 9) {
      self.locationManager.allowsBackgroundLocationUpdates = YES;
    }
}
</code></pre>

<p><strong>MonitorLocationViewController.m</strong>:</p>

<pre><code>-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray&lt;CLLocation *&gt; *)locations
{
    ; //Not being called in background
}

-(void)checkIfNearStore
{
    for (Store *currentStore in self.allStores) {
      if (&amp;&amp;currentStore.alreadySendNotification==NO) {
            NSLog(@&#34;Entered: %@&#34;,[ address]);
            currentStore.alreadySendNotification=YES;
            ;
      }
    }

    for (Store *currentStore in self.storesAlreadySentNotifications) {
      if (!) {
            currentStore.alreadySendNotification=NO;
      }
    }
}
</code></pre>

<p>有人有想法吗?谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><h2>后台位置更新</h2>

<p>我建议您仔细阅读与 <a href="https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges" rel="noreferrer noopener nofollow">startMonitoringSignificantLocationChanges</a> 相关的讨论。方法(您必须使用此方法来解决问题)。请注意,您可以通过调用 <strong>stopMonitoringSignificantLocationChanges</strong> 然后再次调用 start... 方法来强制进行新更新。您还需要在 locationManager:didUpdateLocations 方法中监控时间戳。</p>

<p>Apple 详细说明了当应用处于后台时获取更新所需的步骤。基本上你必须在你的应用委托(delegate)方法中截取一个键。这个<a href="https://stackoverflow.com/questions/27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended" rel="noreferrer noopener nofollow">SO question</a>有示例代码。 </p>

<p>但是在您跳到代码之前,请按照建议查看讨论,因为它会让您更清楚地了解如何掌握这种行为。与往常一样,您也总是希望监控讨论中提到的失败委托(delegate)方法。</p>

<h2>苹果<a href="https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges" rel="noreferrer noopener nofollow">Documentation</a> </h2>

<p> <a href="/image/ucnH7.png" rel="noreferrer noopener nofollow"><img src="/image/ucnH7.png" alt="significantlocationchanges"/></a> </p>

<h2>Nota Bene</h2>

<p>Apple 已发布此非常重要的说明,说明位置更新发送到设备的频率限制。为了节省电池,他们尝试限制更新次数,直到设备移动 500 米。您的测试应考虑这些限制。</p>

<p> <a href="/image/v12lS.png" rel="noreferrer noopener nofollow"><img src="/image/v12lS.png" alt="500meters"/></a> </p>

<p><strong>资源</strong></p>

<p>有关更多补充信息,请注意以下对这些 SO 问题的回答:</p>

<p> <a href="https://stackoverflow.com/questions/35160033/cllocationmanager-monitoredregions-nsset-is-not-correct-or-maybe-something-el/35161986#35161986" rel="noreferrer noopener nofollow">#1</a>
<a href="https://stackoverflow.com/questions/27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended" rel="noreferrer noopener nofollow">#2</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CLLocationManager 委托(delegate)方法未在后台调用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35171872/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35171872/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CLLocationManager 委托(delegate)方法未在后台调用