重要的位置更改启动应用程序启动

时间:2014-11-24 23:03:57

标签: ios iphone core-location cllocationmanager boot

我有一个成功使用重要位置更改的应用,但在非常特定的情况下启动时出现问题。

  • 如果iPhone在启动后3分钟内解锁,应用就会启动。
  • 但是,如果我等待3分钟以上解锁iPhone,则该应用程序无法启动。 (即使我旅行很远)。

值得注意的是,应用程序li​​fe360和Facebook会重新启动其重要的位置更改 如果手机在3分钟后解锁。解锁手机后,以下定位标签飞过:

Nov 24 16:43:41 my-iPhone locationd[64] <Notice>: client 'com.life360.safetymap' starting significant location changes

我是否可以对我的重要位置更改代码的实施进行一些更改,以便应用程序可靠地在启动时启动(或至少在手机解锁时)?

1)myapp-info.plist文件包含

的条目
  • “应用程序注册所需背景模式的位置更新”
  • 所需设备功能的“location-services”
  • NSLocationAlwaysUsageDescription
  • 的说明文字

2)以下代码是我的AppDelegate.m的摘录。

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    [LogPhoneEvent logPhoneEvent:@"APPLICATION DID FINISH LAUNCHING WITH OPTIONS"];
    [LocationManagement startSignificantChangeUpdates];
    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    #if __has_feature(objc_arc)
            self.window = [[UIWindow alloc] initWithFrame:screenBounds];
    #else
            self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
    #endif
        self.window.autoresizesSubviews = YES;

    #if __has_feature(objc_arc)
            self.viewController = [[MainViewController alloc] init];
    #else
            self.viewController = [[[MainViewController alloc] init] autorelease];
    #endif

        // Set your app's start page by setting the <content src='foo.html' /> tag in config.xml.
        // If necessary, uncomment the line below to override it.
        // self.viewController.startPage = @"index.html";
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];

        // NOTE: To customize the view's frame size (which defaults to full screen), override
        // [self.viewController viewWillAppear:] in your view controller.
        [self.viewController setLaunchOptions:launchOptions];
//    }
    return YES;
}

以下是位置管理文件:
LocationManagement.h

#import <CoreLocation/CoreLocation.h>
#import "LogPhoneEvent.h"

@interface LocationManagement : NSObject <CLLocationManagerDelegate>

+ (void)startSignificantChangeUpdates;
+ (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
+ (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;

@end

和LocationManagement.m

//
//  LocationManagement.m
//

#import "LocationManagement.h"


@implementation LocationManagement

CLLocationManager* locationManager;
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

+ (void)startSignificantChangeUpdates
{
    // Create the location manager if this object does not
    // already have one.
    NSLog(@"LocationManagement attempting startSignificantChangeUpdates");

    if (nil == locationManager){
        locationManager = [[CLLocationManager alloc] init];
        [LogPhoneEvent logPhoneEvent:[NSString stringWithFormat:@"startSignificantChangeUpdates creating new CLLocationManager"]];
    }
    [locationManager setDelegate:(id)self];

    if ([CLLocationManager significantLocationChangeMonitoringAvailable]){
        NSLog(@"LocationManagement significantLocationChangeMonitoring IS AVAILABLE");
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0.0")){
            if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
            {
                [locationManager requestAlwaysAuthorization];
            }
        }
        [LogPhoneEvent logPhoneEvent:[NSString stringWithFormat:@"startSignificantChangeUpdates START MONITORING SIGNIFICANT LOCATION CHANGES"]];
        [locationManager startMonitoringSignificantLocationChanges];
        //[locationManager startUpdatingLocation];
        NSLog(@"my-uBox LocationManagement starting significant change updates");
    }
    else{
        [LogPhoneEvent logPhoneEvent:[NSString stringWithFormat:@"startSignificantChangeUpdates significantLocationChangeMonitoring NOT AVAILABLE"]];
        NSLog(@"my-uBox LocationManagement significantLocationChangeMonitoring NOT AVAILABLE");
    }
}

+(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *newLocation = [locations lastObject];
    [LogPhoneEvent logPhoneEvent:[NSString stringWithFormat:@"locationmanager DID UPDATE LOCATION to %@", newLocation]];
    NSLog(@"LocationManagement didUpdateLocations to %@", newLocation);
}

+(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"LocationManagement didFailWithError %@", error);
    [LogPhoneEvent logPhoneEvent:[NSString stringWithFormat:@"locationmanager DID FAIL WITH ERROR %@", error]];
}

@end

我在Iphone 5上使用iOS 8.1.1并且已经坚持了几天。 任何意见都将不胜感激。

0 个答案:

没有答案