@@ -5,35 +5,52 @@ @interface OpenFilePlugin ()<UIDocumentInteractionControllerDelegate>
5
5
6
6
static NSString *const CHANNEL_NAME = @" open_file" ;
7
7
8
+ // Returns the root view controller of the app,
9
+ // or nil if the app has no root view controller.
8
10
static UIViewController *RootViewController () {
11
+ // If the app is running on iOS 13 or higher, then use the new `connectedScenes`
12
+ // API to get the root view controller of the key window.
9
13
if (@available (iOS 13 , *)) {
14
+ // Get a list of all the connected scenes in the app.
10
15
NSSet *scenes = [[UIApplication sharedApplication ] connectedScenes ];
16
+ // Iterate over the scenes and check if each one is a UIWindowScene.
11
17
for (UIScene *scene in scenes) {
12
18
if ([scene isKindOfClass: [UIWindowScene class ]]) {
19
+ // Get a list of all the windows in the scene.
13
20
NSArray *windows = ((UIWindowScene *)scene).windows ;
21
+ // Iterate over the windows and check if each one is the key window.
14
22
for (UIWindow *window in windows) {
15
23
if (window.isKeyWindow ) {
24
+ // Return the root view controller of the key window.
16
25
return window.rootViewController ;
17
26
}
18
27
}
19
28
}
20
29
}
30
+ // If we couldn't find a key window, then return nil.
21
31
return nil ;
22
32
} else {
33
+ // Simply return the root view controller of the key window.
23
34
return [UIApplication sharedApplication ].keyWindow .rootViewController ;
24
35
}
25
36
}
26
37
38
+ // Returns the top view controller in a view controller hierarchy.
27
39
static UIViewController *
28
40
TopViewControllerForViewController (UIViewController *viewController) {
41
+ // If the view controller has a presented view controller, then return the top
42
+ // view controller in that hierarchy.
29
43
if (viewController.presentedViewController ) {
30
44
return TopViewControllerForViewController (
31
45
viewController.presentedViewController );
32
46
}
47
+ // If the view controller is a navigation controller, then return the top
48
+ // view controller in the navigation controller's stack.
33
49
if ([viewController isKindOfClass: [UINavigationController class ]]) {
34
50
return TopViewControllerForViewController (
35
51
((UINavigationController *)viewController).visibleViewController );
36
52
}
53
+ // Otherwise, return the given view controller.
37
54
return viewController;
38
55
}
39
56
0 commit comments