Swift 专栏Swift学习iOS 开发每天分享优质文章

在iOS上清除APP的启动屏幕缓存

2019-12-20  本文已影响0人  韦弦Zhy

每当我在我的iOS应用程序中修改了启动屏幕LaunchScreen.storyboad中的某些内容时,我都会遇到一个问题:系统会缓存启动图像,即使删除了该应用程序,它实际上也很难清除原来的缓存。

有时我修改了LaunchScreen.storyboad,删除应用程序并重新启动,它显示了新的LaunchScreen.storyboad,但LaunchScreen.storyboad中引用的任何图像都不会显示,从而使启动屏幕显得不正常。

今天,我在应用程序的容器中进行了一些挖掘,发现该Library文件夹中有一个名为的文件夹SplashBoard,该文件夹是启动屏幕缓存的存储位置。

因此,要完全清除应用程序的启动屏幕缓存,您所需要做的就是在应用程序内部运行以下代码(我已将该代码打包到UIApplication的扩展中):

import UIKit

public extension UIApplication {

    func clearLaunchScreenCache() {
        do {
            try FileManager.default.removeItem(atPath: NSHomeDirectory()+"/Library/SplashBoard")
        } catch {
            print("Failed to delete launch screen cache: \(error)")
        }
    }

}

注意:仅在iOS 13上测试

您可以将其放在启动屏幕开发过程中启用的参数标志后面的应用程序初始化代码中,然后在不使用启动屏幕工作时将其禁用。

这个技巧在启动屏幕出问题时为我节省了很多时间,希望也能为您节省一些时间。

以上内容来自Quick tip: clearing your app’s launch screen cache on iOS 简单翻译一下搬运过来,希望有用

使用:
UIApplication.shared.clearLaunchScreenCache()

附:

#import <UIKit/UIKit.h>

@interface UIApplication (LaunchScreen)
- (void)clearLaunchScreenCache;
@end
#import "UIApplication+LaunchScreen.h"

@implementation UIApplication (LaunchScreen)
- (void)clearLaunchScreenCache {
    NSError *error;
    [NSFileManager.defaultManager removeItemAtPath:[NSString stringWithFormat:@"%@/Library/SplashBoard",NSHomeDirectory()] error:&error];
    if (error) {
        NSLog(@"Failed to delete launch screen cache: %@",error);
    }
}
@end

OC使用方法

#import "UIApplication+LaunchScreen.h"

[UIApplication.sharedApplication clearLaunchScreenCache];
上一篇下一篇

猜你喜欢

热点阅读