@@ -24,6 +24,10 @@ - (void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session opti
2424
2525 // Set interface style for window
2626 [self setInterfaceStyleFromUserDefaults ];
27+
28+ // Store launch options
29+ launchShortcutItem = connectionOptions.shortcutItem ;
30+ launchURLContexts = connectionOptions.URLContexts ;
2731}
2832
2933- (void )sceneWillEnterForeground : (UIScene*)scene {
@@ -37,6 +41,17 @@ - (void)sceneDidBecomeActive:(UIScene*)scene {
3741 LocalNetworkAlertClass *localNetworkAlert = [LocalNetworkAlertClass new ];
3842 [localNetworkAlert triggerLocalNetworkPrivacyAlert ];
3943 });
44+
45+ // As per Apple documentation
46+ // https://developer.apple.com/documentation/uikit/menus_and_shortcuts/add_home_screen_quick_actions
47+ if (launchShortcutItem) {
48+ [self windowScene: (UIWindowScene*)scene performActionForShortcutItem: launchShortcutItem completionHandler: nil ];
49+ launchShortcutItem = nil ;
50+ }
51+ if (launchURLContexts) {
52+ [self scene: scene openURLContexts: launchURLContexts];
53+ launchURLContexts = nil ;
54+ }
4055}
4156
4257- (void )sceneDidEnterBackground : (UIScene*)scene {
@@ -54,6 +69,54 @@ - (void)sceneDidEnterBackground:(UIScene*)scene {
5469 UIApplication.sharedApplication .shortcutItems = items;
5570}
5671
72+ - (void )windowScene : (UIWindowScene*)windowScene performActionForShortcutItem : (UIApplicationShortcutItem*)shortcutItem completionHandler : (void (^)(BOOL ))completionHandler {
73+ // Use shortcut title (= server description) to map to server list and connect the server.
74+ [self connectToServerFromList: shortcutItem.localizedTitle];
75+ }
76+
77+ - (void )scene : (UIScene*)scene openURLContexts : (NSSet <UIOpenURLContext*>*)URLContexts {
78+ // Use URL host to map to server list and connect the server.
79+ UIOpenURLContext *urlContext = URLContexts.allObjects .firstObject ;
80+ NSURL *url = urlContext.URL ;
81+ [self connectToServerFromList: url.host.stringByRemovingPercentEncoding];
82+ }
83+
84+ #pragma mark - Helper
85+
86+ - (BOOL )connectToServerFromList : (NSString *)host {
87+ if (!host.length ) {
88+ return NO ;
89+ }
90+
91+ // Host name needs ".local." at the end
92+ if ([host hasSuffix: @" .local" ]) {
93+ host = [host stringByAppendingString: @" ." ];
94+ }
95+
96+ // Try to map server name or IP address to the list of Kodi servers
97+ NSInteger index = [AppDelegate.instance.arrayServerList indexOfObjectPassingTest: ^BOOL (NSDictionary *obj, NSUInteger idx, BOOL *stop) {
98+ return [host isEqualToString: obj[@" serverDescription" ]] || [host isEqualToString: obj[@" serverIP" ]];
99+ }];
100+
101+ // We want to connect to the desired server only. If this is not present, disconnect from any active server.
102+ BOOL result = NO ;
103+ NSIndexPath *serverIndexPath;
104+ NSDictionary *params;
105+ if (index != NSNotFound ) {
106+ serverIndexPath = [NSIndexPath indexPathForRow: index inSection: 0 ];
107+ params = @{@" index" : @(index)};
108+ result = YES ;
109+ }
110+
111+ // Case 1: App just starts. Set the last active server before readKodiServerParameters is called
112+ [Utilities saveLastServerIndex: serverIndexPath];
113+
114+ // Case 2: App already runs. Send a notification to select the server via its index.
115+ [NSNotificationCenter .defaultCenter postNotificationName: @" SelectKodiServer" object: nil userInfo: params];
116+
117+ return result;
118+ }
119+
57120- (void )setInterfaceStyleFromUserDefaults {
58121 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults ];
59122 NSString *mode = [userDefaults stringForKey: @" theme_mode" ];
0 commit comments