換個 Launch Screen 有多難?
有些特別的節日會要特別幫產品換個新 icon & launch screen,換 icon 沒啥問題,這個 launch screen 卻怎麼換也換不掉,在 Launch Screen.storyboard 上的 UIImage 總是會吃到舊的圖片,試過各式各樣的花招都沒辦法解決這個問題,換個 Launch Screen 是要有多難?(摔滑鼠
試過的解法
在網路上查一查這個 launch screen image cache 的症狀常見的解法如下:
- 刪除 app → 重新啟動手機 → 重裝 app 這招可行,但是 Production 上的 app 不能期望 user 會做刪除 app 與重開機的操作,而這個問題是 production app 也會產生的,所以不能算是一個 proper solution。
- 將新的 image 重新命名,並且放在 xcassets 以外的資料夾中,再指定給 Launch Screen.storyboard UIImage 以前都是這樣解的,但是現在貌似無效了G_G
- 卍解 Clean + Build、砍掉 Screen Launch.storyboard 再重建一個、blablabla… 有這麼簡單我的人生就不會這麼辛苦了
目前有效的解法
上述的做法都行不通已經讓我開始懷疑人生了,寫 code 真的適合我嗎?
但開始針對 clear launch screen cache 的方向找解法出現了一線生機,我找到了 insidegui 這位老兄的文章,他提供了一個清除 launch screen cache 的方法:
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)") } } }
insidegui 兄
你就像是冬日的暖陽、夏日的涼風
在 AppDelegate 的 didFinishLaunchingWithOptions 或 willFinishLaunchingWithOptions 中清除 launch screen cache 可以確保在 User 第二次開啟 app 時會拿到正確的 launch screen 圖片,可惜的是第一次開啟還是會拿到舊的(無論是 didFinish 或 willFinish 都一樣),另外可優化的地方是,在 user 拿到正確的圖片之後就不應該清 cache。