|
| 1 | +#import <AuthenticationServices/AuthenticationServices.h> |
| 2 | + |
| 3 | +#include "Common.h" |
| 4 | + |
| 5 | +extern UIViewController* UnityGetGLViewController(); |
| 6 | + |
| 7 | +typedef void (*ASWebAuthenticationSessionCompletionCallback)(void* sessionPtr, const char* callbackUrl, int errorCode, const char* errorMessage); |
| 8 | + |
| 9 | +@interface Thirdweb_ASWebAuthenticationSession : NSObject<ASWebAuthenticationPresentationContextProviding> |
| 10 | + |
| 11 | +@property (readonly, nonatomic)ASWebAuthenticationSession* session; |
| 12 | + |
| 13 | +@end |
| 14 | + |
| 15 | +@implementation Thirdweb_ASWebAuthenticationSession |
| 16 | + |
| 17 | +- (instancetype)initWithURL:(NSURL *)URL callbackURLScheme:(nullable NSString *)callbackURLScheme completionCallback:(ASWebAuthenticationSessionCompletionCallback)completionCallback |
| 18 | +{ |
| 19 | + _session = [[ASWebAuthenticationSession alloc] initWithURL:URL |
| 20 | + callbackURLScheme: callbackURLScheme |
| 21 | + completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) |
| 22 | + { |
| 23 | + if (error != nil) |
| 24 | + { |
| 25 | + NSLog(@"[ASWebAuthenticationSession:CompletionHandler] %@", error.description); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + //NSLog(@"[ASWebAuthenticationSession:CompletionHandler] Callback URL: %@", callbackURL); |
| 30 | + } |
| 31 | + |
| 32 | + completionCallback((__bridge void*)self, toString(callbackURL.absoluteString), (int)error.code, toString(error.localizedDescription)); |
| 33 | + }]; |
| 34 | + |
| 35 | + [_session setPresentationContextProvider:self]; |
| 36 | + return self; |
| 37 | +} |
| 38 | + |
| 39 | +- (nonnull ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(nonnull ASWebAuthenticationSession *)session |
| 40 | +{ |
| 41 | + #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000 |
| 42 | + return [[[UIApplication sharedApplication] delegate] window]; |
| 43 | + #elif __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500 |
| 44 | + return [[NSApplication sharedApplication] mainWindow]; |
| 45 | + #else |
| 46 | + return nil; |
| 47 | + #endif |
| 48 | +} |
| 49 | + |
| 50 | +@end |
| 51 | + |
| 52 | +extern "C" |
| 53 | +{ |
| 54 | + Thirdweb_ASWebAuthenticationSession* Thirdweb_ASWebAuthenticationSession_InitWithURL( |
| 55 | + const char* urlStr, const char* urlSchemeStr, ASWebAuthenticationSessionCompletionCallback completionCallback) |
| 56 | + { |
| 57 | + //NSLog(@"[ASWebAuthenticationSession:InitWithURL] initWithURL: %s callbackURLScheme:%s", urlStr, urlSchemeStr); |
| 58 | + |
| 59 | + NSURL* url = [NSURL URLWithString: toString(urlStr)]; |
| 60 | + NSString* urlScheme = toString(urlSchemeStr); |
| 61 | + |
| 62 | + Thirdweb_ASWebAuthenticationSession* session = [[Thirdweb_ASWebAuthenticationSession alloc] initWithURL:url |
| 63 | + callbackURLScheme: urlScheme |
| 64 | + completionCallback:completionCallback]; |
| 65 | + return session; |
| 66 | + } |
| 67 | + |
| 68 | + int Thirdweb_ASWebAuthenticationSession_Start(void* sessionPtr) |
| 69 | + { |
| 70 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 71 | + BOOL started = [[session session] start]; |
| 72 | + |
| 73 | + //NSLog(@"[ASWebAuthenticationSession:Start]: %s", (started ? "YES" : "NO")); |
| 74 | + |
| 75 | + return toBool(started); |
| 76 | + } |
| 77 | + |
| 78 | + void Thirdweb_ASWebAuthenticationSession_Cancel(void* sessionPtr) |
| 79 | + { |
| 80 | + //NSLog(@"[ASWebAuthenticationSession:Cancel]"); |
| 81 | + |
| 82 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 83 | + [[session session] cancel]; |
| 84 | + } |
| 85 | + |
| 86 | + int Thirdweb_ASWebAuthenticationSession_GetPrefersEphemeralWebBrowserSession(void* sessionPtr) |
| 87 | + { |
| 88 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 89 | + return toBool([[session session] prefersEphemeralWebBrowserSession]); |
| 90 | + } |
| 91 | + |
| 92 | + void Thirdweb_ASWebAuthenticationSession_SetPrefersEphemeralWebBrowserSession(void* sessionPtr, int enable) |
| 93 | + { |
| 94 | + Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr; |
| 95 | + [[session session] setPrefersEphemeralWebBrowserSession:toBool(enable)]; |
| 96 | + } |
| 97 | +} |
0 commit comments