CLLocationManager 不在 viewDidLoad 上初始化
CLLocationManager does not initialize on viewDidLoad
我有一个带有 UIWebView
的简单 objective C
应用程序。这是在 viewDidLoad
方法中加载 URL,我需要在 loadRequest
之前本地化此 URL 但我的 CLLocationManager
正在调用 didUpdateToLocation
方法 AFTER loadRequest
.
我的viewDidLoad
方法:
- (void)viewDidLoad {
// execute super method
[super viewDidLoad];
// put gray background color
self.view.backgroundColor = [UIColor lightGrayColor];
// define itself as UIWebView delegate
self.webView.delegate = self;
// define itself as CLLocationManager delegate
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"LOCALIZED????%@",[self getCustomURL:homeURL]);
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[self getCustomURL:homeURL]]]];
}
我在调用 loadRequest
时总是得到 (null),但就在我得到正确的位置之后。
我试图在一个新线程中分离 startUpdatingLocation
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[locationManager startUpdatingLocation];
});
或者等到获得正确的位置:
while (longitude == nil) {
[locationManager startUpdatingLocation];
}
知道如何在 loadRequest
之前获取位置以获得 url
CLLocationManager
是一个异步任务。要在location之后执行loadRequest,需要为CLLocationManager
实现delegate,然后在webview
上调用loadRequest
您可以在 CLLocationManagerDelegate 的方法中使用 loadRequest:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
……
……
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[self getCustomURL:homeURL]]]];
……
……
}
此外,在 iOS8 中,您需要做几件事才能获取位置:
1)在startUpdatingLocation之前调用requestWhenInUseAuthorization方法:
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
2) 添加 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 键到 Info.plist.
我有一个带有 UIWebView
的简单 objective C
应用程序。这是在 viewDidLoad
方法中加载 URL,我需要在 loadRequest
之前本地化此 URL 但我的 CLLocationManager
正在调用 didUpdateToLocation
方法 AFTER loadRequest
.
我的viewDidLoad
方法:
- (void)viewDidLoad {
// execute super method
[super viewDidLoad];
// put gray background color
self.view.backgroundColor = [UIColor lightGrayColor];
// define itself as UIWebView delegate
self.webView.delegate = self;
// define itself as CLLocationManager delegate
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"LOCALIZED????%@",[self getCustomURL:homeURL]);
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[self getCustomURL:homeURL]]]];
}
我在调用 loadRequest
时总是得到 (null),但就在我得到正确的位置之后。
我试图在一个新线程中分离 startUpdatingLocation
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[locationManager startUpdatingLocation];
});
或者等到获得正确的位置:
while (longitude == nil) {
[locationManager startUpdatingLocation];
}
知道如何在 loadRequest
之前获取位置以获得 url
CLLocationManager
是一个异步任务。要在location之后执行loadRequest,需要为CLLocationManager
实现delegate,然后在webview
您可以在 CLLocationManagerDelegate 的方法中使用 loadRequest:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
……
……
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[self getCustomURL:homeURL]]]];
……
……
}
此外,在 iOS8 中,您需要做几件事才能获取位置:
1)在startUpdatingLocation之前调用requestWhenInUseAuthorization方法:
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
2) 添加 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 键到 Info.plist.