我在 Xamarin iOS 中使用以下类进行身份验证,但我无法使用它在 Xamarin macOS 中,因为无法解析SFSafariViewController
、SFSafariViewControllerDelegate
和UIViewController
。
谁能帮我用什么来替换这些类,以便在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);
}
}
SafariServices"在macOS上不可用。
WebKit的WKWebView
和WebView
是,如果您的目标是OS X 10.10及更高版本,Apple建议使用WKWebView
。
回复:https://developer.apple.com/documentation/webkit/wkwebview?language=objc