Skip to content

Commit

Permalink
Fixed deeplink notification at startup. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
cengiz-pz authored Dec 21, 2024
1 parent 3d867a1 commit 7301881
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 25 deletions.
4 changes: 2 additions & 2 deletions DeeplinkPlugin/deeplink_plugin_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class DeeplinkPlugin : public Object {
static DeeplinkPlugin* instance;

bool initialized;
NSURL* receivedUrl;

static void _bind_methods();

public:
static NSURL* receivedUrl;

Error initialize();

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

void clear_data();

void set_received_url(NSURL* url);
void navigate_to_open_by_default_settings();

static DeeplinkPlugin* get_singleton();

Expand Down
51 changes: 37 additions & 14 deletions DeeplinkPlugin/deeplink_plugin_implementation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "deeplink_plugin_implementation.h"
#import "deeplink_service.h"
Expand All @@ -12,6 +13,7 @@
String const DEEPLINK_RECEIVED_SIGNAL = "deeplink_received";

DeeplinkPlugin* DeeplinkPlugin::instance = NULL;
NSURL* DeeplinkPlugin::receivedUrl;


void DeeplinkPlugin::_bind_methods() {
Expand All @@ -33,15 +35,20 @@
return FAILED;
}

if (receivedUrl) {
emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:receivedUrl]);
}

initialized = true;

return OK;
}

String DeeplinkPlugin::get_url() {
String result = "";

if (this->receivedUrl) {
result = [GDPConverter nsStringToGodotString:this->receivedUrl.absoluteString];
if (receivedUrl) {
result = [GDPConverter nsStringToGodotString:receivedUrl.absoluteString];
}

return result;
Expand All @@ -50,8 +57,8 @@
String DeeplinkPlugin::get_scheme() {
String result = "";

if (this->receivedUrl) {
result = [GDPConverter nsStringToGodotString:this->receivedUrl.scheme];
if (receivedUrl) {
result = [GDPConverter nsStringToGodotString:receivedUrl.scheme];
}

return result;
Expand All @@ -60,8 +67,8 @@
String DeeplinkPlugin::get_host() {
String result = "";

if (this->receivedUrl) {
result = [GDPConverter nsStringToGodotString:this->receivedUrl.host];
if (receivedUrl) {
result = [GDPConverter nsStringToGodotString:receivedUrl.host];
}

return result;
Expand All @@ -70,19 +77,36 @@
String DeeplinkPlugin::get_path() {
String result = "";

if (this->receivedUrl) {
result = [GDPConverter nsStringToGodotString:this->receivedUrl.path];
if (receivedUrl) {
result = [GDPConverter nsStringToGodotString:receivedUrl.path];
}

return result;
}

void DeeplinkPlugin::clear_data() {
this->receivedUrl = NULL;
receivedUrl = NULL;
}

void DeeplinkPlugin::set_received_url(NSURL* url) {
this->receivedUrl = url;
void DeeplinkPlugin::navigate_to_open_by_default_settings() {
// if (@available(iOS 18.3, *)) {
// // Create the URL that deep links to your app's custom settings.
// NSURL *url = [[NSURL alloc] initWithString:UIApplicationOpenDefaultApplicationsSettingsURLString];
// // Ask the system to open that URL.
// [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
// }
// else {
// NSLog(@"DeeplinkPlugin::navigate_to_open_by_default_settings: ERROR: iOS version 18.3 or greater is required!");
// }
if (@available(iOS 8.0, *)) {
// Create the URL that deep links to your app's custom settings.
NSURL *url = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];
// Ask the system to open that URL.
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
else {
NSLog(@"DeeplinkPlugin::navigate_to_open_by_default_settings: ERROR: iOS version 8.0 or greater is required!");
}
}

DeeplinkPlugin* DeeplinkPlugin::get_singleton() {
Expand All @@ -92,16 +116,15 @@
DeeplinkPlugin::DeeplinkPlugin() {
NSLog(@"DeeplinkPlugin constructor");

[GodotApplicalitionDelegate addService:[DeeplinkService alloc]];

ERR_FAIL_COND(instance != NULL);
instance = this;

initialized = true;
initialized = false;
}

DeeplinkPlugin::~DeeplinkPlugin() {
NSLog(@"DeeplinkPlugin destructor");

if (instance == this) {
instance = NULL;
}
Expand Down
8 changes: 7 additions & 1 deletion DeeplinkPlugin/deeplink_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
#import "app_delegate.h"
#import "godot_app_delegate.h"

@interface DeeplinkService : ApplicationDelegateService
@interface DeeplinkService : ApplicationDelegateService

+ (instancetype) shared;

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

- (BOOL) application:(UIApplication*) app continueUserActivity:(NSUserActivity*) userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>>* restorableObjects)) restorationHandler;

- (BOOL) application:(UIApplication*) app didFinishLaunchingWithOptions:(NSDictionary<NSString*,id> *) launchOptions;

@end

#endif /* deeplink_plugin_application_delegate_h */
117 changes: 110 additions & 7 deletions DeeplinkPlugin/deeplink_service.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,127 @@
#import "gdp_converter.h"


struct DeeplinkServiceInitializer {
DeeplinkServiceInitializer() {
[GodotApplicalitionDelegate addService:[DeeplinkService shared]];
}
};
static DeeplinkServiceInitializer initializer;


@implementation DeeplinkService

- (instancetype) init {
self = [super init];

return self;
}

+ (instancetype) shared {
static DeeplinkService* sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[DeeplinkService alloc] init];
});
return sharedInstance;
}

- (BOOL) application:(UIApplication*) app openURL:(NSURL*) url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*) options {
DeeplinkPlugin::receivedUrl = url;

DeeplinkPlugin::get_singleton()->set_received_url(url);
DeeplinkPlugin::get_singleton()->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
if (url) {
NSLog(@"Deeplink plugin: URL received!");
}
else {
NSLog(@"Deeplink plugin: URL is empty!");
}

DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
if (plugin) {
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
}

return YES;
}

- (BOOL) application:(UIApplication*) app continueUserActivity:(NSUserActivity*) userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>>* restorableObjects)) restorationHandler {
if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb]) {
NSURL* url = userActivity.webpageURL;
DeeplinkPlugin::get_singleton()->set_received_url(url);
DeeplinkPlugin::get_singleton()->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
}
NSURL* url = userActivity.webpageURL;
DeeplinkPlugin::receivedUrl = url;

NSLog(@"Deeplink plugin: Deeplink received at app resumption!");

DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
if (plugin) {
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
}
}

