C# 收到令牌时的SFSafariViewController通知

C# 收到令牌时的SFSafariViewController通知,c#,xamarin,paypal,sfsafariviewcontrollerdelegate,C#,Xamarin,Paypal,Sfsafariviewcontrollerdelegate,将以下swift示例中的代码转换为C Xamarin,以便在SFSafariViewController内收到Paypal令牌时通知应用程序,但它不会触发该方法 将swift转换为C,如下所示,但在用户登录PayPal并收到令牌后,Safari不会关闭fire SetupMerchant UrlSchemes也被设置为retailsdksampleapp,以匹配PayPal的样例swift应用程序 SafariDelegate safariDelegate = new SafariDelegat

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

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

UrlSchemes也被设置为retailsdksampleapp,以匹配PayPal的样例swift应用程序

SafariDelegate safariDelegate = new SafariDelegate(this);
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("kCloseSafariViewControllerNotification"), safariDelegate.SetupMerchant);

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

如果Safari控制器未关闭且您设置了正确的URLSHEMA,则您缺少AppDelegate类中的OpenUrl覆盖,该类用于侦听应用程序的url方案并发布视图控制器正在侦听的通知

例子:
如果Safari控制器未关闭,并且您设置了正确的URLSHEMA,则您缺少AppDelegate类中的OpenUrl覆盖,该类用于侦听应用程序的url方案,并发布视图控制器正在侦听的通知

例子:
@我很高兴这有帮助@我很高兴这有帮助。。。
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();
    }
}
[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;
}