Skip to content

Commit 695d774

Browse files
authored
Add macOS OAuth browser support and platform integration (#241)
1 parent 1856b75 commit 695d774

File tree

10 files changed

+401
-10
lines changed

10 files changed

+401
-10
lines changed

Assets/Thirdweb/Plugins/macOS.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#import <Cocoa/Cocoa.h>
2+
#import <AuthenticationServices/AuthenticationServices.h>
3+
4+
#include "../iOS/Common.h"
5+
6+
typedef void (*ASWebAuthenticationSessionCompletionCallback)(void* sessionPtr, const char* callbackUrl, int errorCode, const char* errorMessage);
7+
8+
@interface Thirdweb_ASWebAuthenticationSession : NSObject<ASWebAuthenticationPresentationContextProviding>
9+
10+
@property (readonly, nonatomic) ASWebAuthenticationSession* session;
11+
12+
@end
13+
14+
@implementation Thirdweb_ASWebAuthenticationSession
15+
16+
- (instancetype)initWithURL:(NSURL *)URL callbackURLScheme:(nullable NSString *)callbackURLScheme completionCallback:(ASWebAuthenticationSessionCompletionCallback)completionCallback
17+
{
18+
self = [super init];
19+
if (self)
20+
{
21+
_session = [[ASWebAuthenticationSession alloc] initWithURL:URL
22+
callbackURLScheme:callbackURLScheme
23+
completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error)
24+
{
25+
if (error != nil)
26+
{
27+
NSLog(@"[ASWebAuthenticationSession:CompletionHandler] %@", error.description);
28+
}
29+
30+
completionCallback((__bridge void*)self, toString(callbackURL.absoluteString), (int)error.code, toString(error.localizedDescription));
31+
}];
32+
33+
if (@available(macOS 10.15, *))
34+
{
35+
_session.presentationContextProvider = self;
36+
}
37+
}
38+
return self;
39+
}
40+
41+
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session
42+
{
43+
if (@available(macOS 10.15, *))
44+
{
45+
NSWindow* anchor = [NSApplication sharedApplication].keyWindow;
46+
if (anchor == nil)
47+
{
48+
anchor = [NSApplication sharedApplication].mainWindow;
49+
}
50+
if (anchor == nil)
51+
{
52+
anchor = [NSApplication sharedApplication].windows.firstObject;
53+
}
54+
return anchor;
55+
}
56+
return nil;
57+
}
58+
59+
@end
60+
61+
extern "C"
62+
{
63+
Thirdweb_ASWebAuthenticationSession* Thirdweb_ASWebAuthenticationSession_InitWithURL(
64+
const char* urlStr, const char* urlSchemeStr, ASWebAuthenticationSessionCompletionCallback completionCallback)
65+
{
66+
NSURL* url = [NSURL URLWithString: toString(urlStr)];
67+
NSString* urlScheme = toString(urlSchemeStr);
68+
69+
Thirdweb_ASWebAuthenticationSession* session = [[Thirdweb_ASWebAuthenticationSession alloc] initWithURL:url
70+
callbackURLScheme:urlScheme
71+
completionCallback:completionCallback];
72+
return session;
73+
}
74+
75+
int Thirdweb_ASWebAuthenticationSession_Start(void* sessionPtr)
76+
{
77+
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
78+
BOOL started = [[session session] start];
79+
return toBool(started);
80+
}
81+
82+
void Thirdweb_ASWebAuthenticationSession_Cancel(void* sessionPtr)
83+
{
84+
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
85+
[[session session] cancel];
86+
}
87+
88+
int Thirdweb_ASWebAuthenticationSession_GetPrefersEphemeralWebBrowserSession(void* sessionPtr)
89+
{
90+
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
91+
if (@available(macOS 10.15, *))
92+
{
93+
return toBool([[session session] prefersEphemeralWebBrowserSession]);
94+
}
95+
return 0;
96+
}
97+
98+
void Thirdweb_ASWebAuthenticationSession_SetPrefersEphemeralWebBrowserSession(void* sessionPtr, int enable)
99+
{
100+
Thirdweb_ASWebAuthenticationSession* session = (__bridge Thirdweb_ASWebAuthenticationSession*) sessionPtr;
101+
if (@available(macOS 10.15, *))
102+
{
103+
[[session session] setPrefersEphemeralWebBrowserSession:toBool(enable)];
104+
}
105+
}
106+
}

Assets/Thirdweb/Plugins/macOS/MacBrowser.mm.meta

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# macOS OAuth Plugin
2+
3+
The native bridge in this folder (`libMacBrowser.dylib`) is a universal binary (arm64 + x86_64) built from `MacBrowser.mm`.
4+
5+
## Rebuild steps
6+
7+
Run these commands from the repository root on macOS:
8+
9+
```bash
10+
cd Assets/Thirdweb/Plugins/macOS
11+
clang++ -std=c++17 -ObjC++ -fmodules -Wall -Werror -arch arm64 -framework Cocoa -framework AuthenticationServices -dynamiclib MacBrowser.mm -o /tmp/libMacBrowser_arm64.dylib
12+
clang++ -std=c++17 -ObjC++ -fmodules -Wall -Werror -arch x86_64 -framework Cocoa -framework AuthenticationServices -dynamiclib MacBrowser.mm -o /tmp/libMacBrowser_x86_64.dylib
13+
lipo -create /tmp/libMacBrowser_arm64.dylib /tmp/libMacBrowser_x86_64.dylib -output libMacBrowser.dylib
14+
rm /tmp/libMacBrowser_arm64.dylib /tmp/libMacBrowser_x86_64.dylib
15+
```
16+
17+
After rebuilding, re-run your Unity macOS build to make sure the new binary is picked up.

Assets/Thirdweb/Plugins/macOS/README.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
86.5 KB
Binary file not shown.

Assets/Thirdweb/Plugins/macOS/libMacBrowser.dylib.meta

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Runtime/Unity/Browser/CrossPlatformUnityBrowser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public CrossPlatformUnityBrowser(string htmlOverride = null)
3737
_unityBrowser = new AndroidBrowser();
3838
#elif UNITY_IOS
3939
_unityBrowser = new IOSBrowser();
40+
#elif UNITY_STANDALONE_OSX
41+
_unityBrowser = new MacBrowser();
4042
#else
4143
_unityBrowser = new InAppWalletBrowser(htmlOverride);
4244
#endif

0 commit comments

Comments
 (0)