美文网首页
iOS 虚拟位置调研

iOS 虚拟位置调研

作者: 大虾咪 | 来源:发表于2022-11-03 13:54 被阅读0次

    背景:

          有些业务会根据地理位置不同,而有不同的业务处理逻辑。而我们开发或者测试,当然不可能去每一个地址都测试一遍。这种情况下,测试同学一般会找到我们让我们手动改掉系统获取经纬度的回调,或者修改GPX文件,然后再重新打一个包。这样也非常麻烦。
    

    方案:

    一、通过GPX文件修改经纬度信息(模拟iOS设备的位置)

    1. 使用Xcode模拟iOS设备的位置:通过GPX文件修改定位
    2. 更改地图服务
    3. 通过逆地理编码来获取位置信息;


      1.png
    2.png

    二、爱思助手

        1、安装爱思助手最新版本,用数据线连接苹果设备到电脑;
    
        2、连接成功后,依次打开爱思助手“工具箱-虚拟定位”;
    
        3、可以通过三种方式来修改定位:输入地名并搜索、输入精确的经纬度、在地图上直接点击;
    
        4、选择好虚拟定位后点击修改虚拟定位,出现修改虚拟定位成功弹窗表示完成。注意:在修改虚拟定位时请解锁设备屏幕锁,否则可能修改失败!
    
        5、之后在项目中打开地图你会发现定位已经变成刚才设定的位置。
    
        6、如需恢复正常定位,可以在爱思助手上点击“还原真实定位”按钮并“重启”设备;
    

    二、滴滴开源Dokit WithGPS框架研究

    DoraemonKit给出的解决方案:提供一套地图界面,支持在地图中滑动选择或者手动输入经纬度,然后自动替换掉我们App中返回的当前经纬度信息。

    CLLocationManager的delegate中有一个方法如下:

    
    /*
    *  locationManager:didUpdateLocations:
    *
    *  Discussion:
    *    Invoked when new locations are available.  Required for delivery of
    *    deferred locations.  If implemented, updates will
    *    not be delivered to locationManager:didUpdateToLocation:fromLocation:
    *
    *    locations is an array of CLLocation objects in chronological order.
    */
    - (void)locationManager:(CLLocationManager *)manager
       didUpdateLocations:(NSArray<CLLocation *> *)locations API_AVAILABLE(ios(6.0), macos(10.9));
    
    

    我们通常是在这个函数中获取当前系统的经纬度信息。我们如果想要没有侵入式的修改这个函数的默认实现方式,想到的第一个方法就是Method Swizzling。

    但是真正在实现过程中,你会发现Method Swizzling需要当前实例和方法,方法是- (void)locationManager:(CLLocationManager *)manager
    didUpdateLocations:(NSArray<CLLocation *> *)locations 我们有了,但是实例,每一个app都有自己的实现,无法做到统一处理。

    我们就换了一个思路,如何能获取该实现了该定位方法的实例呢?

    就是使用Method Swizzling Hook住CLLocationManager的setDelegate方法,就能获取具体是哪一个实例实现了- (void)locationManager:(CLLocationManager *)manager
    didUpdateLocations:(NSArray<CLLocation *> *)locations 方法。

    具体方法如下:

    第一步: 生成一个CLLocationManager的分类CLLocationManager(Doraemon),在这个分类中,实现- (void)doraemon_swizzleLocationDelegate:(id)delegate这个方法,用来进行方法交换。

    - (void)doraemon_swizzleLocationDelegate:(id)delegate {
        if (delegate) {
            //1、让所有的CLLocationManager的代理都设置为[DoraemonGPSMocker shareInstance],让他做中间转发
            [self doraemon_swizzleLocationDelegate:[DoraemonGPSMocker shareInstance]];
            //2、绑定所有CLLocationManager实例与delegate的关系,用于[DoraemonGPSMocker shareInstance]做目标转发用。
            [[DoraemonGPSMocker shareInstance] addLocationBinder:self delegate:delegate];
    
            //3、处理[DoraemonGPSMocker shareInstance]没有实现的selector,并且给用户提示。
            Protocol *proto = objc_getProtocol("CLLocationManagerDelegate");
            unsigned int count;
            //4.获取协议的所有方法 
            struct objc_method_description *methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count);
            NSMutableArray *array = [NSMutableArray array];
            for(unsigned i = 0; i < count; i++)
            {
                SEL sel = methods[i].name;
                if ([delegate respondsToSelector:sel]) { 
                     //检查DoraemonGPSMocker 是否实现了 协议的所有方法 
                    if (![[DoraemonGPSMocker shareInstance] respondsToSelector:sel]) {
                        NSAssert(NO, @"你在Delegate %@ 中所使用的SEL %@,暂不支持,请联系DoraemonKit开发者",delegate,sel);
                    }
                }
            }
            free(methods);
    
        }else{
            [self doraemon_swizzleLocationDelegate:delegate];
        }
    }
    

    在这个函数中主要做了三件事情,1、将所有的定位回调统一交给[DoraemonGPSMocker shareInstance]处理 2、[DoraemonGPSMocker shareInstance]绑定了所有CLLocationManager与它的delegate的一一对应关系。3、处理[DoraemonGPSMocker shareInstance]没有实现的selector,并且给用户提示。

    第二步:当有一个定位回调过来的时候,我们先传给[DoraemonGPSMocker shareInstance],

    然后[DoraemonGPSMocker shareInstance]再转发给它绑定过的所有的delegate。

    总结:1.进入dokit的虚拟位置设置然后修改了位置

    调用 pointMock 掉 dispatchLocationsToAll

    //locations 虚拟位置设置的位置信息
    - (void)dispatchLocationsToAll:(NSArray*)locations{
    
        for (NSString *key in _locationMonitor.keyEnumerator) {
    
            if ([key hasSuffix:@"_binder"]) {
    
                NSString *binderKey = key;
    
                //根据存储的binderKey 取出CLLocationManager 对象
                CLLocationManager *binderManager = [_locationMonitor objectForKey:binderKey];
    
                [self dispatchLocationUpdate:binderManager locations:locations];
    
            }
    
        }
    
    }
    
    -(void)dispatchLocationUpdate:(CLLocationManager *)manager locations:(NSArray*)locations{
    
        NSString *key = [NSString stringWithFormat:@"%p_delegate",manager];
    
        //根据存储的key 取出CLLocationManagerDelegate 代理对象
    
        id<CLLocationManagerDelegate> delegate = [_locationMonitor objectForKey:key];
    
        if ([delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)]) {
    
            // 此函数通过objc——msg调最初协议的代理实现
            [delegate locationManager:manager didUpdateLocations:locations];
    
        }else if ([delegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]){
    
            [delegate locationManager:manager didUpdateToLocation:locations.firstObject fromLocation:self.oldLocation];
    
            self.oldLocation = locations.firstObject;
    
        }
    
    }
    

    三、自定义位置 获取时拦截 很直接但不够优雅

    在上面- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 方法中拦截是否有存储的位置

    四、总结

    通过以上方案对比 方案三Dokit侵入量较少也更方便测试、开发进行设置虚拟位置 。

    相关文章

      网友评论

          本文标题:iOS 虚拟位置调研

          本文链接:https://www.haomeiwen.com/subject/tchetdtx.html