didFailProvisionalNavigation receiving weird error

I have an action in my app prompted by a push notification. When the user taps the notification sometimes the content (in a WKWebView) fails to load. I haven't been able to reproduce this consistently so it's really hard to tell what's going on except to say that when it does happen I've been able to determine that the `didFailProvisionNavigation` method on my `WKNavigationDelegate` is called with the following error:


Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={_WKRecoveryAttempterErrorKey=, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <1432F4C3-CE99-4357-ABE1-0BA100F9947C>.<1>, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=1}

Does anyone have any idea what this error means or what could cause it? The best I can guess is that maybe the resource I am trying to load is not available but that should not be the case and this error doesn't make sense to me.

Accepted Reply

drewster’s post reminded me I should post an update about the original issue. As far as I can tell the intermittent problem reported by paul.yorke is caused by a race condition associated with extending the sandbox of the networking helper process (r. 45456658). The best workaround we could find was to simply retry the load. That seems to work in the majority of cases.

Share and Enjoy

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

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

Replies

Are you trying to load a network URL? Or a file URL?

Share and Enjoy

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

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

Thanks for replying, this is a local file. it's also worth noting that so far the only users to have reported this to us appear to be on iOS 12+.


The URL of the file I am trying to load will look something like:


file:///var/mobile/Containers/Data/Application/DBE20B99-A6B7-49B3-B421-63A31F8CDCAA/Library/Application%20Support/Resources/main-100000006175210-1540300761.0.html

to load it I am calling:


loadFileURL(fileURL, allowingReadAccessTo: location)

// where fileURL:
file:///var/mobile/Containers/Data/Application/DBE20B99-A6B7-49B3-B421-63A31F8CDCAA/Library/Application%20Support/Resources/main-100000006175210-1540300761.0.html
// where location:
file:///var/mobile/Containers/Data/Application/DBE20B99-A6B7-49B3-B421-63A31F8CDCAA/Library/Application%20Support/Resources/

I can't seem to get a reply to you that isn't put into a moderation queue but to answer your question it's a local file. My other reply has a bit more information if you have a way of seeing it.

this is a local file.

Yeah, I thought that might be the case.

I’ve seen reports like this but I’ve never been able to reproduce it myself. Earlier you wrote:

I haven't been able to reproduce this consistently …

Does that mean that you have reproduced it on your local device? If so, that’s an interesting data point.

Does anyone have any idea what this error means or what could cause it?

Error 1 in the POSIX domain is

EPERM
, which most commonly indicates that the access was blocked by the sandbox. This is relevant here because
WKWebView
does all of its work in separate process, so
loadFileURL(fileURL:allowingReadAccessTo:)
has to extend the web view process’s sandbox to give it access to the directory you pass to the
allowingReadAccessTo
parameter. It’s likely that something gone wrong with that sandbox extension, preventing the web view process from accessing the file’s in your app’s container.

However this is just a guess. There’s a lot of moving parts here. Once I hear back from you about reproducing the problem, I’ll have more concrete suggestions on how to proceed.

ps You wrote:

I can't seem to get a reply to you that isn't put into a moderation queue

DevForums is kinda snarky about posting URLs. However, there’s no need to fear the moderation queue; the moderators are doing a pretty good job these days (also, if it’s a thread that I’ve responded on I’ll be able to approve your post).

Share and Enjoy

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

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

it's good to know you can see the moderation queue. I was worried about my reply getting lost in there. This is the first time i've seen a comment be moderated.


>Does that mean that you have reproduced it on your local device? If so, that’s an interesting data point.


The problem is sporadic and not something I can reproduce on demand. I do have a team member who has said that this happens to him at least once per day. I've added additional logging to the app on his device and will be monitoring that, unfortunately since I added the additionally logging it hasn't happened.


If we are able to reproduce it on his device is there anything you'd like me to get from that device? Anything I can provide that can assit you with this particular issue?


The information you are providing with regards to the error and why it would happen is (your guess) is exactly what I was suspecting, I just did't have enough information to go on to solidify that assumption.


I'll let you know once we're able to reproduce it again.

Also I don't know if this helps something else I've been able to determine about when this actually occurs. Right now I am mostly tracking down the issue from my users. I send a push notification to my users and upon tapping on the notification some of my users end up in a situation where the content never loads and I log the error from this protocol method when that happens which is how I know about it.


But what i've noticed is of all the users who have reported this to me, every one of them have been launching the app from the notification and the app is doing a cold start, meaning the app was previously terminated for whatever reason. Every single one of them are coming in this way and the loading of this file URL is happening as soon as possible. That said, these are also (so far) only on iOS 12+ devices and the NotificationCenterDelegate doesn't get setup until `application:willFinishLaunching:withOptions:` so the receipt of that notification should be happening late enough that the setup process has completed. But that's just an interesting note to make here, it seems clear to me that this problem is at least in part being case by a race condition with the app launching.

The problem is sporadic and not something I can reproduce on demand.

Drat.

If we are able to reproduce it on his device is there anything you'd like me to get from that device?

I think the next step here is for you to file a bug report that includes a sysdiagnose log. The next time you manage to reproduce this, trigger a sysdiagnose log and then file a bug with that log. You can learn more about sysdiagnose logs on our Bug Reporting > Profiles and Logs page.

Also I don't know if this helps …

Yeah, that is interesting. Launching via a push notifications presents a number of complications:

  • Your app might have been terminated in the background

  • One of the associated web view helper processes might have been terminated

  • There maybe a data protection issue

I think we can rule out the last point because, reading through your post, it seems that the user has explicitly tapped the notification, and that implies that the device is unlocked. The other two are still in play. It’s possible that the system isn’t re-establishing the sandbox extension during the terminate/relaunch cycle.

