从 Xcode 12 CarPlay 模拟器启动时,CarPlay 停车应用程序崩溃

CarPlay parking app crashed when launching from Xcode 12 CarPlay simulator

我们正在为 CarPlay 扩展我们的 Xamarin.iOS 应用程序(通过 Xamarin.Forms)。打开 CarPlay 模拟器时,应用程序显示在 CarPlay 屏幕上,但从 CarPlay 模拟器启动时崩溃。

下面是Info.plist场景配置:

    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UISceneConfigurations</key>
        <dict>
            <key>CPTemplateApplicationSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneClassName</key>
                    <string>CPTemplateApplicationScene</string>
                    <key>UISceneConfigurationName</key>
                    <string>ParkingPlus-Car</string>
                    <key>UISceneDelegateClassName</key>
                    <string>ParkingPlus.AppSceneDelegateImp</string>
                </dict>
            </array>
        </dict>
    </dict>

“ParkingPlus”是应用程序包名称!

classAppSceneDelegateImp(在iOS项目的根文件夹下

    public class AppSceneDelegateImp : UIResponder, ICPTemplateApplicationSceneDelegate
    {
        private CPInterfaceController _interfaceController;

        public async void DidConnect(CPTemplateApplicationScene templateApplicationScene, CPInterfaceController interfaceController)
        {
            _interfaceController = interfaceController;
           .....
        }

        public void DidDisconnect(CPTemplateApplicationScene templateApplicationScene, CPInterfaceController interfaceController)
        {
            _interfaceController.Dispose();
            _interfaceController = null;
        }
   }

当我如下覆盖 AppDelegate.GetConfiguration 时

public override UISceneConfiguration GetConfiguration(UIApplication application, UISceneSession connectingSceneSession, UISceneConnectionOptions options)
{
...
}

当点击CarPlay上的应用程序图标时,将调用该方法。但是当我检查connectingSceneSession时,我发现变量成员内部有一些异常。 “CPTemplateApplicationSceneSessionRoleApplication 在此平台上没有关联的枚举值”。

Screenshot for connectingSceneSession inspection

如果继续,应用程序将抛出异​​常,提示 SceneDelegate 未正确加载: Exception

我的环境: Visual studio 对于 mac 版本 8.7.8 Xamarin.iOS 14.0.0 Xcode12.0

似乎 Xamarin.ios 14 在绑定 iOS 库时丢失了一些东西。任何人都有类似的问题。我做错了什么或者有没有其他方法可以通过 Xcode/swift 实现 CarPlay 部件功能,同时将移动应用程序保留在 Xamarin.Forms/Xamarin.iOS?

感谢任何意见或帮助。

在 Xamarin.ios 团队的帮助下,以下是此问题的完整解决方案: https://github.com/xamarin/xamarin-macios/issues/9749

  1. A​​ppDelegate创建场景配置两种情况(CarPlay和phone)

        [DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
        public extern static IntPtr IntPtr_objc_msgSend_IntPtr_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1, IntPtr arg2);

        public static UISceneConfiguration Create(string? name, NSString sessionRole)
        {
            global::UIKit.UIApplication.EnsureUIThread();
            var nsname = NSString.CreateNative(name);

            UISceneConfiguration sceneConfig;
            sceneConfig = Runtime.GetNSObject<UISceneConfiguration>(IntPtr_objc_msgSend_IntPtr_IntPtr(Class.GetHandle("UISceneConfiguration"), Selector.GetHandle("configurationWithName:sessionRole:"), nsname, sessionRole.Handle));
            NSString.ReleaseNative(nsname);
            //Because only the CarPlay scene will be here to create a scene configuration
            //We need manually assign the CarPlay scene delegate here!
            sceneConfig.DelegateType = typeof(AppCarSceneDelegateImp);
            return sceneConfig!;
        }

        [Export("application:configurationForConnectingSceneSession:options:")]
        public UISceneConfiguration GetConfiguration(UIApplication application, UISceneSession connectingSceneSession, UISceneConnectionOptions options)
        {
            UIWindowSceneSessionRole sessionRole;
            bool isCarPlaySceneSession = false;
            try
            {
                //When the connecting scene is a CarPlay scene, an expected exception will be thrown
                //Under this moment from Xamarin.iOS.
                sessionRole = connectingSceneSession.Role;
            }
            catch (NotSupportedException ex)
            {
                if (!string.IsNullOrEmpty(ex.Message) &&
                    ex.Message.Contains("CPTemplateApplicationSceneSessionRoleApplication"))
                {
                    isCarPlaySceneSession = true;
                }
            }

            if (isCarPlaySceneSession && UIDevice.CurrentDevice.CheckSystemVersion(14,0))
            {
                return Create("Car", CarPlay.CPTemplateApplicationScene.SessionRoleApplication);
            }
            else
            {
                //If it is phone scene, we need the regular UIWindow scene
                UISceneConfiguration phoneScene = new UISceneConfiguration("Phone", UIWindowSceneSessionRole.Application);
                //And assign the scene delegate here.
                phoneScene.DelegateType = typeof(AppWindowSceneDelegateImp);
                return phoneScene;
            }
        }
  1. 创建一个 UIWindowScene 委托来处理常规移动场景window
    public class AppWindowSceneDelegateImp : UISceneDelegate
    {
        public override void WillConnect(UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
        {
            var windowScene = scene as UIWindowScene;
            if (windowScene != null)
            {
                //Assign the Xamarin.iOS app window to this scene 
                UIApplication.SharedApplication.KeyWindow.WindowScene = windowScene;
                UIApplication.SharedApplication.KeyWindow.MakeKeyAndVisible();
            }
        }
    }