接口部分
@interface UIApplication (Extend)
-(void)_openUrl:(NSURL *)url;
-(void)_openUrl:(NSURL *)url options:(NSDictionary<UIApplicationOpenExternalURLOptionsKey, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion;
-(nullable UIViewController *)getTopViewController:(UIViewController *)viewController;
-(nullable UIViewController *)rootViewController;
-(nullable UIViewController *)visibleViewController;
@end
实现部分
@implementation UIApplication (Extend)
-(void)_openUrl:(NSURL *)url
{
if ([self canOpenURL:url]) {
if (@available(iOS 10, *)) {
[self openURL:url options:@{} completionHandler:^(BOOL success) {}];
}
else
{
[self openURL:url];
}
}
}
-(void)_openUrl:(NSURL *)url options:(NSDictionary<UIApplicationOpenExternalURLOptionsKey, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion
{
if ([self canOpenURL:url]) {
if (@available(iOS 10, *)) {
[self openURL:url options:options completionHandler:completion];
}
else
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL res = [self openURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
completion(res);
});
});
}
}
else
{
completion(NO);
}
}
-(UIViewController *)getTopViewController:(UIViewController *)viewController
{
if (viewController == nil) {
return nil;
}
else if ([viewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navVC = (UINavigationController *)viewController;
return [self getTopViewController:navVC.visibleViewController];
}
else if ([viewController isKindOfClass:[UITabBarController class]])
{
UITabBarController *tabVC = (UITabBarController *)viewController;
return [self getTopViewController:tabVC.selectedViewController];
}
else if (viewController.presentedViewController){
return [self getTopViewController:viewController.presentedViewController];
}
else
{
return viewController;
}
}
-(nullable UIViewController *)rootViewController
{
return self.delegate.window.rootViewController;
}
-(nullable UIViewController *)visibleViewController
{
UIViewController *rootVC = [self rootViewController];
return [self getTopViewController:rootVC];
}
网友评论