由于苹果爸爸的消极支持,也由于 launch screen 本来就是 App 启动前的系统缓存行为,更换 launchScreen.storyboard 里的图片有风险,覆盖安装后可能碰到:
- 启动画面白屏(大概是缓存更新失败)
- 启动时看到旧的启动画面(系统缓存)
记得在 -[Application didFinishLaunch xx]
调用下面的方法(Swift 或 objc 都行)
Swift version
// 修改 LaunchScreen.storyboard 的步骤:
// 1. 图片素材改名
// 2. 不要放到 .xcassets 里面(?)
// 3. 启动时调用该方法清理缓存
// 4. 修改下面的 launchScreenVersion
// 本方法调用后下次才让新启动画面生效
func clearCacheOfLaunchScreenIfNeeded() {
// 修改 launchScreen 时要发布的 app 版本,每次修改 LaunchScreen 都要记得修改此变量
let launchScreenVersion = "1.9.0"
let key = "launchScreenVersion"
// useless
//let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
let cachedLaunchScreenVersion = UserDefaults.standard.object(forKey: key) as? String
let launchScreenUpdated = (cachedLaunchScreenVersion != launchScreenVersion)
if launchScreenUpdated {
UserDefaults.standard.set(launchScreenVersion, forKey: key)
let home = URL(fileURLWithPath: NSHomeDirectory())
let cacheURL = URL(fileURLWithPath: "Library/SplashBoard", relativeTo: home)
//let cachePath = (NSHomeDirectory() as NSString).appendingPathComponent("Library/SplashBoard")
do {
let exists = try cacheURL.checkResourceIsReachable()
try FileManager.default.removeItem(at: cacheURL)
print("\(#function): \(cacheURL) exists(\(exists)) and remove successfully")
} catch {
print("\(#function) error: \(error)")
}
}
}
objc version
// 修改 LaunchScreen.storyboard 的步骤:
// 1. 图片素材改名
// 2. 不要放到 .xcassets 里面(覆盖安装后白屏问题)
// 3. 启动时调用该方法清理缓存
// 4. 修改下面的 launchScreenVersion
// 本方法调用后下次才让新启动画面生效
- (void)clearCacheOfLaunchScreenIfNeeded
{
// 修改 launchScreen 时要发布的 app 版本
NSString *launchScreenVersion = @"1.9.0";
NSString *key = @"LaunchScreenVersion";
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
NSString *cachedLaunchScreenVersion = [ud objectForKey:key];
//NSString *appVersion = NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"];
BOOL noRecordedCache = (cachedLaunchScreenVersion.length == 0);
BOOL launchScreenUpdated = ([cachedLaunchScreenVersion compare:launchScreenVersion options:NSNumericSearch] != NSOrderedSame);
if (noRecordedCache || launchScreenUpdated) {
[ud setObject:launchScreenVersion forKey:key];
NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/SplashBoard"];
NSURL *cacheURL = [NSURL fileURLWithPath:cachePath];
NSError * __autoreleasing error = nil;
if ([cacheURL checkResourceIsReachableAndReturnError:&error]) {
error = nil;
[NSFileManager.defaultManager removeItemAtURL:cacheURL error:&error];
NSLog(@"%s: remove launchScreen cache error: %@", __PRETTY_FUNCTION__, error);
} else {
NSLog(@"%s: %@ existence checking error: %@", __PRETTY_FUNCTION__, cacheURL, error);
}
}
}
参考: