zl程序教程

您现在的位置是:首页 >  其他

当前栏目

如何清除 iOS APP 的启动屏幕缓存

2023-03-14 22:22:12 时间

本文转载自微信公众号「网罗开发」,作者韦弦Zhy 。转载本文请联系网罗开发公众号。

简介

每当我在我的 iOS 应用程序中修改了 LaunchScreen.storyboad 中的某些内容时,我都会遇到一个问题:

系统会缓存启动图像,即使删除了该应用程序,它实际上也很难清除原来的缓存。

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

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

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

  1. import UIKit 
  2.  
  3. public extension  UIApplication { 
  4.  
  5.     func clearLaunchScreenCache() { 
  6.         do { 
  7.             try FileManager.default.removeItem(atPath: NSHomeDirectory()+"/Library/SplashBoard"
  8.         } catch { 
  9.             print("Failed to delete launch screen cache: \(error)"
  10.         } 
  11.     } 
  12.  

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

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

使用

  1. UIApplication.shared.clearLaunchScreenCache() 
  • 文章提到的缓存目录在沙盒下如下图所示:

  • OC 代码,创建一个 UIApplication 的 Category
  1. #import <UIKit/UIKit.h> 
  2.  
  3. @interface UIApplication (LaunchScreen) 
  4. - (void)clearLaunchScreenCache; 
  5. @end 
  6. #import "UIApplication+LaunchScreen.h" 
  7.  
  8. @implementation UIApplication (LaunchScreen) 
  9. - (void)clearLaunchScreenCache { 
  10.     NSError *error; 
  11.     [NSFileManager.defaultManager removeItemAtPath:[NSString stringWithFormat:@"%@/Library/SplashBoard",NSHomeDirectory()] error:&error]; 
  12.     if (error) { 
  13.         NSLog(@"Failed to delete launch screen cache: %@",error); 
  14.     } 
  15. @end 

OC使用方法

  1. #import "UIApplication+LaunchScreen.h" 
  2.  
  3. [UIApplication.sharedApplication clearLaunchScreenCache];