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

每当我在我的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)")
        }
    }

}

在启动屏开发过程中,您可以将其放在应用程序初始化代码中,然后在不修改启动屏时将其禁用。

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

使用:
UIApplication.shared.clearLaunchScreenCache()

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

附:

  • 文章提到的缓存目录在沙盒下如下图所示:


    app启动图缓存.png
  • OC代码,创建一个UIApplication 的 Category
#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];

同时有一篇文章有更详细的方案:见 https://blog.csdn.net/olsQ93038o99S/article/details/108924170

赏我一个赞吧~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容