[英]Getting Location Services to work in IOS 8
我正在嘗試更新一些舊代碼以使其在IOS 8中正常工作。
但是對於如何正確實施該方法,我仍然很困惑。
我加了
<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>
進入infoPlist.strings
但我不確定如何更新代碼以使其正確執行。 這是我正在使用的舊代碼的副本。 誰能提供有關如何正確更新此代碼以使其與IOS 8配合使用的見識?
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(@"%s", __PRETTY_FUNCTION__);
switch (status) {
case kCLAuthorizationStatusAuthorizedAlways:
{
NSLog(@"kCLAuthorizationStatusAuthorized");
// Re-enable the post button if it was disabled before.
self.navigationItem.rightBarButtonItem.enabled = YES;
[self.locationManager startUpdatingLocation];
}
break;
case kCLAuthorizationStatusDenied:
NSLog(@"kCLAuthorizationStatusDenied");
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Anywall can’t access your current location.\n\nTo view nearby posts or create a post at your current location, turn on access for Anywall to your location in the Settings app under Location Services." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
// Disable the post button.
self.navigationItem.rightBarButtonItem.enabled = NO;
}
break;
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"kCLAuthorizationStatusNotDetermined");
}
break;
case kCLAuthorizationStatusRestricted:
{
NSLog(@"kCLAuthorizationStatusRestricted");
}
break;
}
}
我在這里使用的代碼來自parse的Anywall教程的舊版本。 我嘗試添加
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"kCLAuthorizationStatusNotDetermined");
[self.locationManager requestAlwaysAuthorization];
}
break;
另外,該應用程序還在地圖上顯示用戶的位置,我也必須更新該方法嗎?
您必須這樣做:
1) #import <CoreLocation/CoreLocation.h>
2)設置委托CLLocationManagerDelegate
3)創建CLLocationManager *locationManager;
賓語
4)在.m文件中#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
5)設置位置管理器
if (self.locationManager==nil) {
self.locationManager = [[CLLocationManager alloc]init];
}
self.locationManager.delegate = self;
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
NSString *title;
title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Settings", nil];
[alertView setTag:1001];
[alertView show];
[alertView release];
}
else{
NSString *titles;
titles = @"Title";
NSString *msg = @"Location services are off. To use location services you must turn on 'Always' in the Location Services Settings from Click on 'Settings' > 'Privacy' > 'Location Services'. Enable the 'Location Services' ('ON')";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:titles
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
[self.locationManager requestAlwaysAuthorization];
}
}
[self.locationManager startUpdatingLocation];
6)設置iOS 8的警報視圖
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag==1001){
if (buttonIndex == 1) {
// Send the user to the Settings for this app
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.