2014-06-28 77 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); 
     }); 
    }