return YES;
return YES;
}

- (BOOL) application:(UIApplication*) app didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id>*) launchOptions {
if (launchOptions) {
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
if (url) {
NSLog(@"Deeplink plugin: Deeplink received at startup!");
DeeplinkPlugin::receivedUrl = url;

DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
if (plugin) {
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
}
}
else {
NSLog(@"Deeplink plugin: UIApplicationLaunchOptionsURLKey is empty!");

NSDictionary* userActivityDict = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];
if (userActivityDict) {
url = [userActivityDict objectForKey:UIApplicationLaunchOptionsURLKey];
if (url) {
NSLog(@"Deeplink plugin: Deeplink received at startup from user activity dictionary!");
DeeplinkPlugin::receivedUrl = url;

DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
if (plugin) {
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
}
}
else {
NSLog(@"Deeplink plugin: UIApplicationLaunchOptionsURLKey is empty in user activity dictionary!");

NSUserActivity* userActivity = [userActivityDict objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
if (userActivity) {
if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb]) {
url = userActivity.webpageURL;
DeeplinkPlugin::receivedUrl = url;

NSLog(@"Deeplink plugin: Deeplink received at app startup from user activity!");

DeeplinkPlugin* plugin = DeeplinkPlugin::get_singleton();
if (plugin) {
plugin->emit_signal(DEEPLINK_RECEIVED_SIGNAL, [GDPConverter nsUrlToGodotDictionary:url]);
}
}
else {
NSLog(@"Deeplink plugin: activity type is %@", userActivity.activityType);
}
}
else {
NSLog(@"Deeplink plugin: No user activity in user activity dictionary!");
}
}
}
else {
NSLog(@"Deeplink plugin: No user activity dictionary either!");
}
}
}
else {
NSLog(@"Deeplink plugin: launch options is empty!");
}

return YES;
}

@end
2 changes: 1 addition & 1 deletion addon
Submodule addon updated 2 files
+15 −1 Deeplink.gd
+2 −3 DeeplinkPlugin.gd

0 comments on commit 7301881

Please sign in to comment.