Skip to content

Commit 7301881

Browse files
authored
Fixed deeplink notification at startup. (#7)
1 parent 3d867a1 commit 7301881

File tree

5 files changed

+157
-25
lines changed

5 files changed

+157
-25
lines changed

DeeplinkPlugin/deeplink_plugin_implementation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class DeeplinkPlugin : public Object {
1919
static DeeplinkPlugin* instance;
2020

2121
bool initialized;
22-
NSURL* receivedUrl;
2322

2423
static void _bind_methods();
2524

2625
public:
26+
static NSURL* receivedUrl;
2727

2828
Error initialize();
2929

@@ -37,7 +37,7 @@ class DeeplinkPlugin : public Object {
3737

3838
void clear_data();
3939

40-
void set_received_url(NSURL* url);
40+
void navigate_to_open_by_default_settings();
4141

4242
static DeeplinkPlugin* get_singleton();
4343

DeeplinkPlugin/deeplink_plugin_implementation.mm

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
#import <Foundation/Foundation.h>
6+
#import <UIKit/UIKit.h>
67

78
#import "deeplink_plugin_implementation.h"
89
#import "deeplink_service.h"
@@ -12,6 +13,7 @@
1213
String const DEEPLINK_RECEIVED_SIGNAL = "deeplink_received";
1314

1415
DeeplinkPlugin* DeeplinkPlugin::instance = NULL;
16+
NSURL* DeeplinkPlugin::receivedUrl;
1517

1618

1719
void DeeplinkPlugin::_bind_methods() {
@@ -33,15 +35,20 @@
3335
return FAILED;
3436
}
3537

38+
if (receivedUrl) {
39+
emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:receivedUrl]);
40+
}
41+
3642
initialized = true;
43+
3744
return OK;
3845
}
3946

