我正在開發一個具有許多視圖的應用程序。在我的應用程序中,有時用戶會看到他可以通過點擊按鈕請求他的位置。我試圖遵循Apple的指導方針,只詢問用戶的位置,如果用戶允許的話。我應該做些什麼,將下一個代碼放入應用程序委託中,並將位置管理器屬性聲明到用戶調用的任何視圖中,將位置管理器屬性傳遞給新視圖和舊視圖,並隨時詢問第二個下一個代碼用戶點擊按鈕來定位自己?或者只是使用第二個代碼,將位置管理器屬性僅聲明爲允許用按鈕獲取用戶位置的視圖,以檢查位置服務是否啓用?使用iOS位置管理器
第一個片段。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
[servicesDisabledAlert release];
}
[manager release];
return YES;
}
Second snippet。
- (IBAction)locateUser:(id)sender {
if([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
} else {
[[[[UIAlertView alloc] initWithTitle:@"Location services."
message:@"Location services are disabled."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
}
感謝您的閱讀。
沒有人可以幫助我嗎? – 2010-11-30 15:43:19