Share extension “couldn’t be opened because you don’t have permission to view it.” error handling

Peggy Tsai 
May 6 · 1 min read

(share extension, don’t have permission, error handling)

使用share extension傳送照片到APP時

當APP已經提供相簿的讀取權限
但卻依然出現沒有權限讀取的錯誤訊息(如下圖)

Thread 2: Fatal error: ‘try!’ expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=257 “The file “IMG_0512.PNG” couldn’t be opened because you don’t have permission to view it.” UserInfo={NSFilePath=/var/mobile/Media/PhotoData/OutgoingTemp/xxxx/IMG_0512.PNG, NSUnderlyingError=0x281390f90 {Error Domain=NSPOSIXErrorDomain Code=1 “Operation not permitted”}}

可以參考本文進行解決


原因在於 info中
Executable file 參數 與build settings中Packaging的Product Name參數 不一致

導致沒有權限讀取的情況

因此
將info.plist的Executable.file 參數修改為 $(PRODUCT_NAME)

重新執行專案
即可解決此狀況

參考文獻

彼得潘的 Swift iOS App 開發教室

學習 Swift iOS App 開發的學生作品集

Peggy Tsai 

Written by

iOS & Golang 隨手記 www.linkedin.com/in/peggytsaifc

彼得潘的 Swift iOS App 開發教室

學習 Swift iOS App 開發的學生作品集

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade