1

我们正在为 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 在此平台上没有关联的枚举值”。

connectedSceneSession 检查截图

如果继续,则应用程序将引发异常,这似乎表明 SceneDelegate 未正确加载: 异常

我的环境:Visual Studio for mac 版本 8.7.8 Xamarin.iOS 14.0.0 Xcode 12.0

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

感谢任何评论或帮助。

4

1 回答 1

1

在 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();
            }
        }
    }
于 2020-10-08T06:45:45.700 回答