iOS专题资源__系统知识点iOS猛码计划晚期代码癌患者

iOS 应用内更换AppIcon

2017-04-05  本文已影响668人  Fengur

1、简介

苹果爸爸在iOS 10.3的API来开放了更换icon的方法,客观来说,实用性并不硬霸。

图片.png
核心API是下面这个方法:
Objc
- (void)setAlternateIconName:(NSString *)alternateIconName 
           completionHandler:(void (^)(NSError *error))completionHandler

Swift 3.0

func setAlternateIconName(_ alternateIconName: String?, 
        completionHandler: ((Error?) -> Void)? = nil)

方法一目了然,设置替换图标名称 & 回调
官方API :传送门
按照官方文档的解释,我们需要在info.plist里来一点操作,具体的key代表了什么,建议大家没事就去 官方文档 cmd+F+keyword搜出来啃一啃。

图片.png

2、使用

开干之前,先来一下英文考察:
Alternate交替的、轮流的
Primary主要的;最早的,原始的

图片.png 图片.png 图片.png
<plist version="1.0">
<dict>
  <key>CFBundleIcons</key>
  <dict>
      <key>CFBundleAlternateIcons</key>
      <dict>
          <key>UIPrerenderedIcon</key>
          <false/>
          <key>icontwo</key>
          <dict>
              <key>CFBundleIconFiles</key>
              <array>
                  <string>icontwo</string>
              </array>
          </dict>
      </dict>
      <key>CFBundlePrimaryIcon</key>
      <dict>
          <key>CFBundleIconFiles</key>
          <array>
              <string>Icon60x60</string>
          </array>
      </dict>
  </dict>

图片.png

Objc

- (void)changeIcon{
    if ([UIApplication sharedApplication].supportsAlternateIcons) {
        NSLog(@"this app can change app icon");
    }else{
        NSLog(@"sorry,this app can not change app icon");
        return;
    }
    NSString *iconName = [[UIApplication sharedApplication] alternateIconName];
    if (iconName) {
        [[UIApplication sharedApplication] setAlternateIconName:nil completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"set icon error: %@",error);
            }
            NSLog(@"current icon's name -> %@",iconName);
        }];
    }else{
        [[UIApplication sharedApplication] setAlternateIconName:@"icontwo" completionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"set icon error: %@",error);
            }
            NSLog(@"current icon's name -> %@",iconName);
        }];
    }
}

Swift3.0

    func changeIcon() {
        if UIApplication.shared.supportsAlternateIcons {
            print("this app can change app icon")
        }
        else {
            print("sorry,this app can not change app icon")
            return
        }
        let iconName: String = UIApplication.shared.alternateIconName!
        if iconName != "" {
            UIApplication.shared.setAlternateIconName(nil, completionHandler: {(_ error: Error?) -> Void in
                if error != nil {
                    print("set icon error: \(String(describing: error))")
                }
                print("current icon's name -> \(iconName)")
            })
        }
        else {
            UIApplication.shared.setAlternateIconName("icontwo", completionHandler: {(_ error: Error?) -> Void in
                if error != nil {
                    print("set icon error: \(String(describing: error))")
                }
                print("current icon's name -> \(iconName)")
            })
        }
    }
new .gif

终于找到了一个正常的GIF制作工具,Demo传送门,感谢翻阅。

上一篇下一篇

猜你喜欢

热点阅读