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
8 changes: 8 additions & 0 deletions Assets/Thirdweb/Plugins/macOS.meta

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

106 changes: 106 additions & 0 deletions Assets/Thirdweb/Plugins/macOS/MacBrowser.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#import <Cocoa/Cocoa.h>
#import <AuthenticationServices/AuthenticationServices.h>

#include "../iOS/Common.h"

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
{
self = [super init];
if (self)
{
_session = [[ASWebAuthenticationSession alloc] initWithURL:URL
callbackURLScheme:callbackURLScheme
completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error)
{
if (error != nil)
{
NSLog(@"[ASWebAuthenticationSession:CompletionHandler] %@", error.description);
}

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

if (@available(macOS 10.15, *))
{
_session.presentationContextProvider = self;
}
}
return self;
}

- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session
{
if (@available(macOS 10.15, *))
{
NSWindow* anchor = [NSApplication sharedApplication].keyWindow;
if (anchor == nil)
{
anchor = [NSApplication sharedApplication].mainWindow;
}
if (anchor == nil)
{
anchor = [NSApplication sharedApplication].windows.firstObject;
}
return anchor;
}
return nil;
}

@end

extern "C"
{
Thirdweb_ASWebAuthenticationSession* Thirdweb_ASWebAuthenticationSession_InitWithURL(
const char* urlStr, const char* urlSchemeStr, ASWebAuthenticationSessionCompletionCallback completionCallback)
{
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];
return toBool(started);
}

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

int Thirdweb_ASWebAuthenticationSession_GetPrefersEphemeralWebBrowserSession(void* sessionPtr)
{
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
if (@available(macOS 10.15, *))
{
return toBool([[session session] prefersEphemeralWebBrowserSession]);
}
return 0;
}

void Thirdweb_ASWebAuthenticationSession_SetPrefersEphemeralWebBrowserSession(void* sessionPtr, int enable)
{
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
if (@available(macOS 10.15, *))
{
[[session session] setPrefersEphemeralWebBrowserSession:toBool(enable)];
}
}
}
84 changes: 84 additions & 0 deletions Assets/Thirdweb/Plugins/macOS/MacBrowser.mm.meta

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

17 changes: 17 additions & 0 deletions Assets/Thirdweb/Plugins/macOS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# macOS OAuth Plugin

The native bridge in this folder (`libMacBrowser.dylib`) is a universal binary (arm64 + x86_64) built from `MacBrowser.mm`.

## Rebuild steps

Run these commands from the repository root on macOS:

```bash
cd Assets/Thirdweb/Plugins/macOS
clang++ -std=c++17 -ObjC++ -fmodules -Wall -Werror -arch arm64 -framework Cocoa -framework AuthenticationServices -dynamiclib MacBrowser.mm -o /tmp/libMacBrowser_arm64.dylib
clang++ -std=c++17 -ObjC++ -fmodules -Wall -Werror -arch x86_64 -framework Cocoa -framework AuthenticationServices -dynamiclib MacBrowser.mm -o /tmp/libMacBrowser_x86_64.dylib
lipo -create /tmp/libMacBrowser_arm64.dylib /tmp/libMacBrowser_x86_64.dylib -output libMacBrowser.dylib
rm /tmp/libMacBrowser_arm64.dylib /tmp/libMacBrowser_x86_64.dylib
```

After rebuilding, re-run your Unity macOS build to make sure the new binary is picked up.
7 changes: 7 additions & 0 deletions Assets/Thirdweb/Plugins/macOS/README.md.meta

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

Binary file added Assets/Thirdweb/Plugins/macOS/libMacBrowser.dylib
Binary file not shown.
83 changes: 83 additions & 0 deletions Assets/Thirdweb/Plugins/macOS/libMacBrowser.dylib.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public CrossPlatformUnityBrowser(string htmlOverride = null)
_unityBrowser = new AndroidBrowser();
#elif UNITY_IOS
_unityBrowser = new IOSBrowser();
#elif UNITY_STANDALONE_OSX
_unityBrowser = new MacBrowser();
#else
_unityBrowser = new InAppWalletBrowser(htmlOverride);
#endif
Expand Down
Loading