Skip to content

Commit e3077cc

Browse files
committed
Migrate openURL and performActionForShortcutItem to UIScene
1 parent 38e8b59 commit e3077cc

File tree

3 files changed

+67
-45
lines changed

3 files changed

+67
-45
lines changed

XBMC Remote/AppDelegate.m

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -163,40 +163,6 @@ - (NSDictionary*)getServerHTTPHeaders {
163163

164164
#pragma mark - Helper
165165

166-
- (BOOL)connectToServerFromList:(NSString*)host {
167-
if (!host.length) {
168-
return NO;
169-
}
170-
171-
// Host name needs ".local." at the end
172-
if ([host hasSuffix:@".local"]) {
173-
host = [host stringByAppendingString:@"."];
174-
}
175-
176-
// Try to map server name or IP address to the list of Kodi servers
177-
NSInteger index = [self.arrayServerList indexOfObjectPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
178-
return [host isEqualToString:obj[@"serverDescription"]] || [host isEqualToString:obj[@"serverIP"]];
179-
}];
180-
181-
// We want to connect to the desired server only. If this is not present, disconnect from any active server.
182-
BOOL result = NO;
183-
NSIndexPath *serverIndexPath;
184-
NSDictionary *params;
185-
if (index != NSNotFound) {
186-
serverIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
187-
params = @{@"index": @(index)};
188-
result = YES;
189-
}
190-
191-
// Case 1: App just starts. Set the last active server before readKodiServerParameters is called
192-
[Utilities saveLastServerIndex:serverIndexPath];
193-
194-
// Case 2: App already runs. Send a notification to select the server via its index.
195-
[NSNotificationCenter.defaultCenter postNotificationName:@"SelectKodiServer" object:nil userInfo:params];
196-
197-
return result;
198-
}
199-
200166
- (void)sendWOL:(NSString*)MAC withPort:(NSInteger)WOLport {
201167
CFSocketRef WOLsocket;
202168
WOLsocket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, NULL, NULL);
@@ -272,16 +238,6 @@ - (void)sendWOL:(NSString*)MAC withPort:(NSInteger)WOLport {
272238
}
273239
}
274240

275-
- (BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id>*)options {
276-
// Use URL host to map to server list and connect the server.
277-
return [self connectToServerFromList:url.host.stringByRemovingPercentEncoding];
278-
}
279-
280-
- (void)application:(UIApplication*)application performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem completionHandler:(void(^)(BOOL))completionHandler {
281-
// Use shortcut title (= server description) to map to server list and connect the server.
282-
[self connectToServerFromList:shortcutItem.localizedTitle];
283-
}
284-
285241
- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application {
286242
[[SDImageCache sharedImageCache] clearMemory];
287243
}

XBMC Remote/SceneDelegate.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
@import UIKit;
1010

11-
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate> {}
11+
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate> {
12+
UIApplicationShortcutItem *launchShortcutItem;
13+
id launchURLContexts;
14+
}
1215

1316
@property (strong, nonatomic) UIWindow *window;
1417

XBMC Remote/SceneDelegate.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)