Failed to load resource: The operation couldn’t be completed. Operation not permitted

When try to read document folder from iphone WKWebView, getting the error saying 'Failed to load resource: The operation couldn’t be completed. Operation not permitted'.But working fine in iphone simulator but not in real device.


Tested in iphone 6,iphone 8 plus.


First, when i try to read igot the cors error(cross origin), i solved this by below code.


[_webView.configuration.preferences setValue:@"true" forKey:@"allowFileAccessFromFileURLs"];


Then, after sloving that, now am getting the 'Failed to load resource: The operation couldn’t be completed. Operation not permitted'. Not able to understand why operation not permitted. And not able to find any reference docs or references on this error.



Any suggestions is highly appreciated..!!

Replies

To start, I want to be clear that using KVO to set undocumented properties on system objects is so not supported. Don’t go down that path.

The usual way to solve this problem is to start the web view with a file URL using

-loadFileURL:allowingReadAccessToURL:
. The second parameter lets you specify a file system directory that the web view is allowed to access.

Have you tried this? If not, please do. If you tried it and it doesn’t work for you, please let us know what went wrong.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've similar error on the console when I'm using the app on the actual device (iPad running iOS 11.4) but not on simulator. In my case, I'm trying to load local images with the src as file:///var/mobile/containers/.../Documents/..../abc.jpg. I'm using wkwebview in my Cordova hybrid app.

In the simulator, it all works fine. But not on the actual device. What is the suggested workaround for loading resources which are not under www directory in wkwebview apps?

In the simulator, it all works fine. But not on the actual device.

I wasn’t 100% clear about this in my earlier post, so lets clarify it now…

A difference between the simulator and the device is very much expected here.

WKWebView
is actually backed by a variety of helper processes and, on the device, those are tightly sandboxed. The simulator does not faithfully replicate file system restrictions applied by the iOS sandbox, and thus there are many cases where you can access things on the simulator but not on the device.

What is the suggested workaround for loading resources which are not under www directory in wkwebview apps?

The correct solution here is to put the item you need to access within the directory pointed to by the second parameter of the

-loadFileURL:allowingReadAccessToURL:
method. I’m not sure how you might do this given the Cordova architecture. If you need help with that I recommend that you raise the issue via Cordova’s support channel.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
  • I am facing a similar kind of error when I tried to load a file from the document directory.

    -loadFileURL:allowingReadAccessToURL:

    **[self.webview loadFileURL:@"Exact file path from app document direcotry" allowingReadAccessToURL:@"App document directory path"]; ** I am using this API from WKWebview by mentioning the app document directory path to allow read access to the entire directory.

    But sometimes, a few devices throwing the below error,

    { "_NSURLErrorFailingURLSessionTaskErrorKey" = "LocalDataTask .<14397>"; "_WKRecoveryAttempterErrorKey" = "<WKReloadFrameErrorRecoveryAttempter: 0x282a69240>"; "_kCFStreamErrorCodeKey" = 1; "_kCFStreamErrorDomainKey" = 1; }

    And again we have a few iOS 12+ devices and weirdly it is happening on only 1 or 2 devices.

    It would be much appreciated if anyone shares your feedbacks on how to resolve this issue.

Add a Comment