【问题标题】:Converting Xamarin iOS to Xamarin macOS将 Xamarin iOS 转换为 Xamarin macOS
【发布时间】:2018-04-23 03:06:03
【问题描述】:

我在 Xamarin iOS 中使用以下类进行身份验证,但我无法在 Xamarin macOS 中使用它,因为无法解析 SFSafariViewControllerSFSafariViewControllerDelegateUIViewController

谁能帮我用什么来替换这些类,以便在 Xamarin macOS 应用程序中使用它?

public class PlatformWebView : SFSafariViewControllerDelegate, IBrowser
    {
    private SafariServices.SFSafariViewController m_Safari;
    private readonly UIViewController r_Controller;

    public PlatformWebView(UIViewController i_Controller)
    {
        r_Controller = i_Controller;
    }

    public override void DidFinish(SFSafariViewController i_Controller)
    {
        ActivityMediator.Instance.Send("UserCancel");
    }

    public Task<BrowserResult> InvokeAsync(BrowserOptions i_Options)
    {
        if (string.IsNullOrWhiteSpace(i_Options.StartUrl))
        {
            throw new ArgumentException("Missing StartUrl", nameof(i_Options));
        }

        if (string.IsNullOrWhiteSpace(i_Options.EndUrl))
        {
            throw new ArgumentException("Missing EndUrl", nameof(i_Options));
        }

        // must be able to wait for the intent to be finished to continue
        // with setting the task result
        TaskCompletionSource<BrowserResult> tcs = new TaskCompletionSource<BrowserResult>();

        // create Safari controller
        m_Safari = new SafariServices.SFSafariViewController(new NSUrl(i_Options.StartUrl));
        m_Safari.Delegate = this;

        ActivityMediator.MessageReceivedEventHandler callback = null;
        callback = async (i_Response) =>
        {
            // remove handler
            ActivityMediator.Instance.ActivityMessageReceived -= callback;

            if (i_Response == "UserCancel")
            {
                tcs.SetResult(new BrowserResult
                {
                    ResultType = BrowserResultType.UserCancel
                });
            }
            else
            {
                // Close Safari
                await m_Safari.DismissViewControllerAsync(true);

                // set result
                tcs.SetResult(new BrowserResult
                {
                    Response = i_Response,
                    ResultType = BrowserResultType.Success
                });
            }
        };

        // attach handler
        ActivityMediator.Instance.ActivityMessageReceived += callback;

        // launch Safari
        r_Controller.PresentViewController(m_Safari, true, null);

        // need an intent to be triggered when browsing to the "io.identitymodel.native://callback"
        // scheme/URI => CallbackInterceptorActivity
        return tcs.Task;
    }
}

public class ActivityMediator
    {
    public delegate void MessageReceivedEventHandler(string i_Message);

    private static ActivityMediator s_Instance;

    public static ActivityMediator Instance
    {
        get
        {
            if (s_Instance == null)
            {
                s_Instance = new ActivityMediator();
            }

            return s_Instance;
        }
    }

    private ActivityMediator()
    {
    }

    public event MessageReceivedEventHandler ActivityMessageReceived;

    public void Send(string i_Response)
    {
        ActivityMessageReceived?.Invoke(i_Response);
    }
}

【问题讨论】:

    标签: ios macos xamarin


    【解决方案1】:

    “SafariServices”在 macOS 上不可用。

    WebKit 的 WKWebViewWebView 是,如果您的目标是 OS X 10.10 及更高版本,Apple 建议使用 WKWebView

    回复:https://developer.apple.com/documentation/webkit/wkwebview?language=objc

    【讨论】:

    • 感谢您的回答,WKWebView 在它的 ctor 中需要一个 CGRect 我试图将来自 AppDelegate 的 NSWindow 框架属性传递给它。 WKWebView 已创建,但我不确定如何显示它,我正在使用 xamarin 表单,我想在加载 xaml 主页之前显示浏览器,或者以某种方式使用弹出窗口以便用户使用 Auth0 登录。我该怎么做,我尝试使用 AddSubView 但我只在 xaml 页面中看到内容?
    • @MaximRubchinsky 将其与 Xamarin.Forms 分离的最简单方法是创建一个新的 NSViewController 并嵌入您的 WKWebView 及其 WKNavigationDelegate 和 WKUIDelegate 子类来处理 OAuth2 流。网上有很多 WKWebView 演示,主要是针对 iOS 的,但它们可以在 macOS 上运行,几乎没有什么变化。然后这个 NSViewController 可以用作“弹出框”或完整的 ui 替代品。
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2019-04-11
    相关资源
    最近更新 更多