您好我在iOS 6/7上获取用户当前位置时遇到问题。我需要用户在应用启动时的位置,但需要6-8秒才能获得用户位置,直到找到该位置,该应用程序不会隐藏启动画面,这对我来说是不行的。(对我而言)。
我想使用dispatch_async(GCD)在后台获取用户位置,但它没有改变一件事:(。所以我想知道有没有办法解决我的问题。我非常感谢任何帮助。提前谢谢。 这是我的应用程序初始化的代码:
- (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];
}
}
---编辑---- 以下是获取当前位置的代码
-(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 {
}
}
答案 0 :(得分:0)
您可以使用dispatch_after
创建延迟,延迟完成后,开始执行需要用户位置的代码。以下是我自己的代码中的示例,其中我将CLLocationManager
子类化,并将更新后的位置存储在私有ivar
中(在我的locationManager
CLLocationManager
委托方法中设置子类):
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
然后可以通过getter(getLastUpdatedUserLocation
)访问更新的位置。
以下是代码:
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);
});
}