轉 ios 里如何判斷當前應用的定位服務是否可用


如何在某個程序里面判定當前應用程序的定位服務是否可用,其實沒有什么簡單的方法。

這個[CLLocationManager locationServicesEnabled]檢測的是整個iOS系統的位置服務開關,無法檢測當前應用是否被關閉,只能通過CLLocationManagerDelegate的locationManager:didFailWithError:方法去檢測:

- (void)locationManager: (CLLocationManager *)manager
didFailWithError: (NSError *)error {
    
    NSString *errorString;
    [manager stopUpdatingLocation];
    NSLog(@"Error: %@",[error localizedDescription]);
    switch([error code]) {
        case kCLErrorDenied:
            //Access denied by user
            errorString = @"Access to Location Services denied by user";
            //Do something...
            break;
        case kCLErrorLocationUnknown:
            //Probably temporary...
            errorString = @"Location data unavailable";
            //Do something else...
            break;
        default:
            errorString = @"An unknown error has occurred";
            break;
    }
}

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

 

參考:http://blog.csdn.net/jinglijun/article/details/8893062

 

另外還有一種變通的方法:不需要通過上述方式來判斷用戶是否為應用程序開啟了定位服務,而是通過判斷獲取的坐標值是否為空或者為零來判斷用戶是否為應用程序開啟了定位功能。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM