File Couldn't Be Opened Because You Don't Have Permission to View It Error

The file MyApp.app couldn't be opened because you don't have permission to view it when running app in Xcode 6 Beta 4

There was a problem with the Info.plist of the project. I created a new project with the same name in Xcode 6 beta 4 and then replaced the real project's Info.plist with the new one. The project then built and ran fine.

Look at the diff, it appears like the plist might have somehow gotten mixed up with a playground's plist. The bundle identifier was "com.apple.dt.playground.iOS-18300-13" and the executable and bundle names were "iOS" along with some other oddities.

This is the full diff in case anyone needs it for reference:

        <key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
- <string>iOS</string>
+ <string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
- <string>com.apple.dt.playground.iOS-18300-13</string>
+ <string>com.myCompany.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
- <string>iOS</string>
+ <string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
- <string>AAPL</string>
+ <string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
- <key>CFBundleSupportedPlatforms</key>
- <array>
- <string>iPhoneSimulator</string>
- </array>
+ <key>CFBundleSignature</key>
+ <string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
- <key>DTPlatformName</key>
- <string>iphonesimulator</string>
- <key>DTSDKName</key>
- <string>iphonesimulator8.0</string>
- <key>LSBackgroundOnly</key>
- <true/>
<key>LSRequiresIPhoneOS</key>
<true/>
+ <key>UIMainStoryboardFile</key>
+ <string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
+ <key>UISupportedInterfaceOrientations</key>
+ <array>
+ <string>UIInterfaceOrientationPortrait</string>
+ <string>UIInterfaceOrientationLandscapeLeft</string>
+ <string>UIInterfaceOrientationLandscapeRight</string>
+ </array>
</dict>
</plist>

The file couldn’t be opened because you don’t have permission to view it (REAL DEVICE)

In case someone is interested, I've been able to solve it by calling url.startAccessingSecurityScopedResource() in the document picker before actually accessing the file. Hope this helps!

File couldn’t be opened because you don’t have permission to view it error

Anyone coming across this thread, @LeoDabus pointed me to where to turn off sandbox, which worked:

Sample Image

He also cleaned up my code a bit:

let fileURL = URL( fileURLWithPath: "/Users/me/file.txt" )    
var rawDataString: String
var errorString: String?

do {
rawDataString = try String( contentsOf: fileURL, encoding: .utf8 )
} catch let error as NSError {
errorString = error.description
rawDataString = ""
return
}

Getting The file 'xxx couldn't be opened because you don't have permission to view it when importing

You have to call the method startAccessingSecurityScopedResource() on the URL first, before reading the content of the file. Note: don't forget to call stopAccessingSecurityScopedResource() after you're finished!

You can find more information in the Apple documentation.

Edit:

Here is the code that worked for me:

.fileImporter(
isPresented: $isImporting,
allowedContentTypes: [.plainText],
allowsMultipleSelection: false
) { result in
do {
guard let selectedFile: URL = try result.get().first else { return }
if selectedFile.startAccessingSecurityScopedResource() {
guard let fileContent = String(data: try Data(contentsOf: selectedFile), encoding: .utf8) else { return }
defer { selectedFile.stopAccessingSecurityScopedResource() }
} else {
// Handle denied access
}
} catch {
// Handle failure.
print("Unable to read file contents")
print(error.localizedDescription)
}
}


Related Topics



Leave a reply



Submit