您需要在AppDelegate
的界面中設置CLLocationManagerDelegate
。
爲了啓動CLLocationManager來獲取位置,您必須設置CLLocationManager類的一些屬性。對於這種實現下面的代碼片斷:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Location manager settings
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
// Your code for rootViewController settings here
}
如下
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *userLocation = [locations lastObject];
}
的userLocation
對象將是用戶的當前位置然後實現CLLocationManagerDelegate
方法。
您是否在設備或模擬器中運行? – chandru
即時在設備兄弟運行.. – chakshu