4047
String DeeplinkPlugin::get_url() {
4148
String result = "";
4249

43-
if (this->receivedUrl) {
44-
result = [GDPConverter nsStringToGodotString:this->receivedUrl.absoluteString];
50+
if (receivedUrl) {
51+
result = [GDPConverter nsStringToGodotString:receivedUrl.absoluteString];
4552
}
4653

4754
return result;
@@ -50,8 +57,8 @@
5057
String DeeplinkPlugin::get_scheme() {
5158
String result = "";
5259

53-
if (this->receivedUrl) {
54-
result = [GDPConverter nsStringToGodotString:this->receivedUrl.scheme];
60+
if (receivedUrl) {
61+
result = [GDPConverter nsStringToGodotString:receivedUrl.scheme];
5562
}
5663

5764
return result;
@@ -60,8 +67,8 @@
6067
String DeeplinkPlugin::get_host() {
6168
String result = "";
6269

63-
if (this->receivedUrl) {
64-
result = [GDPConverter nsStringToGodotString:this->receivedUrl.host];
70+
if (receivedUrl) {
71+
result = [GDPConverter nsStringToGodotString:receivedUrl.host];
6572
}
6673

6774
return result;
@@ -70,19 +77,36 @@
7077
String DeeplinkPlugin::get_path() {
7178
String result = "";
7279

73-
if (this->receivedUrl) {
74-
result = [GDPConverter nsStringToGodotString:this->receivedUrl.path];
80+
if (receivedUrl) {
81+
result = [GDPConverter nsStringToGodotString:receivedUrl.path];
7582
}
7683

7784
return result;
7885
}
7986

8087
void DeeplinkPlugin::clear_data() {
81-
this->receivedUrl = NULL;
88+
receivedUrl = NULL;
8289
}
8390

84-
void DeeplinkPlugin::set_received_url(NSURL* url) {
85-
this->receivedUrl = url;
91+
void DeeplinkPlugin::navigate_to_open_by_default_settings() {
92+
// if (@available(iOS 18.3, *)) {
93+
// // Create the URL that deep links to your app's custom settings.
94+
// NSURL *url = [[NSURL alloc] initWithString:UIApplicationOpenDefaultApplicationsSettingsURLString];
95+
// // Ask the system to open that URL.
96+
// [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
97+
// }
98+
// else {
99+
// NSLog(@"DeeplinkPlugin::navigate_to_open_by_default_settings: ERROR: iOS version 18.3 or greater is required!");
100+
// }
101+
if (@available(iOS 8.0, *)) {
102+
// Create the URL that deep links to your app's custom settings.
103+
NSURL *url = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];
104+
// Ask the system to open that URL.
105+
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
106+
}
107+
else {
108+
NSLog(@"DeeplinkPlugin::navigate_to_open_by_default_settings: ERROR: iOS version 8.0 or greater is required!");
109+
}
86110
}
87111

88112
DeeplinkPlugin* DeeplinkPlugin::get_singleton() {
@@ -92,16 +116,15 @@
92116
DeeplinkPlugin::DeeplinkPlugin() {
93117
NSLog(@"DeeplinkPlugin constructor");
94118

95-
[GodotApplicalitionDelegate addService:[DeeplinkService alloc]];
96-
97119
ERR_FAIL_COND(instance != NULL);
98120
instance = this;
99121

100-
initialized = true;
122+
initialized = false;
101123
}
102124

103125
DeeplinkPlugin::~DeeplinkPlugin() {
104126
NSLog(@"DeeplinkPlugin destructor");
127+
105128
if (instance == this) {
106129
instance = NULL;
107130
}

DeeplinkPlugin/deeplink_service.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
#import "app_delegate.h"
1010
#import "godot_app_delegate.h"
1111

12-
@interface DeeplinkService : ApplicationDelegateService
12+
@interface DeeplinkService : ApplicationDelegateService
13+
14+
+ (instancetype) shared;
1315

1416
- (BOOL) application:(UIApplication*) app openURL:(NSURL*) url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*) options;
1517

18+
- (BOOL) application:(UIApplication*) app continueUserActivity:(NSUserActivity*) userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>>* restorableObjects)) restorationHandler;
19+
20+
- (BOOL) application:(UIApplication*) app didFinishLaunchingWithOptions:(NSDictionary<NSString*,id> *) launchOptions;
21+
1622
@end
1723

1824
#endif /* deeplink_plugin_application_delegate_h */

DeeplinkPlugin/deeplink_service.mm

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,127 @@
77
#import "gdp_converter.h"
88

99

10+
struct DeeplinkServiceInitializer {
11+
DeeplinkServiceInitializer() {
12+
[GodotApplicalitionDelegate addService:[DeeplinkService shared]];
13+
}
14+
};
15+
static DeeplinkServiceInitializer initializer;
16+
17+
1018
@implementation DeeplinkService
1119

20+
- (instancetype) init {
21+
self = [super init];
22+
23+
return self;
24+
}
25+
26+
+ (instancetype) shared {
27+
static DeeplinkService* sharedInstance = nil;
28+
static dispatch_once_t onceToken;
29+
dispatch_once(&onceToken, ^{
30+
sharedInstance = [[DeeplinkService alloc] init];
31+
});
32+
return sharedInstance;
33+
}
34+
1235
- (BOOL) application:(UIApplication*) app openURL:(NSURL*) url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*) options {
36+
DeeplinkPlugin::receivedUrl = url;
1337

14-
DeeplinkPlugin::get_singleton()->set_received_url(url);
15-
DeeplinkPlugin::get_singleton()->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
38+
if (url) {
39+
NSLog(@"Deeplink plugin: URL received!");
40+
}
41+
else {
42+
NSLog(@"Deeplink plugin: URL is empty!");
43+
}
44+
45+
DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
46+
if (plugin) {
47+
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
48+
}
1649

1750
return YES;
1851
}
1952

2053
- (BOOL) application:(UIApplication*) app continueUserActivity:(NSUserActivity*) userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>>* restorableObjects)) restorationHandler {
2154
if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb]) {
22-
NSURL* url = userActivity.webpageURL;
23-
DeeplinkPlugin::get_singleton()->set_received_url(url);
24-
DeeplinkPlugin::get_singleton()->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
25-
}
55+
NSURL* url = userActivity.webpageURL;
56+
DeeplinkPlugin::receivedUrl = url;
57+
58+
NSLog(@"Deeplink plugin: Deeplink received at app resumption!");
59+
60+
DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
61+
if (plugin) {
62+
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
63+
}
64+
}
2665

27-
return YES;
66+
return YES;
67+
}
68+
69+
- (BOOL) application:(UIApplication*) app didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id>*) launchOptions {
70+
if (launchOptions) {
71+
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
72+
if (url) {
73+
NSLog(@"Deeplink plugin: Deeplink received at startup!");
74+
DeeplinkPlugin::receivedUrl = url;
75+
76+
DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
77+
if (plugin) {
78+
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
79+
}
80+
}
81+
else {
82+
NSLog(@"Deeplink plugin: UIApplicationLaunchOptionsURLKey is empty!");
83+
84+
NSDictionary* userActivityDict = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];
85+
if (userActivityDict) {
86+
url = [userActivityDict objectForKey:UIApplicationLaunchOptionsURLKey];
87+
if (url) {
88+
NSLog(@"Deeplink plugin: Deeplink received at startup from user activity dictionary!");
89+
DeeplinkPlugin::receivedUrl = url;
90+
91+
DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
92+
if (plugin) {
93+
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
94+
}
95+
}
96+
else {
97+
NSLog(@"Deeplink plugin: UIApplicationLaunchOptionsURLKey is empty in user activity dictionary!");
98+
99+
NSUserActivity* userActivity = [userActivityDict objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
100+
if (userActivity) {
101+
if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb]) {
102+
url = userActivity.webpageURL;
103+
DeeplinkPlugin::receivedUrl = url;
104+
105+
NSLog(@"Deeplink plugin: Deeplink received at app startup from user activity!");
106+
107+
DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
108+
if (plugin) {
109+
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
110+
}
111+
}
112+
else {
113+
NSLog(@"Deeplink plugin: activity type is %@", userActivity.activityType);
114+
}
115+
}
116+
else {
117+
NSLog(@"Deeplink plugin: No user activity in user activity dictionary!");
118+
}
119+
}
120+
}
121+
else {
122+
NSLog(@"Deeplink plugin: No user activity dictionary either!");
123+
}
124+
}
125+
}
126+
else {
127+
NSLog(@"Deeplink plugin: launch options is empty!");
128+
}
129+
130+
return YES;
28131
}
29132

30133
@end

addon

0 commit comments

Comments
 (0)