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

问题描述

我们正在扩展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”是应用包名称

AppSceneDelegateImp类(在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 for Mac版本8.7.8 Xamarin.iOS 14.0.0 Xcode 12.0

似乎Xamarin.ios 14绑定iOS库时缺少某些内容。任何人都有类似的问题。我做错了什么吗?还是可以通过Xcode / swift实现CarPlay零件功能,同时又将移动应用程序保持在Xamarin.Forms / Xamarin.iOS上?

感谢任何评论或帮助。

解决方法

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

  1. AppDelegate为两种情况(CarPlay和电话)创建场景配置

        [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委托来处理常规的移动场景窗口
    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();
            }
        }
    }