2014-01-24 90 views
2

在我的一個項目中,我必須將用戶當前位置發送到Appdelegate中的Web服務器。當用戶點擊顯示的警告的確定按鈕以獲得用戶的許可時,它工作正常。但是,在我第一次運行應用程序時,權限警報顯示在提交給rootview Controller後很晚。同時,經度和緯度獲得cllocation爲0.00000f。如何在appdelegate強制獲得權限或強制獲取用戶位置。用戶當前位置未找到

第二個想法如果用戶拒絕軸位置。警報沒有顯示其他時間。在這種情況下,經緯度得到的位置是0.00000f.How我顯示許可警報2或兩次以上同樣的應用。我希望你能理解我的問題。

快樂編碼

+0

您是否在設備或模擬器中運行? – chandru

+0

即時在設備兄弟運行.. – chakshu

回答

0

按你之情況,你必須設置CLLocationManager的設置窗口的rootViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

第二件事蘋果默認問一次警告,如果用戶給予許可,然後致電CLLocationManagerDelegate method

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

並獲得當前位置的緯度和經度值。如果用戶拒絕位置服務,則必須參考this

+0

我做到了但是警報是不顯示.. :( – chakshu

+0

警報將這樣一旦嘗試完全重新安裝它,那麼你明白了。 –

+0

可以喲我有些think.What我可以做,如果用戶拒絕許可。我必須顯示他/她的位置。我提醒用戶重新安裝應用程序 – chakshu

0

您需要在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方法。