# Build WebRTC library for iOS
###### tags: `iOS` `Qnap` `WebRTC`
[toc]
Since google decide stop support precompiled binary, we need to build our own for the latest version of webrtc.
- [Discussion](https://groups.google.com/forum/#!msg/discuss-webrtc/Ozvbd0p7Q1Y/M4WN2cRKCwAJ)
- ~~[Build script](https://source.chromium.org/chromium/chromium/src/+/master:third_party/webrtc/tools_webrtc/ios/build_ios_libs.py?originalUrl=https:%2F%2Fcs.chromium.org%2Fchromium%2Fsrc%2Fthird_party%2Fwebrtc%2Ftools_webrtc%2Fios%2Fbuild_ios_libs.py)~~
- [Official Steps](http://webrtc.github.io/webrtc-org/native-code/ios/)
- [link text](https://www.jianshu.com/p/ee66963c0c89)
- - -
## **DON'T:**
- Do not use **git pull** to update your src codebase
- **USE:** gclient sync
## AppRTCMobile Build Steps
1. Create a workspace
* `mkdir WebRTC_Source`
* `cd webRTC_Source`
2. Get depot_tools
* [Official Page](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html#_setting_up)
* `git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git`
* `export PATH=/path/to/depot_tools:$PATH`
3. Fetch source code
* `fetch --nohooks webrtc_ios`
* `gclient sync`
* `cd src`
* checkout specific commit if needed
* M90: bb52bdf09516ca548c4aff50526eda561f239bc0
* gclient sync -r src@bb52bdf09516ca548c4aff50526eda561f239bc0
4. AppRTCMobile
* `gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64" enable_dsyms=true symbol_level=2' --ide=xcode `
* 修改src/build/config/ios/ios_sdk.gni
* Open xcodeproj
* Target AppRTCMobile
* Build
* **Note**:
1. 修改src/build/config/ios/ios_sdk.gni使用的codesign需要用公司的development
2. 編輯AppRTCMobile Taget的provision以前要先選擇plist後再去改provision profile的bundle
3. 使用auto sign
4. 必須修改plist分頁的Bundle(寫固定的bundle),**不可以**使用xcode的參數引用
5. 必須一同修改framework_objc_signed_bundle,模式如上
## XCFramework Build Steps
1. Edit src/tools_webrtc/ios/build_ios_libs.py
> Exclude arch other than arm64
2. `python src/tools_webrtc/ios/build_ios_libs.py`
* Wait for build complete
3. `cd src/out_ios_libs`
4. `xcodebuild -create-xcframework -framework "arm64_libs/WebRTC.framework" -output WebRTC.xcframework`
## Trouble shooting:
- Execute fetch or gclient:
- **cipd** command not found while execute fetch and gclient:
- Add depot_tools to PATH env variable
- export PATH=$PATH:/path/of/depot_tools
- Execute gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64"'
- **ERROR** at //build/config/ios/ios_sdk.gni:163:33: Script returned non-zero exit code.
- gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64" **ios_enable_code_signing=false**'
> This method cannot deploy to real device if you want to build AppRTCMobile
- use `xcrun security find-identity -v -p codesigning` to get code sign list
- ~~gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64" **ios_code_signing_identity=YOUR_ENT_CODE_SIGN**'~~
> ~~**NEED**: Enterprise code sign~~
- ==**IMPORTANT** (2021/08/16)==
- ==You can only success gen by delete all 'Apple Development' cert but left the one you can use(Ent) in keychain.==
- Build AppRTCMobile gn gen out/ios --args='target_os="ios" target_cpu="arm64"' --ide=xcode
- **ERROR** at //build/config/ios/ios_sdk.gni:163:33: Script returned non-zero exit code.
- use `xcrun security find-identity -v -p codesigning` to get code sign list
- gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64" **ios_code_signing_identity=YOUR_ENT_CODE_SIGN**'
> **NEED**: Enterprise code sign
> gn gen out/ios --args='target_os=“ios” target_cpu=“arm64” ios_app_bundle_id_prefix="com.qnap" ios_code_signing_identity="65CE1FDF9E9279311F3781CFA600568E01BDF3F1"' --ide=xcode
> " and ' need to be modified if use copy-paste
- **Continue** failing gen because of ios_code_signing_identity
- Hack src/build/config/ios/ios_sdk.gni
- 
- Hardcore your company's identity
- This is the only effect method i have tried.
- 
- Comment this section
- ==**IMPORTANT** (2021/08/16)==
- ==You can only success gen by delete all 'Apple Development' cert but left the one you can use(Ent) in keychain.==
- Another method
- Modify ios_sdk.gni
- ios_code_signing_identity_description = "iPhone Developer"
- New error
- 
- ==sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer==
- New error 2
- 
- ==gn gen out/ios_64 --args='target_os="ios" target_cpu="arm64" rtc_include_tests=false' --ide=xcode==
- Build AppRtcMobile fail
- AppRTCMobile target
- Choose info.plist in General page
- Enable Auto sign and choose Ent team
- Change bundle id to: com.qnap.AppRTCMobile
- framework_objc target
- Choose info.plist in General page
- Enable Auto sign and choose Ent team
- Change bundle id to: org.qnap.WebRTC
- Build system is legacy
- Use new build system
- 
- Clang error
- Modify update.py
- Build for all platform
- 
- The **`build_ios_lib.py`** should located in the folder path
- src/tools_webrtc/ios
- `AttributeError: module 'cgi' has no attribute 'escape'` while use python3.8
- Use python3.7