收到令牌时的 SFSafariViewController 通知
SFSafariViewController notification when token received
将以下 swift 示例中的代码转换为 C# (Xamarin),以便在 SFSafariViewController 中收到 Paypal 令牌时通知应用程序,但它不会触发该方法。
如下所示将 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# 代码中。可能缺少步骤或无效代码转换。
如果 Safari 控制器没有关闭并且您设置了正确的 UrlScheme (),那么您在 AppDelegate
class 中缺少 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;
}
将以下 swift 示例中的代码转换为 C# (Xamarin),以便在 SFSafariViewController 中收到 Paypal 令牌时通知应用程序,但它不会触发该方法。
如下所示将 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# 代码中。可能缺少步骤或无效代码转换。
如果 Safari 控制器没有关闭并且您设置了正确的 UrlScheme (),那么您在 AppDelegate
class 中缺少 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;
}