首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CarPlay停车应用程序在从Xcode 12 CarPlay模拟器启动时崩溃

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

Stack Overflow用户
提问于 2020-10-06 04:08:59
回答 1查看 451关注 0票数 1

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

下面是Info.plist场景配置:

代码语言:javascript
复制
    <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项目的根文件夹下)

代码语言:javascript
复制
    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时,如下所示

代码语言:javascript
复制
public override UISceneConfiguration GetConfiguration(UIApplication application, UISceneSession connectingSceneSession, UISceneConnectionOptions options)
{
...
}

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

用于connectingSceneSession检查的截图

如果继续,则应用程序将抛出一个异常,该异常似乎提示SceneDelegate未正确加载:异常

:Visual for MacVersion8.7.8 Xamarin.iOS 14.0.0 Xcode 12.0

Xamarin.ios 14在绑定iOS库时似乎遗漏了一些东西。任何人都有类似的问题。我是否做错了什么,或者是否有其他方法可以在Xamarin.Forms/Xamarin.iOS上实现移动应用程序的CarPlay部件特性?

感谢您的评论或帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-08 06:45:45

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

  1. 为两种情况创建场景配置的AppDelegate (CarPlay和phone)
代码语言:javascript
复制
        [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委托来处理常规的移动场景窗口
代码语言:javascript
复制
    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();
            }
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64219232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档