在ios7之前,我們進(jìn)入程序提示用戶開(kāi)啟定位是這樣做的,如下:
10年積累的成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有連山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。CLLocationManager * locationManager = [[CLLocationManager alloc] init];//創(chuàng)建位置管理器
//locationManager.delegate=_instance;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=100.0f;
//定位服務(wù)是否可用
BOOL enable=[CLLocationManager locationServicesEnabled];
//是否具有定位權(quán)限
int status=[CLLocationManager authorizationStatus];
if(!enable || status<3){
//請(qǐng)求權(quán)限
[locationManager requestWhenInUseAuthorization];
}
但是到了ios8后,發(fā)現(xiàn)定位壓根就不起作用,老是提示:
ios8定位解決如下:
先在info.plist定義key:(總是授權(quán))NSLocationAlwaysUsageDescription或者使用時(shí)授權(quán)NSLocationWhenInUseUsageDescription
在.h里面
繼承代碼CLLocationManagerDelegate
定義CLLocationManager *_locationManager;
在.m里面定義:
- (void)startTrackingLocation {
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined) {
//總是授權(quán)
[_locationManager requestAlwaysAuthorization];
//每次授權(quán)一次
//[_locationManager requestWhenInUseAuthorization];
}
else if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
[_locationManager startUpdatingLocation];
}
}
//實(shí)現(xiàn)代碼回調(diào)
#pragma mark - CLLocationManager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"Got authorization, start tracking location");
[self startTrackingLocation];
break;
case kCLAuthorizationStatusNotDetermined:
[_locationManager requestAlwaysAuthorization];
break;
default:
break;
}
}
調(diào)用如下:
if (IOS8) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self startTrackingLocation];
}
最后實(shí)現(xiàn)效果如下圖,點(diǎn)擊Allow即可定位了:
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。