收到令牌时的SFSafariViewController通知

时间:2019-04-04 21:54:37

标签: c# xamarin paypal sfsafariviewcontrollerdelegate

将以下快速示例中的代码转换为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#代码触发。可能缺少步骤或无效的代码转换。

1 个答案:

答案 0 :(得分:-1)

如果Safari控制器没有关闭并且您设置了正确的UrlScheme(),则您缺少OpenUrl类中的AppDelegate替代项,该替代项监听应用程序的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;
}