簡體   English   中英

IOS中的位置服務,提示

[英]Location Services in IOS, prompt

我的應用程序需要位置服務才能正常工作,因此首次運行該應用程序時會向用戶詢問問題。 如果用戶說“ OK”,那么它將繼續運行整個應用程序而不會繼續運行,因為它將繼續執行其工作。 因此,第一次運行並不會真正給視圖控制器帶來任何好處。

當我第二次按XCODE中的“執行”按鈕時,它可以正常工作,因為用戶已經授權使用位置服務來獲取經度/緯度。

有人遇到過這個問題嗎? 有什么建議嗎?

在您的viewDidLoad:

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];

您需要做的是在“ didUpdateLocation”調用之后更新視圖,然后調用“ SetNeedsDisplay”,這樣您的視圖便會繪制自身以進行更新。 像這樣:

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

    self.LabellLatitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.latitude];
    self.LabelLongitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.longitude];
    [self.view setNeedsDisplay];

}

如果您添加一個活動指示器直到位置更新,那就更好了。

現在,如果您從“ AppDelegate”類中調用此方法,則可以使用“ NSNotificationCenter”將消息發送到視圖,更新標簽或視圖,或任何您需要的對象,例如:

#pragma mark - locationManager:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdatedLocation" object:nil userInfo:dictionary];


} 

然后在您的UIVIewController ViewDidLoad上,使用此用戶注冊通知:

 //Adding self to notification Center:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"UpdatedLocation" object:nil];

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM