[iOS] iOS 常见编译问题整理
1、错误:App Transport Security has blocked a cleartext HTTP
从 iOS 9.x 版本开始,App 选项默认不支持 HTTP 连网,当使用相关库的时候会出现如下错误:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
Temporary exceptions can be configured via your app‘s Info.plist file.
或者直接编辑Info.plist,在最后 tag 前添加:
1 2 3 4 5 | <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> |
2、错误:Undefined symbols for architecture arm64: "std::basic_ofstream
如果编译中出现如下错误:
Undefined symbols for architecture arm64:
"std::basic_ofstream
通常这是由于相关的 Controller 没有采用 .mm 扩展名导致,可以将调用 .a 库的相关 Controller 修改为 .mm 扩展名。
另外还需要在 Build Phases > Link Binary With Libraries 下添加 libc++.tbd:
3、错误:Undefined symbols for architecture arm64: "_get_crc_table"
如果编译中出现如下错误:
Undefined symbols for architecture arm64:
"_get_crc_table", referenced from:
_unzOpenCurrentFile3 in libZipArchive.a(unzip.o)
通常这是由于缺乏 libz 库引用导致,只需在 Build Phases > Link Binary With Libraries 下添加 libz.tbd 即可:
4、错误:This app has crashed because it attempted to access privacy-sensitive data without a usage description.
如果运行时出现如下错误并程序 crash:
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.
通常这是由于在新的 iOS 版本中,要求我们在使用用户私密资料时加上 usage description。比如这个错误中就提示了我们在使用如下代码(UIImageWriteToSavedPhotosAlbum 函数)访问用户相册时,需要加入 NSPhotoLibraryUsageDescription 描述。
1 2 3 4 5 6 | - (void)btnTakePhoto { UIImage *arImage = [_arController getCurrentARImage]; UIImageWriteToSavedPhotosAlbum(arImage, nil, nil, nil); } |
添加私有方法描述步骤如下:
1)点击 target 设置中的 Info 菜单,在任意位置右键选择 Add Row:
2)出现增加新设置行后,下拉找到 Privacy -> Photo Library Usage Description 菜单项选中,在 Value 栏添加我们想要访问用户相册的理由,比如在我的 AR 应用中,我填写的是 “Photo library is needed for saving AR image” 这一栏你根据情况随便填写即可。
3)然后再运行,如果点击使用了相册的功能,则会出现对话框,和你刚才填写的理由:
当然,除此以外,还有很多私有方法需要添加描述,比如相机、GPS数据等等,添加方法均同理。
5、错误:Undefined symbols for architecture x86_64: "_objc_msgSend"
如果在编译中遇到如下错误:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_MakClass", referenced from:
objc-class-ref in helloworld-XEijke.o
"_objc_msgSend", referenced from:
_main in helloworld-XEijke.o
"_objc_msgSend_fixup", referenced from:
l_objc_msgSend_fixup_alloc in helloworld-XEijke.o
l_objc_msgSend_fixup_release in helloworld-XEijke.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这通常是因为没有引入 Foundation.framework 所致,只需在 Build Phase 里面添加即可。
6、错误:Undefined symbols for architecture x86_64: "_NSAppKitVersionNumber"
如果在编译中遇到如下错误:
Undefined symbols for architecture x86_64:
"_NSAppKitVersionNumber", referenced from:
_cvInitSystem in libopencv_highgui.a(window_cocoa.mm.o)
_cvNamedWindow in libopencv_highgui.a(window_cocoa.mm.o)
"_NSDeviceRGBColorSpace", referenced from:
-[CVView setImageData:] in libopencv_highgui.a(window_cocoa.mm.o)
"_NSFullScreenModeSetting", referenced from:
cvSetModeWindow_COCOA(char const*, double) in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSApplication", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSBitmapImageRep", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSEvent", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSImage", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSScreen", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSSlider", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSTextField", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSView", referenced from:
_OBJC_CLASS_$_CVView in libopencv_highgui.a(window_cocoa.mm.o)
_OBJC_CLASS_$_CVSlider in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_CLASS_$_NSWindow", referenced from:
objc-class-ref in libopencv_highgui.a(window_cocoa.mm.o)
_OBJC_CLASS_$_CVWindow in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_METACLASS_$_NSView", referenced from:
_OBJC_METACLASS_$_CVView in libopencv_highgui.a(window_cocoa.mm.o)
_OBJC_METACLASS_$_CVSlider in libopencv_highgui.a(window_cocoa.mm.o)
"_OBJC_METACLASS_$_NSWindow", referenced from:
_OBJC_METACLASS_$_CVWindow in libopencv_highgui.a(window_cocoa.mm.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Showing first 200 warnings only
这通常是因为没有引入 Cocoa.framework 所致,只需在 Build Phase 里面添加即可。