Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1172 from dalexsoto/alex-WebAuthenticator
Browse files Browse the repository at this point in the history
[WebAuthenticator] Break iOS 13 dependency on WebAuthenticator
  • Loading branch information
Redth authored Mar 26, 2020
2 parents 34cc39b + 743c959 commit 351d96d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Xamarin.Essentials/WebAuthenticator/WebAuthenticator.ios.tvos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ namespace Xamarin.Essentials
{
public static partial class WebAuthenticator
{
#if __IOS__
[System.Runtime.InteropServices.DllImport(ObjCRuntime.Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Required for iOS Export")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:Element should begin with upper-case letter", Justification = "Required for iOS Export")]
static extern void void_objc_msgSend_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1);
#endif

static TaskCompletionSource<WebAuthenticatorResult> tcsResponse;
static UIViewController currentViewController;
static Uri redirectUri;
Expand Down Expand Up @@ -49,7 +56,13 @@ void AuthSessionCallback(NSUrl cbUrl, NSError error)
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
var was = new ASWebAuthenticationSession(new NSUrl(url.OriginalString), scheme, AuthSessionCallback);
was.PresentationContextProvider = new ContextProvider(Platform.GetCurrentWindow());

if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
var ctx = new ContextProvider(Platform.GetCurrentWindow());
void_objc_msgSend_IntPtr(was.Handle, ObjCRuntime.Selector.GetHandle("setPresentationContextProvider:"), ctx.Handle);
}

was.Start();
return await tcsResponse.Task;
}
Expand Down Expand Up @@ -175,13 +188,15 @@ public override void DidFinish(SFSafariViewController controller) =>
DidFinishHandler?.Invoke(controller);
}

class ContextProvider : NSObject, IASWebAuthenticationPresentationContextProviding
[ObjCRuntime.Adopts("ASWebAuthenticationPresentationContextProviding")]
class ContextProvider : NSObject
{
public ContextProvider(UIWindow window) =>
Window = window;

public UIWindow Window { get; private set; }

[Export("presentationAnchorForWebAuthenticationSession:")]
public UIWindow GetPresentationAnchor(ASWebAuthenticationSession session)
=> Window;
}
Expand Down

0 comments on commit 351d96d

Please sign in to comment.