一直在做Android開發(fā),沒有接觸過iOS開發(fā),build iOS App的時候還是遇到不少坑的。
搭建Flutter開發(fā)環(huán)境【Android開發(fā)學(xué)Flutter】搭建Flutter開發(fā)環(huán)境 有寫,這里就不再贅述,還有個小插曲,Xcode需要升級,但是在App Store上升級一直提示磁盤空間不足,但是空間是夠的,最后還是手動下載了Xcode的安裝包,在本地安裝。算是個小坑吧。
開發(fā)環(huán)境:
Mac 10.14.4
Android Studio 3.4.2
Xcode 10.3
網(wǎng)上找了個開源項目來練手,開源中國客戶端:flutter-osc,目前有22k star,還是不錯的。
在 Xcode 上打開工程下的 ios 目錄:

build 的時候遇到這個問題:
Command /bin/sh failed with exit code 255
網(wǎng)上找的解決辦法,在 terminal 執(zhí)行:
flutter clean
flutter build ios
build ios的時候遇到:
Cannot find "xcodebuild". Xcode 9.0 or greater is required to develop for iOS.
Encountered error while building for device.
執(zhí)行:
flutter doctor
提示開發(fā)環(huán)境還沒有搭建好:
xieyuedeMacBook-Pro:flutter-osc-master xieyue$ flutter doctor
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure
you trust this source!
Downloading android-arm-profile/darwin-x64 tools... 1.0s
Downloading android-arm-release/darwin-x64 tools... 0.5s
Downloading android-arm64-profile/darwin-x64 tools... 1.3s
Downloading android-arm64-release/darwin-x64 tools... 0.4s
Downloading android-x86 tools... 2.0s
Downloading android-x64 tools... 2.2s
Downloading android-arm tools... 1.2s
Downloading android-arm-profile tools... 0.6s
Downloading android-arm-release tools... 0.5s
Downloading android-arm64 tools... 1.3s
Downloading android-arm64-profile tools... 0.7s
Downloading android-arm64-release tools... 0.6s
Doctor summary (to see all details, run flutter doctor -v):
[?] Flutter (Channel beta, v1.7.8+hotfix.4, on Mac OS X 10.14.4 18E226, locale
zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
! Some Android licenses not accepted. To resolve this, run: flutter doctor
--android-licenses
[?] Xcode - develop for iOS and macOS
? Xcode installation is incomplete; a full installation is necessary for iOS
development.
Download at: https://developer.apple.com/xcode/download/
Or install Xcode via the App Store.
Once installed, run:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
? CocoaPods not installed.
CocoaPods is used to retrieve the iOS and macOS platform side's plugin
code that responds to your plugin usage on the Dart side.
Without CocoaPods, plugins will not work on iOS or macOS.
For more info, see https://flutter.dev/platform-plugins
To install:
brew install cocoapods
pod setup
! Brew can be used to install CocoaPods.
Download brew at https://brew.sh/.
[?] iOS tools - develop for iOS devices
? libimobiledevice and ideviceinstaller are not installed. To install with
Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
? ios-deploy not installed. To install:
brew install ios-deploy
! Brew can be used to install tools for iOS device development.
Download brew at https://brew.sh/.
[?] Android Studio (version 3.4)
[!] IntelliJ IDEA Community Edition (version 2017.3.4)
? Flutter plugin not installed; this adds Flutter specific functionality.
? Dart plugin not installed; this adds Dart specific functionality.
[!] Connected device
! No devices available
! Doctor found issues in 5 categories.
其實Xcode已經(jīng)安裝了的,但是還需要執(zhí)行下面的指令:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
根據(jù)提示往下執(zhí)行:
brew install cocoapods
提示:-bash: brew: command not found
先安裝Homebrew:
打開 Terminal 輸入以下命令行:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
輸入命令 brew help 檢查是否正確安裝;
如果提示:brew:command not found,就需要進(jìn)行環(huán)境配置;
終端輸入:
sudo vim .bash_profile
在 .bash_profile 文件的末尾添加如下代碼:
export PATH=/usr/local/bin:$PATH
在 vim 模式下,按下 i鍵 進(jìn)入編輯模式;編輯完成后,按 Esc鍵 退出編輯模式;輸入 :wq 保存退出;
刷新環(huán)境變量,輸入命令:
source .bash_profile
安裝Homebrew成功后繼續(xù)安裝cocoapods:
brew install cocoapods
然后:
pod setup
最后還剩這個警告:[!] IntelliJ IDEA Community Edition (version 2017.3.4),這個是IntelliJ IDEA編譯器,我們只要Android Studio可用就好,大家忽視就好。
build工程后出現(xiàn):
diff: /Users/xieyue/Downloads/flutter-osc-master/ios/Pods/Manifest.lock: No such file or directory
error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.
關(guān)閉Xcode,cd到項目ios目錄下:
pod install
再打開 Xcode 重新 build,可以成功運(yùn)行在模擬器上。
運(yùn)行到真機(jī)出現(xiàn):

“設(shè)置”>“通用”>“描述文件”或“描述文件與設(shè)備管理”,設(shè)置信任后終于可以運(yùn)行到真機(jī)。
build 第三方項目是成功了,但是build自己項目時遇到兩個新問題,也記錄在這里:
Command PhaseScriptExecution failed with a nonzero exit code

需要安裝插件:

然后可以build成功,但是模擬器上一片空白,因為打開的是一個web頁面,用的是HTTP協(xié)議,搜索了一下,蘋果出于安全考慮,提交到App Store的App都要用HTTPS,調(diào)試的時候還是可以用HTTP的,不過需要設(shè)置一下,方法可以參考:
ATS(App Transport Security)對HTTP協(xié)議屏蔽引起的問題
設(shè)置之后就可以正常在模擬器上運(yùn)行了。但是真機(jī)上還是有問題:
Showing All Messages
:-1: Signing for "Runner" requires a development team. Select a development team in the project editor. (in target 'Runner')

選擇開發(fā)團(tuán)隊后就可以在真機(jī)上運(yùn)行了。