xamarin-ios에서 auth에 다음 클래스를 사용하고 있지만 SFSafariViewController, SFSafariViewControllerDelegate 및 UIViewController를 확인할 수 없기 때문에 xamarin-macos를 사용할 수 없습니다. 아무도 나를 도와 나는 자 마린 - 맥 응용 프로그램에게 그것을xamarin-iOS에서 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);
}
}
감사합니다. WKWebView가 생성되었지만 표시 방법, xamarin 양식을 사용 중이고 xaml mainpage가로드되기 전에 브라우저를 표시하거나 사용자가 Auth0으로 로그인하기 위해 팝업을 어떻게 든 사용하고 싶습니다. 어떻게 할 수 있습니까? AddSubView를 사용해 보았지만 xaml 페이지에서만 contenet을 볼 수 있습니까? –
@MaximRubchinsky Xamarin.Forms에서 분리하는 가장 깨끗한 방법은 새 NSViewController를 만들고 WKWebView와 WKNavigationDelegate 및 WKUIDelegate 하위 클래스를 포함시켜 OAuth2 흐름을 처리하는 것입니다. 주로 iOS 용으로 WKWebView 데모가 웹에 많이 있지만 MacOS에서는 거의 변화가 없습니다. 그런 다음이 NSViewController는 "popover"또는 완전한 UI 교체로 사용될 수 있습니다. – SushiHangover