美文网首页
iOS调试UI的工具Lookin《微信读书》

iOS调试UI的工具Lookin《微信读书》

作者: Onlyoner | 来源:发表于2019-11-01 11:19 被阅读0次

lookin官网地址
微信读书iOS团队出品

Lookin 可以查看与修改 iOS App 里的 UI 对象,类似于 Xcode 自带的 UI Inspector 工具,或另一款叫做 Reveal 的软件。
但借助于“控制台”和“方法监听”功能,Lookin 还可以进行 UI 之外的调试。
此外,虽然 Lookin 主体是一款 macOS 程序,它亦可嵌入你的 iOS App 而单独运行在 iPhone 或 iPad 上。
最后,Lookin 完全免费。

image.png

一堆 View 混在一起分不清?
Lookin 会显示变量名,以及 indexPath 等各种提示

下边说一下iOS项目中集成的方式:https://lookin.work/faq/integration-guide/
在你的 Podfile 中添加以下内容:

pod 'LookinServer', :configurations => ['Debug']

这里指定了只有在 Debug 模式下才能使用 Lookin。

运行 pod install 或 pod update LookinServer

现在你应该已经可以正常使用 Lookin 了。

设置为摇一摇代码方式:

//Macro.h
//https://www.jianshu.com/p/6517ab655be7
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
    Stuff; \
    _Pragma("clang diagnostic pop") \
} while (0)
#ifdef DEBUG // 开发阶段-DEBUG阶段:使用Log
#define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else // 发布阶段-上线阶段:移除Log
#define NSLog(FORMAT, ...) nil
#endif
#define __LogFunc__ NSLog(@"%s",__func__);
//LookinTool.m
+ (void)showAlertSheetWithTitle:(NSString *)title message:(NSString *)message actionArray:(NSArray <NSDictionary*>*)arr{
    /*
     [{ title
        param:{}
     }]
     */
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    [arr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:obj[@"title"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            SEL selector = NSSelectorFromString(@"perform:");
            if ([LookinTool respondsToSelector:selector]) {
                SuppressPerformSelectorLeakWarning([ToolsClass performSelector:selector withObject:obj]);
            }
        }];
        [alertController addAction:action];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
+ (void)perform:(NSDictionary *)obj{
    NSDictionary *param = obj[@"param"];
    NSString *LookinTypeStr = @"LookinType";
    if ([param.allKeys containsObject:LookinTypeStr]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:param[LookinTypeStr] object:nil];
    }
}
//这一段代码可以写在MainTabBarViewController.m中也可以写在AppDelegate.m中
// 摇一摇摇动结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    __LogFunc__
#ifdef DEBUG
    [LookinTool showAlertSheetWithTitle:@"LooKin"
                                message:@"UI分析 🐰"
                            actionArray:@[@{@"title":@"导出当前UI结构",
                                            @"param":@{@"LookinType":@"Lookin_Export"}
                                           },
                                          @{@"title":@"2D视图审查元素",
                                            @"param":@{@"LookinType":@"Lookin_2D"}
                                           },
                                         @{@"title":@"3D视图",
                                            @"param":@{@"LookinType":@"Lookin_3D"}
                                           }
                            ]];

#else
    //nothing to do ...
#endif
}
}
IMG_0061.PNG
IMG_0062.PNG RPReplay_Final1572577743.gif

官方文档:

如何在 iPhone 或 iPad 上使用 Lookin ?

  1. 首先,请确保你的 iOS App 已经嵌入了 LookinServer.framework
    但即使没有嵌入 LookinServer,调用下面这些步骤所示的代码也仅仅是不产生任何效果而已,绝不会导致你的 App 发生 Crash 等情况。
  1. 调用下面这句代码会将当前的 UI 结构导出为 Lookin 文档,并使用 AirDrop 或微信等方式转发出去。
    该文档可在电脑上使用 Lookin 客户端打开。
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_Export" object:nil];

    image
  2. 调用下面这句代码可进入 2D 模式,在该模式下,你可查看 UIView 的常用属性,或者测量两个 UIView 之间的距离。

    [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_2D" object:nil];

    image
  3. 调用下面这句代码可进入 3D 模式,在该模式下,你可查看当前的 3D 结构。

    [[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_3D" object:nil];

    image
  4. 你可以把以上三个功能的调用代码放到你的 App 的“摇一摇”手势之类的 Debug 菜单中,比如下图这样。

    image
image.png
image.png

相关文章

网友评论

      本文标题:iOS调试UI的工具Lookin《微信读书》

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