Flutter的老项目,如果需要增设一个Watch APP...
写在前面:本文禁止任何转载,仅发布在Segmentfault
开始玩
首先,需要在xcode里打开你的项目……
热知识: VSCode中,在ios
文件夹上右键,有一个选项是Open in Xcode
如何添加Watch APP
- xcode里顶上选择File/New/Target…
- 选watchOS的APP,创建完成
如何为Watch APP添加库
watch app当然需要引用一些库,我们继续使用cocoapods,flutter会创建一个podfile,我们需要在flutter_ios_podfile_setup这一行之前添加这些(注意target名字与xcode中target相同):
target ‘Your Watch App' do
platform :watchos, '11.0'
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# 想要什么库自己玩
pod 'Alamofire'
pod 'CryptoSwift', '~> 1.8.3'
# Pods for Your Watch App
target 'Your Watch AppTests' do
inherit! :search_paths
# Pods for testing
end
target 'Your Watch AppUITests' do
# Pods for testing
end
end
# 这一行之前
flutter_ios_podfile_setup
pod install就可以开始玩了。
各种问题
如果你按以上操作做了,那么100%不能运行成功,我在下方分别列举各种问题和解决方案。
提示 Sandbox: rsync.samba(xxxxx) deny(1) file-write-create
原因:xcode15之后默认配置不对
解决:配置build settings
中所有ENABLE_USER_SCRIPT_SANDBOXING
为NO
swift库提示Module xxx not found 或者framework not found
这个问题可能在build或Achieve中单独出现
原因:配置不对
可能1: 检查target/watch app
中,build settings
中的Framework Search Path
,例如CryptoSwift
可以加上无脑加上"${PODS_CONFIGURATION_BUILD_DIR}/CryptoSwift-watchOS”
,也可以直接加$(SRCROOT)
粗暴解决
可能2: 检查target/watch app
中,build settings
中的build active architecture
,全部改为yes。检查下方Supported Platforms
,全部改为watchOS
这里有两个要点:
- 库的搜寻范围需要覆盖到路径
- 本身配置的platform不能出错,因为不同platform的库不一样
提示Cycle inside Runner
原因:flutter的script未考虑嵌入watch app,调整你的主项目Target的Build Phases
顺序即可
参见:https://stackoverflow.com/questions/77209490/flutter-with-watchos-companion-app-error-in-xcode15
简单说就是把主项目Target的Build Phases
中的Embed Watch Content
这个选项,放到Run Script
前面一个,在flutter脚本干活之前,先把watch app打包好集成进去,避免被flutter脚本影响。
开发这么多年我第一次知道这里可以拖拽排序……
完成运行
解决后注意build
和Archive
都不报错才行。
尾声
终极省流说明达成。这一周各种无头苍蝇一样到处查资料,走了无数弯路,得到的经验都写在上面了。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。