App中愈來愈多的功能依賴用戶實際的位置,例如基於用戶位置提供推薦數據、基於定位判斷某些功能是否可用,可是在開發調試中XCode卻沒有提供自定義的模擬定位的功能,因此本文主要的目的是現實一個能夠在開發調試過程當中隨時模擬定位的功能。git
PS: 當你的app在脫離了XCode的調試測試階段,仍想修改定位的話。
github
咱們在iOS的app開發中一般採用的是CLLocationManager
來獲取用戶當前的位置,固然也能夠採用MKMapView
的showUserLocation
來獲取用戶的位置,因此咱們分別針對這兩種狀況分析。bash
採用CLLocationManager
獲取定位時,是根據CLLocationManagerDelegate
中- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
的回調來獲取到定位的。咱們只須要在系統回調這個方法傳遞給業務代碼的中間,插入一部分代碼,來修改locations參數。本來的邏輯爲系統回調
->業務代碼
,如今變爲系統回調
->模擬定位模塊
->業務代碼
,就實現了無侵入式的實現模擬定位功能。爲了實現這個邏輯,能夠有如下幾個思路。app
由於業務代碼
是根據- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
方法來接受回調的,因此能夠採用Runtime swizzle
這個方法,來實現模擬定位的功能,可是咱們中間組件是不知道業務代碼
中具體是哪一個類,因此沒法具體指定runtime swizzle
哪一個類,因此只能遍歷全部的類,判斷當前類的方法列表中是否有locationManager:didUpdateLocations:
這個方法,若是存在則swizzle
。測試
這種思路是Swizzle了CLLocationManager
的setDelegate:
方法,當調用setDelegate
時,將真實的delegate object
保存下來,再將咱們定義的中間代理類swizzle delegate
對象設置爲CLLocationManager
的delegate
,這樣當系統回調CLLocationManagerDelegate
,會先回調到中間代理類swizzle delegate
中,再由swizzle delegate
將事件傳遞到真實的delegate object
。ui
swizzle
CLLocationManager
中的setDelegate:
方法便可。swizzle delegate
中須要實現所有的CLLocationManagerDelegate
方法,若是後續增長代理方法,仍須要修改這個類。Objective-C中有2個基類,經常使用的就是NSObject
,另外一個就是NSProxy
,NSProxy
主要用於消息轉發處理,因此採用NSProxy
咱們能夠更好的處理方法二中的缺點。atom
建立一個新的類MockLocationProxy
,集成自NSProxy
。spa
// MockLocationProxy.h
#import <CoreLocation/CoreLocation.h>
@interface MockLocationProxy : NSProxy
@property (nonatomic, weak, readonly, nullable) id <CLLocationManagerDelegate> target;
- (instancetype)initWithTarget:(id <CLLocationManagerDelegate>)target;
@end
複製代碼
// MockLocationProxy.m
#import "MockLocationProxy.h"
@implementation MockLocationProxy
- (instancetype)initWithTarget:(id<CLLocationManagerDelegate>)target {
_target = target;
return self;
}
@end
複製代碼
接着就來處理消息轉發的邏輯,首先咱們要知道咱們想要的是什麼效果,系統回調給MockLocationProxy
,MockLocationProxy
只處理locationManager:didUpdateLocations:
,其餘的消息都仍然交給原target。代理
因此咱們在MockLocationProxy.m
中添加如下方法:調試
// MockLocationProxy.m
@implementation MockLocationProxy
- (instancetype)initWithTarget:(id<CLLocationManagerDelegate>)target {
_target = target;
return self;
}
- (BOOL)respondsToSelector:(SEL)aSelector {
if (aSelector == @selector(locationManager:didUpdateLocations:)) {
return YES;
}
return [self.target respondsToSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL sel = invocation.selector;
if ([self.target respondsToSelector:sel]) {
[invocation invokeWithTarget:self.target];
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [self.target methodSignatureForSelector:sel];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
if ([self.target respondsToSelector:_cmd]) {
// 模擬定位代碼
CLLocation *mockLocation = [[CLLocation alloc] initWithLatitude:39.908722 longitude:116.397499];
locations = @[mockLocation];
[self.target locationManager:manager didUpdateLocations:locations];
}
}
@end
複製代碼
當消息發送給MockLocationProxy
時,判斷當前方法是不是locationManager:didUpdateLocations:
,若是是,則MockLocationProxy
響應事件,不然直接傳遞給本來的target。到此已經能夠隨時處理模擬定位。你只須要在模擬定位的代碼作一些處理,就能夠隨時修改定位。
上述方法雖然能夠模擬定位,可是每次修改模擬值都需從新build,那麼有沒有辦法在運行時隨時修改這個值呢?
LLDebugTool
固然能夠,你只須要在你的項目中集成LLDebugTool
,調用其中的Location模塊,LLDebugTool
提供了一個UI來隨時修改這個模擬值,讓你在調試時,隨時模擬定位,LLDebugTool
仍提供了不少其餘的功能,若是你只須要模擬定位的功能,則只須要集成LLDebugTool/Location
這個subspec就能夠了。
前言說過,定位除了CLLocationManager以外,MKMapView
的showUserLocation
也能夠獲取定位信息,那麼如何解決這個問題呢? 你能夠在LLDebugTool/Location
中查看答案。