【问题标题】:iOS Background app : best way to run appiOS后台应用程序:运行应用程序的最佳方式
【发布时间】:2014-04-13 10:14:10
【问题描述】:

我需要创建一个在后台模式下检索 iOS 无线电信息(rssi、bearer、...)的应用程序;该应用程序将在 App Store 上提供,因此符合 App Store 指南。 应用应合理保留手机电量。

我目前同时使用 BackgroundFetch 和 GPS,效果很好,但是当 iOS 内存不足时,应用程序会被操作系统杀死。

我还查看了使用 GPS 并保持 iPhone 电池寿命的 Google 应用 (Google Now),但我不知道他们是如何做到的。

谢谢

【问题讨论】:

    标签: ios iphone background gps mode


    【解决方案1】:

    我对上面的代码进行了测试。

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        //NSLog(@"Location: %f, %f", newLocation.coordinates.longtitude, newLocation.coordinates.latitude);
        longitude = newLocation.coordinate.longitude;
        latitude = newLocation.coordinate.latitude;
        gpsTimestamp = [newLocation.timestamp timeIntervalSince1970];
        NSLog(@"Changed ! ");
        if (!timer &&  ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)) {
            NSLog(@"Starting task");
            timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(createBgIndicator) userInfo:nil repeats:YES];
    
        }
    
    }
    

    在这种情况下,计时器只会在我移动时启动并执行 5 次(10 秒)。我想让计时器一直运行。

    【讨论】:

      【解决方案2】:
      1. 确保在您的 Plist 中启用“后台获取”和“位置更新”。
      2. 当您的应用程序在后台时,用户 startMonitoringSignificantLocationChanges 而不是 startUpdatingLocation。以下是文档中的一些信息。

      “[startUpdatingLocation] 通常需要启用位置跟踪硬件更长的时间,这可能会导致更高的功耗。” “[使用 startMonitoringSignificantLocationChanges] 应用程序可以在设备从之前的通知移动 500 米或更远时收到通知。它的通知频率不应超过每五分钟一次。”

      我会在这里阅读文档,以便您更好地理解 (https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges)

      如您所见,使用 startMonitoringSignificantLocationChanges 可显着延长电池寿命。以下是我在 AppDelegate.m 中实现它的方式:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          ...
          [self.locationManager startMonitoringSignificantLocationChanges];
          ...
      }
      
      - (void)applicationDidEnterBackground:(UIApplication *)application {
          ...
          [self.locationManager stopUpdatingLocation];
          [self.locationManager startMonitoringSignificantLocationChanges];
          ...
      }
      
      - (void)applicationWillEnterForeground:(UIApplication *)application {
          ...
          [self.locationManager stopMonitoringSignificantLocationChanges];
          [self.locationManager startUpdatingLocation];
          ...
      }
      
      -(CLLocationManager *)locationManager{
      
          if(!_locationManager){
              _locationManager = [[CLLocationManager alloc]init];
              _locationManager.delegate = self;
              _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
              _locationManager.distanceFilter = 5;
          }
          return _locationManager;
      }
      

      这样我确保我只在应用程序处于活动状态时使用耗电的“startUpdatingLocation”方法,而在非活动时使用省电的“stopMonitoringSignificantLocationChanges”。

      【讨论】:

      • 我将尝试这种 GPS 方法。我已经在我的应用程序中使用了这种方法,效果很好。但在这种情况下,我需要每秒钟保持一个计时器更新。我还尝试将后台 ExpirationHandler 与 GPS 结合使用,但每次系统需要内存时应用程序都会被终止。测试代码后我会接受答案:) 谢谢
      • 如果您使用 stArtMonitoringSignificantLocationUpdates,您不必担心您的应用会被杀死。这是文档中的另一行“如果您启动此服务并且您的应用程序随后终止,如果有新事件到达,系统会自动将应用程序重新启动到后台。”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多