I added a book shelf to dsh-media-hub, a self-hosted DeepSeek Harness plugin for watching shows and keeping ebooks. In a desktop browser or mobile Safari, saving an EPUB is just <a href="..." download>. When the same page is installed to an iPhone’s home screen (display-mode: standalone), tapping that link opens iOS’s built-in file preview full screen with no back button and no browser bar. The only way back to the app is swiping it away and reopening it.
Getting out of that trap took four releases on November 23 (v0.15.5 through v0.16.3) and ended in WebKit’s Objective-C++ source, because navigator.share({ files }) does not hand the iOS share sheet a file the way Safari’s own downloader does.
What failed first
My first attempt (v0.15.5) was the lazy one: check for standalone mode and add target="_blank" rel="noopener" to the link, expecting iOS to open an in-app Safari sheet with a Done button. It didn’t. iOS still took over the screen with the file preview and no way out.
In v0.15.6 I stopped navigating away from the page at all. In standalone mode, the button fetched the book with fetch(), wrapped the blob in a File, and passed it to navigator.share({ files: [file] }). If the fetch took long enough for iOS to expire the tap’s user activation, navigator.share() rejected with NotAllowedError, so I kept the File in state for a second tap:
try {
await navigator.share({ files: [file] })
setState({ step: 'idle' })
} catch (error) {
if (error?.name === 'AbortError') return setState({ step: 'idle' })
if (error?.name === 'NotAllowedError') return setState({ step: 'ready', file })
setState({ step: 'error', message: error?.message ?? String(error) })
}
On a real phone, fetching a 13 MB EPUB almost always outlives iOS’s gesture window. Tapping Save showed a spinner, finished, and then sat there waiting for a second tap before the share sheet would open. A button that does nothing at the end of a download feels broken even when it’s working as coded.
Worse, once the share sheet did open, Apple Books and other ebook readers weren’t in it. Save to Files, AirDrop, and messaging apps like Telegram showed up; Books didn’t, even with an .epub filename and application/epub+zip on the File.
In the v0.16.0 release notes I guessed the share sheet only listed apps with a Share Extension and moved on. That guess turned out to be wrong, so I went looking in WebKit.
What WKShareSheet.mm actually hands UIKit
In Source/WebKit/UIProcess/Cocoa/WKShareSheet.mm, WebKit writes every shared file to a real temporary file URL on disk (writeFileToShareableURL). Then, right before presenting UIActivityViewController, it checks who started the share (line 351):
if (!data.files.isEmpty()) {
bool usePlaceholderFiles = data.originator == WebCore::ShareDataOriginator::Web;
When usePlaceholderFiles is false (a native share, outside web content), WebKit hands the array of NSURLs straight to UIActivityViewController. When it’s true (anything from navigator.share()), iOS wraps each URL in WKShareSheetFileItemProvider, a UIActivityItemProvider subclass (lines 149 to 174):
- (instancetype)initWithURL:(NSURL *)url
{
RELEASE_ASSERT(isMainRunLoop());
if (!(self = [super initWithPlaceholderItem:[NSData data]]))
return nil;
_url = url;
...
}
- (id)item
{
return _url.get();
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController
dataTypeIdentifierForActivityType:(UIActivityType)activityType
{
return typeIdentifierForFileURL(_url.get());
}
-item returns the real file URL once you pick a destination, and dataTypeIdentifierForActivityType: returns the Uniform Type Identifier from the file extension (org.idpf.epub-container for an EPUB). The catch is line 153: the placeholder item passed to UIActivityItemProvider is an empty NSData, not a file NSURL.
UIActivityViewController looks at the placeholder item’s class to decide which apps and extensions to offer before -item is ever called. Because the placeholder is NSData, iOS treats the item as raw data while building the sheet. Targets that take generic data (Save to Files, AirDrop, messengers) match and appear. Apps that expect a file on disk, like Apple Books, and share extensions whose activation rules ask for files (public.file-url or file-count rules) get filtered out. Tweaking the MIME type or filename in JavaScript does nothing, since the filtering happens on [NSData data] before your file URL is ever read.
The git history on WKShareSheet.mm makes the reason clear. In October 2022 (255315@main, bug 246097), WebKit stopped passing raw file URLs to UIActivityViewController for Web Share as IPC hardening, so QuickLook wouldn’t parse untrusted files from web pages into thumbnail previews outside the WebContent process. That first patch used a placeholder URL, which broke “Save X images” and “Print” because an empty URL gave the share sheet no type information. Six months later (263341@main, bug 255641), WebKit switched the placeholder to [NSData data] plus a UTI from dataTypeIdentifierForActivityType: and LPLinkMetadata with shouldFetchSubresources set to NO. That brought image saving and printing back without turning thumbnail parsing back on, and left file-only targets off the sheet.
What I ended up shipping
Since JavaScript can’t get Apple Books into navigator.share(), dsh-media-hub puts two icons side by side in standalone iOS mode (web/device-copy.js):
return h(React.Fragment, null,
toShareSheet(),
h(IconButton, {
label: t('book.openInSafari'),
icon: Icon.Compass,
href: `x-safari-${new URL(link, document.baseURI).href}`,
}),
failure && h('span', { className: 'vsync-error', role: 'alert' }, failure))
For the share sheet, downloading and sharing are now two separate taps. Tapping Download pulls the book into Cache Storage (with progress and Range resume when a mobile connection drops), and once it’s cached the button becomes Share. That tap awaits nothing and calls navigator.share({ files: [copy.file] }) right away, so NotAllowedError is gone and the cached copy is still there when you reopen the app.
Beside it sits an Open in Safari button using iOS 17’s x-safari-https: URL scheme. Safari downloads the book through its own download view, which has real navigation and hands out a file URL so Apple Books and every installed reader appear.
I made one more mistake before calling it done. In v0.16.0 I hid the Safari button until after “Download to device” had finished, as if both actions used the cached file. That wasted a full download every time someone wanted Books: a home-screen web app and Safari don’t share Cache Storage, and they don’t share cookies either, so Safari always fetches the file itself and asks you to sign in the first time. In v0.16.3 I put the Safari icon out from the start.
An iOS home-screen app ends up with an awkward split. The share sheet keeps you in the app with no second login, and misses Books; Safari reaches Books, and makes you sign in once and download it there. I kept both icons on the card because WebKit’s IPC hardening isn’t going away, and Save to Files is only one extra tap if you’d rather stay out of Safari.