# TestTarget **Repository Path**: 759625907/test-target ## Basic Information - **Project Name**: TestTarget - **Description**: 测试多Target - **Primary Language**: Objective-C - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-04-20 - **Last Updated**: 2021-04-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # TestTarget > 我们永远也看不透事物的本质,看到的只是自己思想的倒影! ### 开发背景        在iOS应用开发中,不免需要引入一些第三方SDK,方便开发。比如蓝牙或相机扫描身份证[[IDCardRecognition](https://github.com/ouzhijun/IDCardRecognition) ](https://github.com/ouzhijun/IDCardRecognition)之类的。 ### 存在问题   这些SDK仅支持真机不支持模拟器。即,真机上可以正常运行,而模拟器会报错,如```error: Building for iOS Simulator, but the linked library 'OnlyForiOS.a' was built for iOS. (in target 'TestTarget' from project 'TestTarget')```或者```Undefined symbols for architecture x86_64...ld:symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)``` ### 参考解决办法 1. 写脚本或新建scheme 参考链接1:[关于“不支持模拟器调试的第三方SDK”解决办法](https://www.jianshu.com/p/cc7075e8be2f) 2. 模拟器删除相关第三方 copy新建一份代码,删除相关第三方,实现在模拟器上查看界面 ### 不足   1.写脚本繁琐,对不懂脚本语言的同学来说,知其然不知其所以然。另外,这样还是会有问题,只要库在,那么运行模拟器就报错。   2.每次界面变动需要同时比较修改另一份代码,麻烦 ### 思考   有没有一种方式可以在一个工程里进行修改,真机状态下.a文件参与编译,在模拟器状态下不参与编译呢?   在阅读以下等博客后,整理出了思路   参考链接2:[iOS添加多个Target,实现打包不同版本](https://blog.csdn.net/oHaiKuoTianKong1682/article/details/107766372)   参考链接3:[iOS开发:集成的SDK不支持模拟器调试怎么办?](https://www.jianshu.com/p/7b7294b2bba8?utm_campaign=hugo&utm_medium=reader_share&utm_content=note&utm_source=weixin-friends) ### 改进 1. 添加Target,并确保新添加的Target名称与info.plist及配置保持一致(运行时Target名称没有改变的,需重新打开项目) * ![rename.png](https://upload-images.jianshu.io/upload_images/3115781-263c42fc65a5897b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 2. 添加.a文件时,如果仅支持真机不支持模拟器,那么Add to targets:只勾选原有的Target即可。 ![AddFile.png](https://upload-images.jianshu.io/upload_images/3115781-54b644618c1e8f0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 3. 在Build Setting中搜索:preprocessor macros,添加设置一个预编译宏,来处理条件编译。如SIMULATOR=1。 ![条件编译.png](https://upload-images.jianshu.io/upload_images/3115781-ef8bbc8badaeccb4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 4. 在真机使用到而模拟器没有使用到的地方使用预编译指令。 例: ``` #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { #if SIMULATOR == 0 ViewController *vc = [[ViewController alloc]init]; [self.navigationController pushViewController:vc animated:YES]; #endif } ``` 5.如果是**真机**运行,执行Target为**TestTarget**的Scheme,如果是**模拟器**执行**TestTarget-Simulator**即可。通过切换Target的方式来处理真机与模拟器的调试切换 ![ChangeTarget.png](https://upload-images.jianshu.io/upload_images/3115781-e0f93874963298de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) * 总结:以上,实现了不支持模拟器SDK、文件的**条件编译**,利用条件编译,在模拟器环境下不对不支持x86的SDK进行编译。 BTW:喜欢伸手测试的小伙伴可以点击[下载](https://gitee.com/759625907/test-target.git)进行测试,喜欢请点赞,谢谢!