You could try exercising that cycle yourself by adding a test mode to your app that does something like this:

  1. Wait for the app to go into the background.

  2. Start a

    UIApplication
    background task to keep the app from being suspended.
  3. Start a one minute timer.

  4. When the timer fires, terminate the app by calling

    exit
    .

Once the app is terminated you can relaunch it via the push notification to see if that reproduces the problem more reliably.

Obviously this for debugging only; don’t inflict this on your real users! (-:

ps I’ve pulled your thread out of Xcode > Swift and into Core OS > File and Storage because this is definitely not a Swift issue (-:

Share and Enjoy

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

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

> ps I’ve pulled your thread out of Xcode > Swift and into Core OS > File and Storage because this is definitely not a Swift issue (-:


Thanks!! I wasn't sure where to put it and I saw another question relating to WKWebView in that same topic so decided to start there.


One question, would it be easier for me to email you directly (using the address listed below) so we can more easily facilitate this discussion. My email would be coming from @nytimes.com. I've also filed a radar for this issue without any sysdiagnose logs, just identifiying the initial problem and sent it over to my relationship representative but she is unable to get me any assistance on it for the time being so I really appreciate the help you've been giving. The radar I filed is: 45456658


Ok so I have been trying to reproduce using the steps below and unfortunately I've had no luck. This is starting to feel to me that it is likely a race condition that isn't exclusive relating to the fact the sandbox needs to be provisioned but also perhaps something else we're doing (in the app) from a cold start. Unfortunately on my device I am unable to reproduce it ever and so I have yet to actually make this happen. I still have plenty of users complaining about this issue though and we're coming up on an important day where I will be delivering a lot of content via push notifications that I hope can load in our app.


Meanwhile my co worker who has been able to reproduce regularly but not consistently, suddenly when I need it to be reproduced hasn't been able to at all :/.


Sigh


Ok so to explain a little bit about app start up, each time the app launches, in addition to setting up all the objects we need to operate we also do things like sending the itunes receipt to the server for re-validation or to check if anything changed, and verifying the user is registered for push notifications. But also we're doing a content update. The size of the content update depends on the amount of time since the last update since it is a delta based update.


For a lot of the users I've seen with this issue I can see in their logs that they've gone 12+ hours (sometimes days) since the last app launch when this problem manifests which leads me to believe that perhaps something having to do with the content update (Which ccouldn't happen in the background because the app is in a terminated state) which is writing the files to disk that the WKWebView will later be reading from mixed together with the one off bit of content we're downloading incidentally in response to the push notification may be creating a race condition that could cause such an issue... Not really sure, but I am trying to explore all avenues.

I wasn't sure where to put it and I saw another question relating to

WKWebView
in that same topic so decided to start there.

Yeah, right now there’s no obvious place for

WKWebView
questions, so I find them scattered all over the map )-:

The radar I filed is: 45456658

Thanks for that.

One question, would it be easier for me to email you directly (using the address listed below) so we can more easily facilitate this discussion.

I’m not able to provide tech support over email but, yes, I think it would be valuable if we discussed this one-to-one. My recommendation is that you open a DTS tech support incident and we can pick things up there.

Please reference this thread so your request comes to me.

Share and Enjoy

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

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

Thanks, I filed the TSI and received a follow-up number for it. Not sure if I should share it publicly though. (is it safe to share here or can you find it without it?)

I filed the TSI

Cool. It’s already landed in my queue and I’ll respond shortly.

Not sure if I should share [the follow-up number] publicly though.

It’s probably best to keep it private.

For other folks reading this thread, if you file a TSI and want to bring it to my attention, just email me the follow-up number (my email address is in my signature).

Share and Enjoy

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

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

Hi All,


I was having this issue 100% of the time when trying to load a PDF with WKWebView from my app's document folder, but it worked fine with loading from the bundle.


Previously I would load all URLs like this:

let request = URLRequest(url: url)
webView.load(request)


This worked for files that were in the app bundle but not ones we stored in documents.

This resolved it:

if url.isFileURL {
     webView.loadFileURL(url, allowingReadAccessTo: url)
} else {
     let request = URLRequest(url: url)
     webView.load(request)
}


Hope it helps!

Andrew

I was having this issue 100% of the time when trying to load a PDF with

WKWebView
from my app's document folder …

This is expected behaviour (well, not expected by you, but you get the idea :-). For reliability and security reasons [1], each web view is backed by a cluster of helper processes responsible for networking and rendering. Each of these helper processes runs in a tight sandbox. When you load a file system URL, the system extends the sandbox of the networking helper process to allow it to access the file. Without this sandbox extension the networking helper process is unable to access your Documents directory, and thus can’t load your PDF.

The fact that this works when the PDF is in your app’s bundle is more an accident of the implementation than a specific design goal. Application resources aren’t sensitive, and thus the networking helper process can access that URL without a sandbox extension.

Still, it’s best not to rely on that, but instead to call

webView.loadFileURL(url:allowingReadAccessTo:)
for all local file system loads, which is exactly what the code snippet you posted does.

Share and Enjoy

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

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

[1] If you’re curious about the background to this, I highly recommend that you watch WWDC 2018 Session 207 Strategies for Securing Web Content.

drewster’s post reminded me I should post an update about the original issue. As far as I can tell the intermittent problem reported by paul.yorke is caused by a race condition associated with extending the sandbox of the networking helper process (r. 45456658). The best workaround we could find was to simply retry the load. That seems to work in the majority of cases.

Share and Enjoy

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

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