简体   繁体   中英

How do I make "apple-mobile-web-app-capable" not lose its session?

I have a web app.

<meta name="apple-mobile-web-app-capable" content="yes">

I put this in there.

I add to home screen.

However, I realize that I always have to login again after I close the web app.

How do I make it keep the session?

There is an easy answer, but as of yet I was unable to find any reference to it in Apple's official documentation.

The trick is to do this:

// Start or resume session
session_start(); 

// Extend cookie life time by an amount of your liking
$cookieLifetime = 365 * 24 * 60 * 60; // A year in seconds
setcookie(session_name(),session_id(),time()+$cookieLifetime);

If you extend the lifetime of your session cookie like this, Safari will hold on to the session cookie and even allow sharing of the session between the 'home screen installed' version of your web app and normal visits through Safari itself.

For a more elaborate discussion, take a look at my answer to this question:

Maintain PHP Session in web app on iPhone

I use static class and static variable such as dictionary(string,object) to keep data when I have to access outside app. If you use FormAuthentication, you will notice

User.Identity.IsAuthenticated = true 

but session does not exist. you can use "User.Identity.Name" as key to get data in dictionary.

This works in ASP.Net MVC3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM