Skip to content

Commit b83059f

Browse files
authored
IThirdwebBrowser + iOS Google Login (#123)
1 parent f97bfb9 commit b83059f

File tree

111 files changed

+263
-3377
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+263
-3377
lines changed
Lines changed: 1 addition & 1 deletion
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#import <Foundation/Foundation.h>
2+
#import <SafariServices/SafariServices.h>
3+
#import <UIKit/UIKit.h>
4+
5+
@interface iOSBrowser : NSObject <SFSafariViewControllerDelegate>
6+
@end
7+
8+
@implementation iOSBrowser
9+
10+
static UIViewController* GetCurrentViewController() {
11+
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
12+
UIViewController *rootViewController = window.rootViewController;
13+
14+
UIViewController *currentController = rootViewController;
15+
while (currentController.presentedViewController) {
16+
currentController = currentController.presentedViewController;
17+
}
18+
return currentController;
19+
}
20+
21+
void _OpenURL(const char* url) {
22+
NSString *urlString = [NSString stringWithUTF8String:url];
23+
NSURL *nsURL = [NSURL URLWithString:urlString];
24+
25+
SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:nsURL];
26+
UIViewController *currentViewController = GetCurrentViewController();
27+
[currentViewController presentViewController:safariViewController animated:YES completion:nil];
28+
}
29+
30+
@end
Lines changed: 8 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Plugins/Authentication for Unity.meta renamed to Assets/Thirdweb/Core/Scripts/Browser.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#if UNITY_ANDROID
2+
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
namespace Thirdweb.Browser
9+
{
10+
public class AndroidBrowser : IThirdwebBrowser
11+
{
12+
private TaskCompletionSource<BrowserResult> _taskCompletionSource;
13+
14+
private string _customScheme;
15+
16+
public async Task<BrowserResult> Login(string loginUrl, string customScheme, CancellationToken cancellationToken = default)
17+
{
18+
_taskCompletionSource = new TaskCompletionSource<BrowserResult>();
19+
20+
cancellationToken.Register(() =>
21+
{
22+
_taskCompletionSource?.TrySetCanceled();
23+
});
24+
25+
_customScheme = customScheme;
26+
27+
Application.deepLinkActivated += OnDeepLinkActivated;
28+
29+
try
30+
{
31+
OpenURL(loginUrl);
32+
var completedTask = await Task.WhenAny(_taskCompletionSource.Task, Task.Delay(TimeSpan.FromSeconds(60)));
33+
if (completedTask == _taskCompletionSource.Task)
34+
{
35+
return await _taskCompletionSource.Task;
36+
}
37+
else
38+
{
39+
return new BrowserResult(BrowserStatus.Timeout, null, "The operation timed out.");
40+
}
41+
}
42+
finally
43+
{
44+
Application.deepLinkActivated -= OnDeepLinkActivated;
45+
}
46+
}
47+
48+
private void OpenURL(string url)
49+
{
50+
AndroidJavaClass thirdwebActivityClass = new("com.unity3d.player.UnityPlayer");
51+
AndroidJavaObject thirdwebActivity = thirdwebActivityClass.GetStatic<AndroidJavaObject>("currentActivity");
52+
thirdwebActivity.Call("OpenCustomTab", url);
53+
}
54+
55+
private void OnDeepLinkActivated(string url)
56+
{
57+
if (!url.StartsWith(_customScheme))
58+
return;
59+
60+
_taskCompletionSource.SetResult(new BrowserResult(BrowserStatus.Success, url));
61+
}
62+
}
63+
}
64+
65+
#endif
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace Thirdweb.Browser
5+
{
6+
public class CrossPlatformBrowser : IThirdwebBrowser
7+
{
8+
private IThirdwebBrowser _browser;
9+
10+
public async Task<BrowserResult> Login(string loginUrl, string redirectUrl, CancellationToken cancellationToken = default)
11+
{
12+
#if UNITY_ANDROID
13+
_browser = new AndroidBrowser();
14+
#elif UNITY_IOS
15+
_browser = new IOSBrowser();
16+
#else
17+
_browser = new StandaloneBrowser();
18+
#endif
19+
20+
return await _browser.Login(loginUrl, redirectUrl, cancellationToken);
21+
}
22+
}
23+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#if UNITY_IOS
2+
3+
using System;
4+
using System.Runtime.InteropServices;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using UnityEngine;
8+
9+
namespace Thirdweb.Browser
10+
{
11+
public class IOSBrowser : IThirdwebBrowser
12+
{
13+
private TaskCompletionSource<BrowserResult> _taskCompletionSource;
14+
15+
private string _customScheme;
16+
17+
public async Task<BrowserResult> Login(string loginUrl, string customScheme, CancellationToken cancellationToken = default)
18+
{
19+
_taskCompletionSource = new TaskCompletionSource<BrowserResult>();
20+
21+
cancellationToken.Register(() =>
22+
{
23+
_taskCompletionSource?.TrySetCanceled();
24+
});
25+
26+
_customScheme = customScheme;
27+
28+
Application.deepLinkActivated += OnDeepLinkActivated;
29+
30+
try
31+
{
32+
OpenURL(loginUrl);
33+
var completedTask = await Task.WhenAny(_taskCompletionSource.Task, Task.Delay(TimeSpan.FromSeconds(60)));
34+
if (completedTask == _taskCompletionSource.Task)
35+
{
36+
return await _taskCompletionSource.Task;
37+
}
38+
else
39+
{
40+
return new BrowserResult(BrowserStatus.Timeout, null, "The operation timed out.");
41+
}
42+
}
43+
finally
44+
{
45+
Application.deepLinkActivated -= OnDeepLinkActivated;
46+
}
47+
}
48+
49+
[DllImport("__Internal")]
50+
private static extern void _OpenURL(string url);
51+
52+
public void OpenURL(string url)
53+
{
54+
_OpenURL(url);
55+
}
56+
57+
private void OnDeepLinkActivated(string url)
58+
{
59+
if (!url.StartsWith(_customScheme))
60+
return;
61+
62+
_taskCompletionSource.SetResult(new BrowserResult(BrowserStatus.Success, url));
63+
}
64+
}
65+
}
66+
67+
#endif
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
namespace Thirdweb.Browser
5+
{
6+
public interface IThirdwebBrowser
7+
{
8+
Task<BrowserResult> Login(string loginUrl, string redirectUrl, CancellationToken cancellationToken = default);
9+
}
10+
11+
public enum BrowserStatus
12+
{
13+
Success,
14+
UserCanceled,
15+
Timeout,
16+
UnknownError,
17+
}
18+
19+
public class BrowserResult
20+
{
21+
public BrowserStatus status { get; }
22+
23+
public string callbackUrl { get; }
24+
25+
public string error { get; }
26+
27+
public BrowserResult(BrowserStatus status, string callbackUrl)
28+
{
29+
this.status = status;
30+
this.callbackUrl = callbackUrl;
31+
}
32+
33+
public BrowserResult(BrowserStatus status, string callbackUrl, string error)
34+
{
35+
this.status = status;
36+
this.callbackUrl = callbackUrl;
37+
this.error = error;
38+
}
39+
}
40+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)