forked from H5GG/H5GG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopShow.h
executable file
·156 lines (113 loc) · 5.77 KB
/
TopShow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// TopMsg.h
// h5gg
//
// Created by admin on 21/3/2022.
//
#ifndef TopMsg_h
#define TopMsg_h
#include <objc/runtime.h>
#include "makeWindow.h"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wobjc-protocol-method-implementation"
#pragma GCC diagnostic ignored "-Wnullability-completeness"
@interface TopShow : UIViewController <UIDocumentPickerDelegate>
//@property UIWindow* lastKeyWindow;
@property UIInterfaceOrientationMask followOrientationMask;
@property UIWindow* alertWindow;
@property NSString* pickedfile;
@property void(^pickedfile_notify)();
@end
@implementation TopShow
//如果不定义旋转相关委托函数, 并且没有锁定屏幕旋转, 则UIAlertController会跟随陀螺仪旋转, 并且界面全部卡死
//主要是supportedInterfaceOrientations返回的支持方向集合, 如果原window不支持竖屏, 新window旋转为横屏, 则原window会卡死
- (BOOL)shouldAutorotate {
BOOL should = YES; //self.lastKeyWindow.rootViewController.shouldAutorotate;
dumpKeyWindow("TopShow shouldAutorotate");
return should;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
UIInterfaceOrientationMask mask = (UIInterfaceOrientationMask)(1<<UIApplication.sharedApplication.statusBarOrientation); //self.lastKeyWindow.rootViewController.supportedInterfaceOrientations;
uint64_t mask2 = 1<<UIApplication.sharedApplication.statusBarOrientation;
mask = self.followOrientationMask | mask2;
dumpKeyWindow("TopShow supportedOrientations");
return mask;
}
-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
UIInterfaceOrientation preferred = UIApplication.sharedApplication.statusBarOrientation; //self.lastKeyWindow.rootViewController.preferredInterfaceOrientationForPresentation;
dumpKeyWindow("TopShow preferredOrientation");
return preferred;
}
+(void)present:(UIViewController*(^)(TopShow* controller))alert {
void (^submit)() = ^(){
dumpKeyWindow("TopShow present");
TopShow* rootVC = [TopShow new];
rootVC.followOrientationMask = UIApplication.sharedApplication.keyWindow.rootViewController.supportedInterfaceOrientations;
rootVC.alertWindow = makeWindow(NSStringFromClass(UIWindow.class));
rootVC.alertWindow.rootViewController = rootVC;
//显示在系统的普通Alert之上, //UIWindowLevelStatusBar=1000, UIWindowLevelAlert=2000
rootVC.alertWindow.windowLevel = UIWindowLevelAlert + 1;
//makeKeyAndVisible会影响APP本身的窗口层级,容易引发BUG //[rootVC.alertWindow makeKeyAndVisible];
[rootVC.alertWindow setHidden:NO];
//等待下一轮再显示, 不然rootViewController的viewDidAppear还没调用就viewWillDisappear了
dispatch_async(dispatch_get_main_queue(), ^() {
[rootVC presentViewController:alert(rootVC) animated:YES completion:nil];
});
};
if([NSThread isMainThread])
submit();
else
dispatch_async(dispatch_get_main_queue(), submit);
}
-(void)dismiss {
NSLog(@"TopShow dismiss on %d", [NSThread isMainThread]);
[self.alertWindow setHidden:YES];
//self.lastKeyWindow = nil;
self.alertWindow = nil;
}
+(void)alert:(NSString*)title message:(NSString*)message
{
[self present:^(TopShow* controller) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:Localized(@"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[controller dismiss];
}]];
return alert;
}];
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
NSLog(@"documentPickerWasCancelled=%@", controller);
[self dismiss];
}
- (void)_documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
NSLog(@"didPickDocumentAtURL %@", url);
BOOL canAccessingResource = [url startAccessingSecurityScopedResource];
NSLog(@"canAccessingResource=%d", canAccessingResource);
[self dismiss];
self.pickedfile = [url path];
self.pickedfile_notify();
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls {
NSLog(@"didPickDocumentAtURLs %@", urls);
[self _documentPicker:controller didPickDocumentAtURL:urls[0]];
}
+(void)filePicker:(NSArray*)types callback:(void(^)(NSString*))callback
{
[self present:^(TopShow* controller) {
//似乎有个人还是不行, 先加回static试试? 还是不行,就算移动到app目录也不行, 奇奇怪怪???
TopShow* filePickerCallback = controller;// [[TopShow alloc] init];
//因为delegate是弱引用, 所以整个block保持他自己
filePickerCallback.pickedfile_notify = ^(){
callback(filePickerCallback.pickedfile);
};
//https://www.jianshu.com/p/d6fe1e7af9b6
//UIDocumentMenuViewController需要iCloud权限, UIDocumentPickerViewController这个似乎不用
UIDocumentPickerViewController* documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport]; //UIDocumentPickerModeOpen部分APP不回调
NSLog(@"modalPresentationStyle=%d", documentPicker.modalPresentationStyle);
//documentPicker.modalPresentationStyle = UIModalPresentationOverFullScreen; //默认似乎是UIModalPresentationPageSheet, 绝对不能用UIModalPresentationFullScreen会卡死一天一夜!!!!
documentPicker.delegate = filePickerCallback; //弱引用
return documentPicker;
}];
}
@end
#endif /* TopMsg_h */