Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

localStorage doesn't work for HTML5 games on iPad by default

A topic by jaredkrinke created Feb 04, 2023 Views: 714 Replies: 8
Viewing posts 1 to 5
(+1)

In Safari on iPad, there is now a default setting called "Prevernt cross-site tracking". Unfortunately, this breaks localStorage for HTML5 games on itch.io because the games are hosted in a frame on a different domain (and this is considered cross-site tracking).

Is this known? Are there any plans to mitigate this? E.g. allow opting into either: a frame running on the same domain, or launch the game in a new tab.

Moderator moved this topic to Questions & Support
Moderator

That's how itch.io is designed to work. There are ways to set the headers right, but I don't know how.

(2 edits)

Let me try a different question: is there anything I can do as a developer to make localStorage work for players on iPad on itch.io?

I've got people complaining that they lost hours of progress :(

Moderator(+1)

Yes, as I said, far as I know there's a way. It's just not the default, for security reasons. This is often discussed on the forum, so there should be someone here who knows the answer.

For anyone who's curious, I attempted the following work-around:

  1. Detect if the game is running on iPad
  2. If so, prompt the user to click a link to load the frame's URL in a new tab (ensuring localStorage writes are no longer cross-domain)

#2 works, but, to my surprise, I couldn't find a reliable way to determine if the user is on an iPad. Recent Safari updates make iPads (and iPhones) claim to be desktop Macs, so you have to resort to fragile fingerprinting techniques. (Unsurprisingly, there also is no way to detect if "Prevent Cross-Site Tracking" is enabled.)

If anyone has other ideas, let me know!

Admin (1 edit)

I couldn’t find a reliable way to determine if the user is on an iPad

Instead of detecting the device you should do feature detection. In this case you would try to access the localStorage object within a try catch statement. If that fails then you instruct them with alternative solution or a message about how their saves will not persist.

(3 edits) (+1)

Unfortunately that doesn't work (that was actually the first approach I tried). The localStorage calls all succeed on iPad--they just don't persist across page reloads :(

On a previous version of Safari, they would all fail and you could detect it, but that's not the case anymore...

Admin (6 edits) (+1)

We have to run games in an iframe on a different domain for security reasons:

  • We can not run games on the same domain as itchio since we are letting you execute untrusted JavaSript.
  • It must run in an iframe embedded on an itchio domain for us to apply site-locking. Site locking is to mitigate third-party game aggregators that steal our games to put ads on top of them. They will reuse our CDN to host them and end up costing us bandwidth money. (Note that you may be able to load the embed by direct URL, but sitelocking is applied asynchronously and may activate at a later time in the future. There is no guarantee the CDN URL will continue to work directly, you should instead use the embed code to link directly to a game: https://itch.io/updates/introducing-game-embeds)

Unfortunately there are limited things we can do here, other than instructing users to change their browser’s security settings. Having the user visit the domain ahead of time could be valid approach, but I don’t recommend having them visit the game’s page directly. Alternatively you could use some kind of third party API to host save data but then you would have to deal with the privacy implications.

Thanks for the detailed response! I guess if I ever get in the position of having a lot of players on iPad, it will be time to just do a proper port 🙂

This topic has been auto-archived and can no longer be posted in because there haven't been any posts in a while.