iOS 更換桌面圖標(biāo)(setAlternateIconName后不彈出alert)

我自己使用的方法:

- (void)lc_setAlternateIconName:(NSString*)iconName
 {
     //anti apple private method call analyse
     if ([[UIApplication sharedApplication] respondsToSelector:@selector(supportsAlternateIcons)] &&
         [[UIApplication sharedApplication] supportsAlternateIcons])
     {
         NSMutableString *selectorString = [[NSMutableString alloc] initWithCapacity:40];
         [selectorString appendString:@"_setAlternate"];
         [selectorString appendString:@"IconName:"];
         [selectorString appendString:@"completionHandler:"];
         
         SEL selector = NSSelectorFromString(selectorString);
         IMP imp = [[UIApplication sharedApplication] methodForSelector:selector];
         void (*func)(id, SEL, id, id) = (void *)imp;
         if (func)
         {
             func([UIApplication sharedApplication], selector, iconName, ^(NSError * _Nullable error) {});
         }
     }
 }

注意事項
1.不要在Appdelegate.m中更換,如果要在Appdelegate.m更換則需要使用異步延時加載

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       xxxxxx  // 更換圖標(biāo)
 });

2.icon資源文件需要在項目目錄下,不能是Assets.xcassets中的圖片,否則無效

3.supportsAlternateIcons只是檢測是否能進行更換,并不能進行系統(tǒng)版本號判斷,需要自己進行版本判斷,要不然在低版本系統(tǒng)中會崩潰,比如:iOS7

可以參考:[http://www.itdecent.cn/p/9c78f161eeb8]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容