因爲公司一款產品的需求,最近一直在研究iOS設備的後臺定位。主要的難點就是,當系統進入後臺以後,程序會被掛起,屆時定時器、以及代碼都不會Run~ 因此一旦用戶將個人App先換到了後臺,個人定位功能將沒法繼續。git
通過了我幾天的查找資料和嘗試,我發現了一個我我的認爲很是簡單的方法來解決這個問題。這個方法說白了是使用一個第三方的類庫,通過測試,App在真機後臺運行3小時,App依然在定時的向服務器發送位置座標。這個類庫的名字叫作「voyage11/Location」,做者的叫作Ricky。你們能夠去Github下載這個類庫。要注意的時,要測試後臺定位功能,最好在真機上測試,模擬器上測試怪怪的~結果不要做爲參考。github
下載後咱們獲得的是一個工程,你們運行一下看看效果,UI什麼也沒有,效果都顯示在控制檯裏面,運行一會以後,切換的後臺再看看效果。web
下面介紹一下這個類庫的類和方法,瞭解了這些以後,你大概就會知道怎麼使用voyage/Location這個類庫了。數組
從下載的工程裏,我能夠直觀的看到這個類庫的結構:服務器
千萬不要感受這麼一大坨會不會很麻煩TT NO!不要懼怕,他用起來真的很簡單,你只須要略微修改幾個參數,其餘的那一坨你能夠不用管(若是隻求能用,不求甚解的話)。網絡
我來簡單說一下這幾個類的做用:oop
和咱們直接打交道的主要就是LocationTracker這個類。用這個類,咱們能夠配置定位的相關參數。咱們來看看這個類的主要方法:測試
+ (CLLocationManager *)sharedLocationManager;
構造方法,得到一個LocationTraker的單例對象(不瞭解單列是啥意思的,你能夠理解成建立一個全局變量)。atom
1 - (void)startLocationTracking;
這個方法是開始追蹤定位,以後,定位功能就跑起來了。url
- (void)stopLocationTracking;
這個方法和上面的方法是一對,它用來關閉定位追蹤。
- (void)updateLocationToServer;
這個方法用來向服務器發送已獲取的設備位置信息。
另外還有兩個類是「LocationShareModel」和「BackgroundTaskManager」。他們的工做主要是處理定位服務的後臺運行和處理設備獲取的定位數據。具體的原理咱們不用去管它。
That's all~怎麼樣,真的很簡單吧~
好啦,趁熱乎,咱們趕忙拿來用用試試吧~
首先咱們把咱們要用到的類先從下載的項目文件夾中拿出來,咱們要用的總共有三個類 :「LocationTracker」「LocationShareModel」和「BackgroundTaskManager」以下圖:
下一步,Xcode打開咱們要使用這個類庫的工程,把這三個類庫加入到工程中去(你能夠選中這6個文件拖進文件導航)
拋開這個類庫不談,若是要進行後臺定位服務,你須要確保爲工程作出以下設置:
1.開啓後臺定位模式:選中工程Target->Capabilities->Background Modes-勾選Location updates:
2.在Plist中添加前/後臺定位的鍵值:在Plist根目錄新建兩個鍵值以下,這些鍵值將會在程序開啓時讓用戶容許開啓後前/臺定位。
設置完以上配置以後,咱們就能夠來想用咱們的voyageLocation啦
首先在你想要使用定位功能的ViewController 導入頭文件
#import "LocationTracker.h"
而後聲明兩個成員變量:
@property LocationTracker * locationTracker; @property (nonatomic) NSTimer* locationUpdateTimer;
以後寫一個方法配置LocationTraker:
1 -(void)setUpLocationTraker{ 2 self.locationTracker = [LocationTracker sharedLocationManager];
3 [self.locationTracker startLocationTracking]; 4 //設定向服務器發送位置信息的時間間隔 5 NSTimeInterval time = 300.0; 6 //開啓計時器 7 self.locationUpdateTimer = 8 [NSTimer scheduledTimerWithTimeInterval:time 9 target:self 10 selector:@selector(updateLocation) 11 userInfo:nil 12 repeats:YES]; 13 }
上面計時器每隔300s運行一次「updateLocation」方法,該方法的實現以下:
1 -(void)updateLocation { 2 NSLog(@"開始獲取定位信息..."); 3 //向服務器發送位置信息
4 [self.locationTracker updateLocationToServer]; 5 }
上面的updateLocationToServer方法就是你向服務器發送信息的方法了,這個方法須要你依照本身的需求進行改動打開「LocationTraker.m」文件找到該方法:
1 - (void)updateLocationToServer { 2
3 NSLog(@"updateLocationToServer"); 4
5 // Find the best location from the array based on accuracy
6 NSMutableDictionary * myBestLocation = [[NSMutableDictionary alloc]init]; 7
8 for(int i=0;i<self.shareModel.myLocationArray.count;i++){ 9 NSMutableDictionary * currentLocation = [self.shareModel.myLocationArray objectAtIndex:i]; 10
11 if(i==0) 12 myBestLocation = currentLocation; 13 else{ 14 if([[currentLocation objectForKey:ACCURACY]floatValue]<=[[myBestLocation objectForKey:ACCURACY]floatValue]){ 15 myBestLocation = currentLocation; 16 } 17 } 18 } 19 NSLog(@"My Best location:%@",myBestLocation); 20
21 //If the array is 0, get the last location 22 //Sometimes due to network issue or unknown reason, you could not get the location during that period, the best you can do is sending the last known location to the server
23 if(self.shareModel.myLocationArray.count==0) 24 { 25 NSLog(@"Unable to get location, use the last known location"); 26
27 self.myLocation=self.myLastLocation; 28 self.myLocationAccuracy=self.myLastLocationAccuracy; 29
30 }else{ 31 CLLocationCoordinate2D theBestLocation; 32 theBestLocation.latitude =[[myBestLocation objectForKey:LATITUDE]floatValue]; 33 theBestLocation.longitude =[[myBestLocation objectForKey:LONGITUDE]floatValue]; 34 self.myLocation=theBestLocation; 35 self.myLocationAccuracy =[[myBestLocation objectForKey:ACCURACY]floatValue]; 36 } 37
38 NSLog(@"Send to Server: Latitude(%f) Longitude(%f) Accuracy(%f)",self.myLocation.latitude, self.myLocation.longitude,self.myLocationAccuracy); 39
40 //TODO: 在這裏插入你向服務器發送請求的代碼 41
42 //當你向服務器發送位置信息成功後,要清空當前的數組,以便下一回合的定位
43 [self.shareModel.myLocationArray removeAllObjects]; 44 self.shareModel.myLocationArray = nil; 45 self.shareModel.myLocationArray = [[NSMutableArray alloc]init]; 46 }
在上面代碼的第處40行進行修改,添加你像服務器發送位置信息的請求,當請求成功後,不要忘記執行第43-45行的代碼,清空數組,以便下一次定位。
例如我加入的代碼以下,我是用了AFNetworking的網絡請求類庫:
1 AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager]; 2 NSString *url=[NSString stringWithFormat:@"http://172.1.1.36:8080/uploadDeviceLocation.action"]; 3 NSMutableDictionary *parameter=[[NSMutableDictionary alloc]init]; 4 [parameter setObject:@"####################" forKey:@"udid"]; 5 [parameter setObject: [NSString stringWithFormat:@"%f",self.myLocation.longitude] forKey:@"x"]; 6 [parameter setObject:[NSString stringWithFormat:@"%f",self.myLocation.latitude] forKey:@"y"]; 7 [manager GET:url parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) { 8 NSLog(@" 成功了"); 9 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 10 NSLog(@"失敗了");
43 [self.shareModel.myLocationArray removeAllObjects];
44 self.shareModel.myLocationArray = nil;
45 self.shareModel.myLocationArray = [[NSMutableArray alloc]init];
11 }];
OK~ 搞定,趕忙試試吧! 哦對了,你不以爲你忘記什麼了嗎? 對了 要把 [self setUpLocationTraker] 方法放到你的 viewDidLoad 裏面~哈哈
這樣 後臺位置上傳就解決了。這是控制檯打出的Log。
解決了糾結好幾天的問題,如今個人內心還有點小興奮。總結一下這個類庫的特色,第一就是使用很是簡單。第二,運行穩定,通過我近2個小時的測試,定位一直跑,後臺一直能收到上報的信息,妥妥的。第三,這個類庫的做者考慮到了定位耗電的問題,我在測試時,用的是一部很老的iPhone4S,兩個小時掉了10%的電,對於我來講仍是能夠接受的。再次感謝Rickey。這是他的博客,下面有捐款的連接,但願你們去表示一下對他的感謝(支持paypal、visa、master等,銀聯不支持哦)。