2011-11-24 103 views
5

當我在iphone中安裝我的應用程序並首次運行時,它會詢問用戶對核心位置服務的權限。這裏是模擬器的圖像。iPhone:核心位置彈出問題

在我的應用程序中,我的第一個應用程序視圖需要當前位置,並根據位置列出了一些事件。如果應用程序無法獲取位置,則會顯示默認的事件列表。

所以,我想知道是否有可能持有應用程序流程,直到用戶點擊「Don't allow」或「ok」按鈕?
我知道如果用戶點擊「不允許」,那麼kCLErrorDenied錯誤將被解僱。

當前會發生什麼,如果用戶沒有點擊任何按鈕,應用程序將顯示帶有默認列表(無位置)的列表頁面。之後,如果用戶點擊「ok」按鈕,則沒有任何反應!如何在「ok」按鈕點擊後刷新頁面?

謝謝...。

enter image description here

回答

1

是的,只是不直到調用這些委託方法做任何事情。當他們點擊「確定」時,這只是Cocoa的一個信號,然後嘗試檢索用戶的位置 - 您應該構建應用程序,以便在CLLocationManager有位置或無法獲取位置時,您的應用程序會繼續。

你不會說,暫停你的應用程序,直到位置返回/失敗;這不是面向對象的開發。

0

在您的視圖邏輯中等待,直到調用didUpdateToLocation或didFailWithError的CoreLocation委託。讓這些方法調用/ init你的列表和UI數據填充。

樣品控制器:

部首

@interface MyCLController : NSObject <CLLocationManagerDelegate> { 
    CLLocationManager *locationManager; 
} 

@property (nonatomic, retain) CLLocationManager *locationManager; 

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

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error; 

@end 

代碼

#import "MyCLController.h" 

@implementation MyCLController 

@synthesize locationManager; 

- (id) init { 
    self = [super init]; 
    if (self != nil) { 
     self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     self.locationManager.delegate = self; // send loc updates to myself 
    } 
    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"Location: %@", [newLocation description]); 

    // FILL YOUR VIEW or broadcast a message to your view. 

} 

- (void)locationManager:(CLLocationManager *)manager 
      didFailWithError:(NSError *)error 
{ 
    NSLog(@"Error: %@", [error description]); 

    // FILL YOUR VIEW or broadcast a message to your view. 
} 

- (void)dealloc { 
    [self.locationManager release]; 
    [super dealloc]; 
} 

@end