首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS 6/7的后台线程中获取用户的当前位置

在iOS 6/7的后台线程中获取用户的当前位置
EN

Stack Overflow用户
提问于 2014-06-29 02:33:43
回答 1查看 195关注 0票数 0

你好,我有一个问题,在iOS 6/7上获取用户的当前位置。我需要用户在应用程序启动时的位置,但它需要6-8秒来获得用户的位置,直到位置被检索到应用程序不会隐藏闪屏,这是不好(对我)。

我想使用dispatch_async (GCD)在后台获取用户的位置,但这并没有改变任何事情:( .所以我想知道有没有办法解决我的问题。我真的很感谢你的帮助。提前谢谢。以下是我的应用程序的初始化代码:

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

FinalMenuViewController *leftMenuViewController = [[FinalMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
                                                containerWithCenterViewController:[self navigationController]
                                                leftMenuViewController:leftMenuViewController
                                                rightMenuViewController:nil];
container.panMode = MFSideMenuPanModeNone;
self.window.rootViewController = container;

if(([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)){
    [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
}else{
    [[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:(250.0/255.0) green:(139.0/255.0) blue:(40.0/255.0) alpha:1.0]];
}

[self.window makeKeyAndVisible];

[self displaySplashImage];

[self setDefaultData];

[self initLocationManager];

return YES;
}

-(void) displaySplashImage{

CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;

UIImage* myImage;
NSString *imgName = ([[UIScreen mainScreen] bounds].size.height == 568) ? @"Default-568h@2x.png" : @"Default.png";
myImage = [UIImage imageNamed:imgName];
self.splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];

mainV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, screenHeight)];
[mainV addSubview:self.splashView];
[mainV setBackgroundColor: [UIColor clearColor]];

self.splashView.image = myImage;
self.splashView.contentMode = UIViewContentModeScaleAspectFit;

[self.window addSubview:mainV];
[self.window bringSubviewToFront:mainV];
[self removeImageView];

}
-(void) removeImageView{
if(mainV){
    [mainV removeFromSuperview];
}
}

- EDIT -以下是获取当前位置的代码

代码语言:javascript
复制
-(void) initManager{
locManager_ = [[CLLocationManager alloc] init];
locManager_.delegate = self;
locManager_.distanceFilter = 50;
locManager_.desiredAccuracy = kCLLocationAccuracyBest;
[locManager_ startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){
CLLocation *loc = [locations lastObject];
GlobalVariables *vars = [GlobalVariables getInstance];
vars.currentLocatoin = loc;

}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if ([error domain] == kCLErrorDomain) {
    switch ([error code]) {
        case kCLErrorDenied:{
            NSString *msg = @"ERROR_MSG_HERE";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            break;

        }
        case kCLErrorLocationUnknown:{
            NSString *msg = @"We were unable to find your location";
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            break;
        }

        default:
            break;
    }
} else {

}
}
EN

回答 1

Stack Overflow用户

发布于 2017-07-07 22:44:27

您可以使用dispatch_after创建延迟,在延迟结束后,开始执行需要用户位置的代码。下面是我自己的代码中的一个示例,其中我继承了CLLocationManager,并将更新后的位置存储在一个私有ivar中(它是在我的CLLocationManager子类中的locationManager委托方法中设置的):

代码语言:javascript
复制
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

然后,可以通过getter (getLastUpdatedUserLocation)访问更新后的位置。

代码如下:

代码语言:javascript
复制
UserLocationManager* userLocationManager = [UserLocationManager sharedLocationManager];

    [userLocationManager requestAuthorizationAndStartUpdates];

    __block CLLocation* userLocation = [userLocationManager getLastUpdatedUserLocation];

    if(userLocation == nil){

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 6.00*NSEC_PER_SEC), dispatch_get_main_queue(), ^{

            userLocation = [userLocationManager getLastUpdatedUserLocation];


            NSLog(@"The user location after 6.00 secondss is lat: %f, long: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        });
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24469789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档