繁体   English   中英

每次启动iOS时获取当前位置

[英]Fetch current location on each launch of app ios

我正在开发iOS应用,我想在每次启动应用时加载用户的当前位置。

我已经在didFinishLaunchingWithOptions编写了此代码段,但是当我启动我的应用程序时,它仅一次获取用户位置。 (我正在5s iOS 7中测试我的应用程序)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    locationManager = [[CLLocationManager alloc] init];

                locationManager.delegate = self;
                if(IS_OS_8_OR_LATER){
                    NSUInteger code = [CLLocationManager authorizationStatus];
                    if (code == kCLAuthorizationStatusNotDetermined && ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
                        // choose one request according to your business.
                        if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
                            [locationManager requestAlwaysAuthorization];
                        } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
                            [locationManager  requestWhenInUseAuthorization];
                        } else {
                            NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
                        }
                    }
                }
                [locationManager startUpdatingLocation];


                 ...
                 ...
                 ...
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    CLLocation *currentLocation = newLocation;
    [locationManager stopUpdatingLocation];
}

现在当我回家时,位置已更改,当我尝试打开我的应用程序时,它不会获取家位置,而是仅显示我的旧位置。(我的旧位置和家位置之间有很大差异)

请帮助,并在此先感谢。

我认为您正在寻找applicationWillEnterForegroundapplicationDidBecomeActive ,每次用户打开您的应用程序时都会调用它们(“变为活动状态”)。

在应用程序的首次启动时, didFinishLaunchingWithOptions仅被调用一次。

在docs中 ,状态转换事件概述:

发射时间:

application:willFinishLaunchingWithOptions

application:didFinishLaunchingWithOptions

过渡到前台:

applicationDidBecomeActive

过渡到背景:

applicationDidEnterBackground

过渡到非活动状态

applicationWillResignActive (离开前台状态时调用。)

applicationWillEnterForeground (从后台状态转换出来时调用。)

终止:

applicationWillTerminate (仅在应用程序运行时调用。如果应用程序被挂起,则不会调用此方法。)

applicationDidBecomeActive中编写代码。 每当您有背景时就会调用它。

- (void)applicationDidBecomeActive:(UIApplication *)application {
//Get Current Location
    if ([CLLocationManager locationServicesEnabled])
    {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.pausesLocationUpdatesAutomatically = NO;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];
        }
        [self.locationManager startUpdatingLocation];
    }
    else
    {
        NSLog(@"Location services are not enabled");
    }
}

#pragma mark -- Location Delegate Methods

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];
    if (location != nil)    
        NSLog(@"current lat:%f , currentLong:%f",location.coordinate.latitude,location.coordinate.longitude);

    [manager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"Error = %@",error.localizedDescription);
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        case kCLAuthorizationStatusRestricted:
        case kCLAuthorizationStatusDenied:
        {
            // do some error handling
        }
            break;
        default:{
            [self.locationManager startUpdatingLocation];
        }
            break;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM