iOS 更換桌面圖標(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  // 更換圖標
 });

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

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

可以參考:[http://www.lxweimin.com/p/9c78f161eeb8]

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容