Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Assets/Plugins/iOS/Common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef Common_h
#define Common_h

typedef int bool_t;

inline bool_t toBool(bool v)
{
return v ? 1 : 0;
}

inline bool toBool(bool_t v)
{
return v != 0;
}

inline NSString* toString(const char* string)
{
if (string != NULL)
{
return [NSString stringWithUTF8String:string];
}
else
{
return [NSString stringWithUTF8String:""];
}
}

inline char* toString(NSString* string)
{
const char* cstr = [string UTF8String];

if (cstr == NULL)
return NULL;

char* copy = (char*)malloc(strlen(cstr) + 1);
strcpy(copy, cstr);
return copy;
}

#endif /* Common_h */
33 changes: 33 additions & 0 deletions Assets/Plugins/iOS/Common.h.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 0 additions & 30 deletions Assets/Plugins/iOS/iOSBrowser.m

This file was deleted.

97 changes: 97 additions & 0 deletions Assets/Plugins/iOS/iOSBrowser.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#import <AuthenticationServices/AuthenticationServices.h>

#include "Common.h"

extern UIViewController* UnityGetGLViewController();

typedef void (*ASWebAuthenticationSessionCompletionCallback)(void* sessionPtr, const char* callbackUrl, int errorCode, const char* errorMessage);

@interface Thirdweb_ASWebAuthenticationSession : NSObject<ASWebAuthenticationPresentationContextProviding>

@property (readonly, nonatomic)ASWebAuthenticationSession* session;

@end

@implementation Thirdweb_ASWebAuthenticationSession

- (instancetype)initWithURL:(NSURL *)URL callbackURLScheme:(nullable NSString *)callbackURLScheme completionCallback:(ASWebAuthenticationSessionCompletionCallback)completionCallback
{
_session = [[ASWebAuthenticationSession alloc] initWithURL:URL
callbackURLScheme: callbackURLScheme
completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error)
{
if (error != nil)
{
NSLog(@"[ASWebAuthenticationSession:CompletionHandler] %@", error.description);
}
else
{
//NSLog(@"[ASWebAuthenticationSession:CompletionHandler] Callback URL: %@", callbackURL);
}

completionCallback((__bridge void*)self, toString(callbackURL.absoluteString), (int)error.code, toString(error.localizedDescription));
}];

[_session setPresentationContextProvider:self];
return self;
}

- (nonnull ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(nonnull ASWebAuthenticationSession *)session
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000
return [[[UIApplication sharedApplication] delegate] window];
#elif __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
return [[NSApplication sharedApplication] mainWindow];
#else
return nil;
#endif
}

@end

extern "C"
{
Thirdweb_ASWebAuthenticationSession* Thirdweb_ASWebAuthenticationSession_InitWithURL(
const char* urlStr, const char* urlSchemeStr, ASWebAuthenticationSessionCompletionCallback completionCallback)
{
//NSLog(@"[ASWebAuthenticationSession:InitWithURL] initWithURL: %s callbackURLScheme:%s", urlStr, urlSchemeStr);

NSURL* url = [NSURL URLWithString: toString(urlStr)];
NSString* urlScheme = toString(urlSchemeStr);

Thirdweb_ASWebAuthenticationSession* session = [[Thirdweb_ASWebAuthenticationSession alloc] initWithURL:url
callbackURLScheme: urlScheme
completionCallback:completionCallback];
return session;
}

int Thirdweb_ASWebAuthenticationSession_Start(void* sessionPtr)
{
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
BOOL started = [[session session] start];

//NSLog(@"[ASWebAuthenticationSession:Start]: %s", (started ? "YES" : "NO"));

return toBool(started);
}

void Thirdweb_ASWebAuthenticationSession_Cancel(void* sessionPtr)
{
//NSLog(@"[ASWebAuthenticationSession:Cancel]");

Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
[[session session] cancel];
}

int Thirdweb_ASWebAuthenticationSession_GetPrefersEphemeralWebBrowserSession(void* sessionPtr)
{
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
return toBool([[session session] prefersEphemeralWebBrowserSession]);
}

void Thirdweb_ASWebAuthenticationSession_SetPrefersEphemeralWebBrowserSession(void* sessionPtr, int enable)
{
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
[[session session] setPrefersEphemeralWebBrowserSession:toBool(enable)];
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Thirdweb/Core/Scripts/Browser/AndroidBrowser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if UNITY_ANDROID
#if UNITY_ANDROID && !UNITY_EDITOR

using System;
using System.Threading;
Expand Down
4 changes: 2 additions & 2 deletions Assets/Thirdweb/Core/Scripts/Browser/CrossPlatformBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class CrossPlatformBrowser : IThirdwebBrowser

public async Task<BrowserResult> Login(string loginUrl, string redirectUrl, CancellationToken cancellationToken = default)
{
#if UNITY_ANDROID
#if UNITY_ANDROID && !UNITY_EDITOR
_browser = new AndroidBrowser();
#elif UNITY_IOS
#elif UNITY_IOS && !UNITY_EDITOR
_browser = new IOSBrowser();
#else
_browser = new StandaloneBrowser();
Expand Down
Loading