【问题标题】:SFSafariViewController notification when token received收到令牌时的 SFSafariViewController 通知
【发布时间】:2019-08-26 18:32:51
【问题描述】:

将以下 swift 示例中的代码转换为 C# (Xamarin) 以在 SFSafariViewController 中收到 Paypal 令牌但不触发该方法时通知应用程序。

https://github.com/paypal/paypal-here-sdk-ios-distribution/blob/master/SampleApp/PPHSDKSampleApp/InitializeViewController.swift

如下所示将 swift 转换为 C#,但在用户登录 PayPal 并收到令牌后,Safari 不会关闭以触发 SetupMerchant()

UrlSchemes 也设置为 Retailsdksampleapp 以匹配来自 PayPal 的示例 swift 应用程序。

SafariDelegate safariDelegate = new SafariDelegate(this);
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("kCloseSafariViewControllerNotification"), safariDelegate.SetupMerchant);
void loadBrowser()
{
    var url = new NSUrl("https://paypalauth.herokuapp.com/toPayPal/" + Application.paypalEnvironment + "?returnTokenOnQueryString=true");
    var svc = new SFSafariViewController(url);
    svc.Delegate = safariDelegate;
    this.PresentViewController(svc, true, null);
}
public class SafariDelegate : SFSafariViewControllerDelegate
{
    ViewController _controller = null;
    public SafariDelegate(ViewController controller)
    {
        _controller = controller;
    }

    public void SetupMerchant(NSNotification notification)
    {
        // Dismiss the SFSafariViewController when the notification of token has been received.
            this._controller.PresentedViewController?.DismissViewController(true, () => { });

        // Grab the token(s) from the notification and pass it into the merchant initialize call to set up
        // the merchant.  Upon successful initialization, the 'Connect Card Reader' button will be
        // enabled for use.
        var accessToken = notification.Object.ToString();
    }
}

从 Paypal 运行 swift 示例应用程序时,它会在登录后关闭浏览器 (SFSafariViewController) 并触发 SetupMerchant() 但不在 C# 代码中。可能是缺少步骤或代码转换无效。

【问题讨论】:

    标签: c# xamarin paypal sfsafariviewcontrollerdelegate


    【解决方案1】:

    如果 Safari 控制器没有关闭并且您设置了正确的 UrlScheme (),那么您在 AppDelegate 类中缺少 OpenUrl 覆盖,该类监听您的应用程序的 url 方案并发布您的视图控制器的通知正在听。

    示例:

    [Export("application:openURL:sourceApplication:annotation:")]
    public bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
    {
        if (sourceApplication == "com.apple.SafariViewService")
        {
            var dict = HttpUtility.ParseQueryString(url.Query);
            var token = dict["access_token"];
            NSNotificationCenter.DefaultCenter.PostNotificationName("kCloseSafariViewControllerNotification", new NSString(token));
        };
        return true;
    }
    

    【讨论】:

    • @HY 很高兴它有帮助...?
    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多