2014-06-28 42 views
0

您好,我在iOS 6/7上获取用户当前位置时遇到问题。我需要用户在应用程序启动时的位置,但需要6-8秒才能获取用户位置,直到检索到位置为止,应用程序不会隐藏启动画面,这对我来说并不好。获取iOS 6/7后台线程中用户的当前位置

我想使用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) ? @"[email protected]" : @"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

http://stackoverflow.com/questions/8854100/async-call-a-method-using-ios-4 –

+0

的可能的复制在这里没有足够的信息。你在initLocationManager方法中做了什么?根据这段代码,启动画面在被呈现后立即被移除,所以你的应用可能被卡在别的地方。你有没有使用仪器工具来看看哪里? – Paulw11

回答

0

代码可以使用dispatch_after来创建一个延迟,和一个在延迟完成后,开始执行需要用户位置的代码。下面是我自己的代码,我在那里继承CLLocationManager和存储在私人ivar更新的位置(这是设置locationManager委托方法里面我CLLocationManager子类)的样本:

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

更新后的位置,然后访问通过吸气剂(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); 
     }); 
    }