如何将 Apple Watch Xcode 26 连接到 iOS .NET10 MAUI?
问题背景
作为高级.NET工程师,我已有将MAUI应用发布到App Store的经验,但对Mac生态开发较为陌生。我正在开发一个Apple Watch与iOS设备同步的开关示例应用,遇到了MAUI编写的iOS App无法与Xcode 26开发的Apple Watch App建立连接的问题,具体情况如下:
- 最初用Xcode 26开发的iOS+Watch配对App(仓库链接)运行完美;
- 拆分出独立的WatchOnly App后,与原Xcode iOS App仍能正常同步,验证了仅需匹配Bundle ID即可,无需作为单一整体安装;
- 用.NET 10 MAUI重写iOS组件后(仓库链接),无法与Watch App建立连接,调试输出持续报错:
(注:保留原Xcode iOS App时,运行MAUI iOS App会启动旧的Xcode App,说明Bundle ID匹配正常)INFO: 2026-03-23 15:49:11.265 watchme[3997:1934753] Paired: True, WatchAppInstalled: False, Reachable: False 2026-03-23 15:49:11.265 watchme[3997:1934753] Cannot send state because the watch companion app is unavailable.
现有配置核对
MAUI iOS App的Info.plist配置
<key>CFBundleIdentifier</key> <string>com.theclubreferee.ioswatch.watchme</string> <key>WKCompanionAppBundleIdentifier</key> <string>com.theclubreferee.ioswatch.watchme.watchkitapp</string>
Apple Watch App的Xcode配置
PRODUCT_BUNDLE_IDENTIFIER = com.theclubreferee.ioswatch.watchme.watchkitapp; INFOPLIST_KEY_WKCompanionAppBundleIdentifier = com.theclubreferee.ioswatch.watchme;
排查与解决方案
1. 验证Watch App的实际安装状态
调试日志显示WatchAppInstalled: False,需先确认物理设备上的安装状态:
- 在iPhone的Watch App中,检查你的Watch App是否已正确安装到Apple Watch;
- 用Xcode直接将Watch App安装到配对的物理Watch设备(模拟器配对易出现兼容性问题);
- 确保MAUI App的运行目标为物理iPhone设备,而非模拟器。
2. 补全MAUI App的WatchKit相关配置
MAUI iOS项目需显式配置Watch配对相关项,以下是关键检查点:
(1)完善Info.plist的Watch配置
除WKCompanionAppBundleIdentifier外,添加WKWatchApp声明以明确App支持Watch配对:
<key>WKWatchApp</key> <true/> <key>WKWatchKitApp</key> <dict> <key>WKCompanionAppBundleIdentifier</key> <string>com.theclubreferee.ioswatch.watchme</string> </dict>
(2)配置Entitlements权限
在Platforms/iOS/Entitlements.plist中添加WatchKit访问权限:
<key>com.apple.developer.watchkit</key> <true/>
注意:此权限需在Apple开发者后台开启,并使用包含该权限的Provisioning Profile。
3. 统一签名与Provisioning Profile配置
MAUI App与Watch App必须使用同一开发者账号的配置:
- MAUI iOS App的Provisioning Profile需包含Watch配对权限;
- Watch App的Provisioning Profile需与MAUI App的Bundle ID关联;
- MAUI项目中手动指定签名Team和Provisioning Profile(避免自动签名的不确定性)。
4. 修正MAUI中WCSession的实现逻辑
MAUI需正确绑定WatchConnectivity的Delegate接口,否则无法正常建立连接:
(1)初始化WCSession
在AppDelegate.cs的FinishedLaunching方法中启动Session:
using WatchConnectivity; public override bool FinishedLaunching(UIApplication app, NSDictionary options) { if (WCSession.IsSupported) { var session = WCSession.DefaultSession; session.Delegate = this; // 确保AppDelegate实现IWCSessionDelegate session.ActivateSession(); } return base.FinishedLaunching(app, options); }
(2)完整实现IWCSessionDelegate接口
MAUI需用Export属性绑定Objective-C的Delegate方法,否则Session无法回调:
[Export("session:activationDidCompleteWithState:error:")] public void ActivationDidComplete(WCSession session, WCSessionActivationState activationState, NSError error) { if (error != null) { Console.WriteLine($"Session激活失败: {error.LocalizedDescription}"); } else { Console.WriteLine($"Session激活状态: {activationState}"); Console.WriteLine($"配对状态: 已配对={session.Paired}, WatchApp已安装={session.WatchAppInstalled}, 可连接={session.Reachable}"); } } [Export("sessionDidBecomeInactive:")] public void DidBecomeInactive(WCSession session) { } [Export("sessionDidDeactivate:")] public void DidDeactivate(WCSession session) { session.ActivateSession(); // 重新激活Session,保持连接 }
5. 清理重建项目以消除缓存影响
执行以下步骤清除可能的配置缓存:
- 清理MAUI项目:
dotnet clean; - 删除iPhone和Watch上的旧App,重新安装;
- 重启Visual Studio和Xcode,确保设备连接与签名配置生效。
调试技巧
在MAUI页面中添加实时状态检查,辅助定位问题:
protected override void OnAppearing() { base.OnAppearing(); if (WCSession.IsSupported) { var session = WCSession.DefaultSession; Console.WriteLine($"配对状态: {session.Paired}"); Console.WriteLine($"WatchApp安装状态: {session.WatchAppInstalled}"); Console.WriteLine($"可连接状态: {session.Reachable}"); Console.WriteLine($"配对App Bundle ID: {session.CompanionAppBundleIdentifier}"); } }
若session.CompanionAppBundleIdentifier与Watch App的Bundle ID不匹配,说明配置仍存在问题。
总结
核心问题大概率是MAUI配置缺失或WCSession实现不完整,优先排查签名配置和IWCSessionDelegate的绑定逻辑。若问题仍存在,可对比Xcode原生iOS App与MAUI iOS App的Info.plist、Entitlements文件差异,或用Xcode打开MAUI的iOS构建项目(Platforms/iOS/bin/Debug/net10-ios/下的.xcodeproj)检查配置一致性。




