博客> iOS动态更换icon存在的一个小问题
iOS动态更换icon存在的一个小问题
2020-01-18 14:09 评论:0 阅读:745 superjunjun
UINavgationController 自定义UITabBar

动态更换icon 的时候会遇到一个问题 也就是 会弹出系统alertview

大多数的时候 不想要这个弹出框 网上有很多的拦截alertview方法 通过runtime 动态替函数来实现

首先 替换icon方法

  • (void)updateIcon { NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];

    if (phoneVersion.floatValue >= 10.3) { if ([UIApplication.sharedApplication supportsAlternateIcons]) {

        NSTimeInterval currentDateTimeinterval = [BOTimeStampAssistor getCurrentTime];
        NSTimeInterval new_icon_b = [BOTimeStampAssistor getTimeStampWithDateString:@"2018-3-3"];
        NSTimeInterval new_spring_b = [BOTimeStampAssistor getTimeStampWithDateString:@"2018-2-5"];
        NSTimeInterval new_spring_e = [BOTimeStampAssistor getTimeStampWithDateString:@"2018-3-3"];
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (currentDateTimeinterval >= new_spring_b && currentDateTimeinterval <= new_spring_e) {
                [UIApplication.sharedApplication setAlternateIconName:@"new_spring" completionHandler:^(NSError * _Nullable error) {
                    NSLog(@"%@",error);
                }];
            } else if (currentDateTimeinterval >= new_icon_b) {
                [UIApplication.sharedApplication setAlternateIconName:@"new_icon" completionHandler:^(NSError * _Nullable error) {
                    NSLog(@"%@",error);
                }];
            } else {
                [UIApplication.sharedApplication setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
                    NSLog(@"%@",error);
                }];
            }
        });
    }

    } }

动态拦截 网上很多方法都是 通过判断alertviewcontroller的title和message 为空来判断是当前替换icon的alertviewcontroller 这个有一定的局限性 有的时候自己弹出的alert 也是没有title和message 这样就会出现bug

所以:

import "UIViewController+Present.h"

import

@implementation UIViewController (Present)

  • (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Method presentM = class_getInstanceMethod(self.class, @selector(presentViewController:animated:completion:)); Method presentSwizzlingM = class_getInstanceMethod(self.class, @selector(dy_presentViewController:animated:completion:)); method_exchangeImplementations(presentM, presentSwizzlingM); }); }

BOOL isFind = NO;

  • (void)dy_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {

    if ([viewControllerToPresent isKindOfClass:[UIAlertController class]]) {

    UIAlertController *alert = (UIAlertController *)viewControllerToPresent;
    
    isFind = NO;
    BOOL isChangeIconAlert = [self findIconVCByAlertVC:alert];
    if (isChangeIconAlert) {
        NSLog(@"这是更换 icon 系统的 alert \n");
        return;
    } else {
        [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion];
        return;
    }

    }

    [self dy_presentViewController:viewControllerToPresent animated:flag completion:completion]; }

  • (BOOL)findIconVCByAlertVC:(UIAlertController )alertVC { for (UIViewController subVC in alertVC.childViewControllers) { NSString *className = NSStringFromClass([subVC class]); if ([className containsString:@"AlternateApplicationIcons"] && [className containsString:@"AlertContent"] ) { isFind = YES; break; } // if ([subVC isKindOfClass:NSClassFromString(@"_UIAlternateApplicationIconsAlertContentViewController")]) { // isFind = YES; // break; // } } return isFind; }

@end

收藏
1
sina weixin mail 回到顶部