22

Service Workers were available in WKWebView in iOS 11.3 betas, but do not appear to be available in the final GM version of iOS 11.3.

Does anyone know how to re-enable Service Workers in WKWebView on iOS?

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75

2 Answers2

25

Service Workers in WKWebView now require an entitlement:

com.apple.developer.WebKit.ServiceWorkers, which should be added to the .entitlements plist as a Boolean with a value of YES.

Currently this will only work in the iOS Simulator, until such time as Apple update the Apple Developer Portal to make it possible to create App IDs and Provisioning Profiles containing this entitlement.

For more information see this bug: https://bugs.webkit.org/show_bug.cgi?id=182865 and associated change set: https://trac.webkit.org/changeset/228933/webkit

enter image description here

EDIT: Unfortunately still seems to be the case in iOS 16.

aleclarson
  • 18,087
  • 14
  • 64
  • 91
Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • Strange how they would release this half-done. App ID's still have no option for this. – DarkNeuron May 24 '18 at 08:49
  • 2
    Agreed. I shall be paying the WebKit lab a visit at WWDC. – Andrew Ebling May 24 '18 at 14:08
  • 1
    Lemme know how it goes :-) – DarkNeuron May 25 '18 at 07:03
  • XCode 9.4 , iOS 11.4 - not working for device/simulator. What versions are you guys testing? – CheshireKat May 31 '18 at 14:33
  • Take away from the WWDC lab on ServiceWorker in WKWebView: "We have no news at this time" :( – Andrew Ebling Jun 05 '18 at 22:03
  • 3
    Confirmation following WWDC: the required entitlement is not available for App IDs in the developer portal. – Andrew Ebling Jun 05 '18 at 22:06
  • @AndrewEbling did you happen to find out if they have plans to enable this in iOS 12? – Ivan Chub Jun 13 '18 at 23:15
  • @ivan chub Please see both my comments above from 5th June. – Andrew Ebling Jun 14 '18 at 06:25
  • Would it be possible to add the entitlement or activate Service Workers in another way in WKWebView maybe by using private APIs for an internal (Enterprise) app? – keeluu Jul 12 '18 at 13:16
  • @keeluu unfortunately not. Entitlements are enforced by a kernel extension, so unfortunately they cannot be worked around with private API. Please see these slides for more info: http://newosxbook.com/files/HITSB.pdf – Andrew Ebling Jul 12 '18 at 16:43
  • 7
    @AndrewEbling Apple - frankly this is ridiculous, so iOS Safari supports it but because we can't flag our WKWebView apps as Service Worker entitled we can't use them in Cordova based apps where we are wanting to use SW offline capability rather than implementing it through some hacky localhost server - where can I add support for this feature request to be added? – steve Sep 06 '19 at 08:23
  • 1
    Totally agree @steve. But that's Apple for you. Hacky localhost server was the best workaround we came up with too. – Andrew Ebling Sep 06 '19 at 10:43
  • 4
    Apple is positively negligent as it regards HTML5 in iOS applications. They remove and/or break things we need all the time (`loadFileURL(... :allowingReadAccessTo)` just broke in iOS 13). – Jeff McMahan Oct 10 '19 at 16:59
  • So, please confirm—you can't publish an app to the App Store with a service worker? But you can run it on the Simulator? – Dmitry Minkovsky Sep 13 '20 at 14:44
  • @DmitryMinkovsky TMK you still cannot use ServiceWorker in apps going into the App Store. It _may_ still work in the Simulator, but I've not tried - I no longer work on the project in question. – Andrew Ebling Sep 14 '20 at 08:17
  • Thank you @AndrewEbling – Dmitry Minkovsky Sep 14 '20 at 14:50
14

This entitlement is now available with iOS 14 (com.apple.developer.web-browser). Due to the fact that Apple now allows other browsers being the default option there is a new entitlement available which also includes service workers!!!

The updated documentation can be found here: Apple Docs

The following is the interesting part:

Using Default Browser Capabilities

- Apps that use the com.apple.developer.web-browser managed entitlement can:

- Be an option for the user to choose as their default browser.

- Load pages from all domains with full script access.

- Use Service Workers in WKWebView instances.

But this comes with big drawbacks. Universal links won't work anymore and there is a list of not allowed key for the info.plist (see link).

Can anyone already confirm having a WKWebview using this entitlement in the app store?


UPDATE:

A better solution might be using App Bound Domains.

This works for up to 10 domains and you restricts your WKWebview to these domains! It won't work for other websites. You do not have to own the domain.

Add the following to you WKWebviewConfiguration:

let configuration = WKWebViewConfiguration()
configuration.limitsNavigationsToAppBoundDomains = true

In your info plist add key with WKAppBoundDomains:

<key>WKAppBoundDomains</key>
<array>
  <string>example.com</string>
  <string>example2.com</string>
</array>

After a quick test this returned 'serviceWorker' in navigator === true. If this goes through the review process this might be a breakthrough for PWAs!!!!

Silicium
  • 435
  • 8
  • 18
  • 1
    Did you run this on a real device or on simulator? Did you set the entitlement in your .plist file or were you able to add it to your app-id / provisioning profile? – CularBytes Dec 23 '20 at 16:25