iOS:use safe method to prevent unexpected exception

This commit is contained in:
pengfeizhou
2021-02-01 15:31:24 +08:00
committed by osborn
parent 6bf38bd290
commit 5a12a770fd
11 changed files with 125 additions and 65 deletions

View File

@@ -27,7 +27,9 @@
@implementation DoricModalPlugin
- (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
__block DoricGravity gravity = DoricGravityBottom;
[dic[@"gravity"] also:^(NSNumber *it) {
gravity = (DoricGravity) [it integerValue];
@@ -37,11 +39,13 @@ - (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
} else {
ShowToast(dic[@"msg"], gravity);
}
});
}];
}
- (void)alert:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
message:dic[@"msg"]
preferredStyle:UIAlertControllerStyleAlert];
@@ -52,11 +56,13 @@ - (void)alert:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
}];
[alert addAction:action];
[self.doricContext.vc presentViewController:alert animated:YES completion:nil];
});
}];
}
- (void)confirm:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
message:dic[@"msg"]
preferredStyle:UIAlertControllerStyleAlert];
@@ -67,20 +73,22 @@ - (void)confirm:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
[promise reject:nil];
}];
[alert addAction:cancelAction];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:dic[@"okLabel"] ?: NSLocalizedString(@"Ok", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[promise resolve:nil];
}];
[alert addAction:okAction];
[self.doricContext.vc presentViewController:alert animated:YES completion:nil];
});
}];
}
- (void)prompt:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
message:dic[@"msg"]
preferredStyle:UIAlertControllerStyleAlert];
@@ -113,6 +121,6 @@ - (void)prompt:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
[alert addAction:okAction];
[self.doricContext.vc presentViewController:alert animated:YES completion:nil];
});
}];
}
@end