iOS開發基礎篇 CoreLocation定位服務

2021-07-04 17:33:35 字數 2513 閱讀 3624

cllocationmanager的常用操作和屬性
locmanager = [[cllocationmanager alloc]init];

//設定每隔100公尺更新位置
locmanager.distancefilter = 100;
//設定位置精度
locmanager.desiredaccuracy = kcllocationaccuracybest; 

//設定**

locmanager.delegate = self;

開始使用者定位- (void)startupdatinglocation;

停止使用者定位- (void) stopupdatinglocation;

當呼叫了startupdatinglocation方法後,就開始不斷地定位使用者的位置,中途會頻繁地呼叫**的下面方法
- (void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations;

//協議中的方法,作用是每當位置發生更新時會呼叫的委託方法

-(void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations

//當位置獲取或更新失敗會呼叫的方法
-(void)locationmanager:(cllocationmanager *)manager didfailwitherror:(nserror *)error
if ([error code] == kclerrorlocationunknown)
uialertview *alertview = [[uialertview alloc]initwithtitle:@"location"
message:errormsg delegate:self cancelbuttontitle:@"ok"otherbuttontitles:nil, nil];
[alertview show];
}

剛才部落格已經發出去了,突然想到乙個問題,在ios8上使用定位還需要在配置一些檔案資訊,希望來得及。

1、在使用corelocation前需要呼叫如下函式【ios 8專用】:

ios 8對定位進行了一些修改,其中包括定位授權的方法,cllocationmanager增加了下面的兩個方法:

(1)始終允許訪問位置資訊

- (void)requestalwaysauthorization;

(2)使用應用程式期間允許訪問位置資料

- (void)requestwheninuseauthorization;

示例如下:

self.locationmanager = [[cllocationmanager alloc]init];

_locationmanager.delegate = self;

_locationmanager.desiredaccuracy = kcllocationaccuracybest;

_locationmanager.distancefilter = 10;

[_locationmanager requestalwaysauthorization];//新增這句

[_locationmanager startupdatinglocation];

2、在info.plist檔案中新增如下配置:

(1)nslocationalwaysusagedescription

(2)nslocationwheninuseusagedescription

這兩個鍵的值就是授權alert的描述,示例配置如下[勾選show raw keys/values後進行新增]:

總結:ios 8對定位進行了一些修改,其中包括定位授權的方法,cllocationmanager增加了以下兩個方法:

added -[cllocationmanager requestalwaysauthorization]

added -[cllocationmanager requestwheninuseauthorization]

在使用定位服務前需要通過上面兩個方法申請授權:

[cllocationmanager requestalwaysauthorization] 授權使應用在前台後台都能使用定位服務

-[cllocationmanager requestwheninuseauthorization] 授權則與之前的一樣

另外,在使用這兩個方法授權前,必須在info.plist中增加相應的鍵值( nslocationalwaysusagedescription、nslocationwheninuseusagedescription),這兩個鍵的值就是授權alert的描述。

基礎篇 ios開發概述

ios是由蘋果公司開發的移動作業系統。蘋果公司最早於2007年1月9日的macworld大會上公布這個系統,最初是設計給iphone使用的,後來陸續套用到ipod touch ipad上。ios與蘋果的macos作業系統一樣,屬於類unix的商業作業系統。原本這個系統名為iphone os,因為ip...

基礎篇 ios開發概述

ios是由蘋果公司開發的移動作業系統。蘋果公司最早於2007年1月9日的macworld大會上公布這個系統,最初是設計給iphone使用的,後來陸續套用到ipod touch ipad上。ios與蘋果的macos作業系統一樣,屬於類unix的商業作業系統。原本這個系統名為iphone os,因為ip...

iOS開發學習筆記 基礎篇

建立新工程 點選next後,繼續到下乙個介面。填寫產品名稱和類字首 公司標識等資訊。點選next,會提示選擇工程存放的資料夾,選擇完成後,就會彈出下面的介面。這個介面就是xcode的工作介面。xcode作為乙個ide開發環境,為我們提供了ios程式開發所需要的一切。編譯器 除錯工具 模擬器 編輯器 ...