What are the ways to maintain sessions in a PhoneGap Android app?
-
I am trying to convert a website to a phonegap android application. The web server sets the session id on the client as a cookie. This session id has to be maintained for the complete session. When I move from one page to another ( the second page is a local html file in the app), using href, or window.location, or navigator.app.loadUrl, it loads the second page in a browser, and the cookies get destroyed, so I can't make an valid ajax request from the second page. I wouldn't prefer putting all the pages in a single html file ( like jquery mobile) . Given that I have seperate html files, how to navigate between them, such that the second page is loaded in the same webview,( so that the cookies are preserved) rather than in a browser. Do I have to create another activity for it ? Is there any other way of navigation in phonegap, except those mentioned above ? Or is the only way of staying in the same webview is to dynamically load pages using ajax and replace the existing page with them ? Another possibility is I can parse the session id from the response, store it in the local storage and append with every request. But this cookie is HTTP Read only, so I can't read it using javascript
-
Answer:
Have you configured the app to manage cookies for you? If you have, ajax requests should work. You will have a .java file in your phonegap project (named after your project). In the onCreate method, add this statement CookieManager.setAcceptFileSchemeCookies(true); //available in android level 12 and above If you are supporting Android level lower than 12, try this super.loadUrl(...); CookieManager.getInstance().setAcceptCookie(true); That is all that needs to be done. In case this doesn't work for you (I don't have all the details so possible it won't work if you are making cross-domain calls), you can look into using localStorage (http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html#localStorage) You can clear the storage once done (logout?).
Lata Sadhwani at Quora Visit the source
Other answers
There is not session concept in phonegap. but you can use localStorage instead of Session Example with Login : Get User data & verify with web service if Success, store localStroage.appname_login = "true" else store localStroage.appname_login = "false" For next time , you can check like this if(localStroage.appname_login == "true") //do something else //do something Simple example with login : http://phonegappro.com/tutorials/phonegap-login-system-using-php-mysql/
Sundara Vel
Related Q & A:
- In what ways can we distinguish between a human and bot behavior?Best solution by researchgate.net
- What are some ways I can make a little money?Best solution by Yahoo! Answers
- What are easy ways of writing a good essay?Best solution by Quora
- What are the best ways to drive traffic to a website?Best solution by Quora
- What are some ways be become a swiss resident?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.