Skip to content

Commit 5fffef4

Browse files
authored
Replace SafariServices with ASWebAuthenticationSession for iOS Google Login (#125)
1 parent b83059f commit 5fffef4

File tree

10 files changed

+299
-66
lines changed

10 files changed

+299
-66
lines changed

Assets/Plugins/iOS/Common.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef Common_h
2+
#define Common_h
3+
4+
typedef int bool_t;
5+
6+
inline bool_t toBool(bool v)
7+
{
8+
return v ? 1 : 0;
9+
}
10+
11+
inline bool toBool(bool_t v)
12+
{
13+
return v != 0;
14+
}
15+
16+
inline NSString* toString(const char* string)
17+
{
18+
if (string != NULL)
19+
{
20+
return [NSString stringWithUTF8String:string];
21+
}
22+
else
23+
{
24+
return [NSString stringWithUTF8String:""];
25+
}
26+
}
27+
28+
inline char* toString(NSString* string)
29+
{
30+
const char* cstr = [string UTF8String];
31+
32+
if (cstr == NULL)
33+
return NULL;
34+
35+
char* copy = (char*)malloc(strlen(cstr) + 1);
36+
strcpy(copy, cstr);
37+
return copy;
38+
}
39+
40+
#endif /* Common_h */

Assets/Plugins/iOS/Common.h.meta

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

Assets/Plugins/iOS/iOSBrowser.m

Lines changed: 0 additions & 30 deletions
This file was deleted.

Assets/Plugins/iOS/iOSBrowser.mm

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

Assets/Plugins/iOS/iOSBrowser.m.meta renamed to Assets/Plugins/iOS/iOSBrowser.mm.meta

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

Assets/Thirdweb/Core/Scripts/Browser/AndroidBrowser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if UNITY_ANDROID
1+
#if UNITY_ANDROID && !UNITY_EDITOR
22

33
using System;
44
using System.Threading;

Assets/Thirdweb/Core/Scripts/Browser/CrossPlatformBrowser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class CrossPlatformBrowser : IThirdwebBrowser
99

1010
public async Task<BrowserResult> Login(string loginUrl, string redirectUrl, CancellationToken cancellationToken = default)
1111
{
12-
#if UNITY_ANDROID
12+
#if UNITY_ANDROID && !UNITY_EDITOR
1313
_browser = new AndroidBrowser();
14-
#elif UNITY_IOS
14+
#elif UNITY_IOS && !UNITY_EDITOR
1515
_browser = new IOSBrowser();
1616
#else
1717
_browser = new StandaloneBrowser();

0 commit comments

Comments
 (0)