lookin官網(wǎng)地址
微信讀書iOS團隊出品
Lookin 可以查看與修改 iOS App 里的 UI 對象咬摇,類似于 Xcode 自帶的 UI Inspector 工具呀打,或另一款叫做 Reveal 的軟件营密。
但借助于“控制臺”和“方法監(jiān)聽”功能鼓鲁,Lookin 還可以進行 UI 之外的調(diào)試音同。
此外请敦,雖然 Lookin 主體是一款 macOS 程序肆饶,它亦可嵌入你的 iOS App 而單獨運行在 iPhone 或 iPad 上改衩。
最后,Lookin 完全免費驯镊。
一堆 View 混在一起分不清葫督?
Lookin 會顯示變量名竭鞍,以及 indexPath 等各種提示
下邊說一下iOS項目中集成的方式:https://lookin.work/faq/integration-guide/
在你的 Podfile 中添加以下內(nèi)容:
pod 'LookinServer', :configurations => ['Debug']
這里指定了只有在 Debug 模式下才能使用 Lookin。
運行 pod install 或 pod update LookinServer
現(xiàn)在你應該已經(jīng)可以正常使用 Lookin 了橄镜。
設置為搖一搖代碼方式:
//Macro.h
//http://www.reibang.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 // 開發(fā)階段-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 // 發(fā)布階段-上線階段:移除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
}
}
官方文檔:
如何在 iPhone 或 iPad 上使用 Lookin 偎快?
- 首先,請確保你的 iOS App 已經(jīng)嵌入了 LookinServer.framework
但即使沒有嵌入 LookinServer洽胶,調(diào)用下面這些步驟所示的代碼也僅僅是不產(chǎn)生任何效果而已晒夹,絕不會導致你的 App 發(fā)生 Crash 等情況。
-
調(diào)用下面這句代碼會將當前的 UI 結構導出為 Lookin 文檔姊氓,并使用 AirDrop 或微信等方式轉(zhuǎn)發(fā)出去丐怯。
該文檔可在電腦上使用 Lookin 客戶端打開。
[[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_Export" object:nil];
-
調(diào)用下面這句代碼可進入 2D 模式翔横,在該模式下响逢,你可查看 UIView 的常用屬性,或者測量兩個 UIView 之間的距離棕孙。
[[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_2D" object:nil];
-
調(diào)用下面這句代碼可進入 3D 模式舔亭,在該模式下,你可查看當前的 3D 結構蟀俊。
[[NSNotificationCenter defaultCenter] postNotificationName:@"Lookin_3D" object:nil];
-
你可以把以上三個功能的調(diào)用代碼放到你的 App 的“搖一搖”手勢之類的 Debug 菜單中钦铺,比如下圖這樣。