菜鸟教程小白 发表于 2022-12-11 16:38:06

ios(swift)在后台(ios 9.3.2)位置停止工作


                                            <p><p>我不确定这是错误还是某种配置问题,但是(但我将每个选项都放在 info.plist、locationManager.allowsBackgroundLocationUpdates = true 以及我在网上找到的所有其他提示中,但问题仍然存在) </p>

<p>locationManager(经理:CLLocationManager,didUpdateLocations 位置:){
}</p>

<p>在后台(应用关闭)一段时间后(随机)停止更新位置。在 iphone 4s ios 8.4 上不存在此问题。</p>

<p>也许其他人解决了这个问题?
谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我解决了它每 3 分钟重新启动一次位置管理器:</p>

<p>在你的委托(delegate)方法中(在我的例子中是 <code>locationDidUpdateToLocation</code>)放一些类似</p>的东西

<pre><code>   self.timeElapsedSinceLastTrackingUpdate = NSDate().timeIntervalSinceDate(self.lastUpdatedTime)
   if timeElapsedSinceLastTrackingUpdate &gt; 180 {
      self.lastUpdatedTime = NSDate()         
      self.LocationMgr.stopUpdatingLocation()
      UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})               
      NSTimer.scheduledTimerWithTimeInterval(180, target: self.LocationMgr, selector: #selector(self.LocationMgr.startUpdatingLocation), userInfo: nil, repeats: false)
   }
</code></pre>

<p>还要确保您的 <code>Info.plist</code></p> 中有两个键(<code>NSLocationAlwaysUsageDescription</code> 和 <code>NSLocationWhenInUseUsageDescription</code>)

<pre><code>    &lt;key&gt;NSLocationAlwaysUsageDescription&lt;/key&gt;
    &lt;string&gt;This app needs to use your location&lt;/string&gt;

    &lt;key&gt;NSLocationWhenInUseUsageDescription&lt;/key&gt;
    &lt;string&gt;This app needs to use your location&lt;/string&gt;
</code></pre>

<p>最后,请务必在位置管理器上设置这些变量:</p>

<p><code>pausesLocationUpdatesAutomatically = false</code> </p>

<p>和</p>

<pre><code>allowsBackgroundLocationUpdates = true
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios(swift)在后台(ios 9.3.2)位置停止工作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37571122/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37571122/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios(swift)在后台(ios 9.3.2)位置停止工作