Is there a way to persist cookies or HTML5 localStorage across WebBrowser instances on Windows Phone? - html

Short version: I have a WebBrowser control hosted in a Windows Phone 8 app. How can I store values from javascript so that they persist across the user closing and reopening my app?
Long version:
I'm developing a Windows Phone 8 application that has a single WebBrowser control hosted in a single MainPage.xaml page that lives for the entire life of my app. I created the app with the "Windows Phone HTML5 App" project type when creating the project in Visual Studio 2012. 99% of my application is hosted in web pages (on the internet, not stored on the phone) that I direct the WebBrowser to go to when the app starts up. In my application's web pages I'm trying to persist data across pages and across sessions. For example, once the user logs in once then I want to store that on the phone so the next time they start the app they don't have to log in again.
Cookies and HTML5 Local Storage (via window.localStorage.setItem and getItem) both work fine for sharing data across pages in the app while the app is running and even if you switch out of the app (via the Windows phone "hard button") and go back in. But if the user exits the app by pressing the hard "back" button then the next time the app is started all localStorage and cookies seem to be gone.
Is this the expected behavior? I guess I'm not sure where WebBrowser would store the data (Isolated Storage? Or maybe in the same place it's stored if going to the web site with Internet Explorer?). In any case, if there's no "fix" for this, can anyone the best way for me to provide my own storage mechanism so that I can let my javascript code persist values across instances of my app running? I'm happy to use the app's Isolated Storage if only I knew of a way to get and retrieve values from it using javascript. Thank you.

I'm not sure if this is expected behaviour or not.
To get at the Isolated Storage you will need to use JS/.NET interop.
if you want to trigger the persistent storage from JS:
Use window.external.notify in JS, generating a JSON string (for instance) to pass along to the .NET side. That could be written to IsolatedStorage without the .NET having to parse the data. You could use IsolatedStorage.AppSettings or a full file depending on the size of the data.
Alternately you could trigger the process from .NET:
Call WebBrowser.InvokeScript to call a JS function which returns the same JSON string representing your data.
The .NET side could detect and restore this data on startup and use WebBrowser.InvokeScript to pass the JSON string back into the WebBrowser via a JS function.
You'd of course have to deal with error cases (attempting to restore bad/corrupt JSON).
Also, if you trigger this from .NET in response to the App.Closing event you need to watch out that you don't take too long writing data.
The faster you run the better, but this definitely needs to be done within 10 seconds or the OS will kill your app.
See MSDN docs for WebBrowser.InvokeScript() and ScriptNotify registration to window.external.notify.

Related

Possible ways to deploy an application to Windows Phone Store using automated tool/process

Is it possible to submit a new app or update an existing app to Windows Phone store with any automate process instead of uploading it manually?
Please suggest the solutions for the same.
I found few automated tools for ios application submission like fastlane
Not possible at the moment and no indication about such feature coming.
There may be such functionality soon.
From https://blogs.windows.com/buildingapps/2016/03/31/announcing-new-dev-center-capabilities-to-increase-app-revenue-and-streamline-management/:
The new Windows Store submission API will be made available in
preview, beginning today and rolling out in waves. This new API offers
a subset of functionality provided by Dev Center as a REST API and
supports these actions for published apps: submitting updates,
modifying metadata, and adding/removing in-app products. You can
request access to the preview through the “Feedback” tab in Dev Center
by selecting “Submission API” in the “Suggestions” tab. Access will be
granted in waves, beginning with a small group of developers. Build
session: https://channel9.msdn.com/Events/Build/2016/B839

How do I structure my code to run a XAML page in a WinRT app and "return" a result?

I'm currently building a universal app for Windows 8.1 and Windows Phone 8.1.
In Windows Runtime, the file picker has a very easy to use API that looks like this:
StorageFile file = await openPicker.PickSingleFileAsync();
It treats user input as an asynchronous operation that can be awaited. In this instance, if a user cancels, null is returned, but you could imagine an exception being thrown if the user cancels.
I was wondering if there was some way to create a page so that I can create a similar API. Specifically, I am doing OAuth 2.0 authentication with a hosted service, and I want to send the user to the authentication page hosted in a WebView and return the code for requesting access tokens if the operation is successful or throw an exception if the user does not authorize my application.
For example:
var authentication = new AuthenticationAccess();
string code = await authentication.RequestAuthorizationAsync();
The call would swap out the Page in the Frame with the Page containing the WebView's, the user would be able to authenticate and then control would return back to the calling function and the page swapped back to the original page.
I'm kind of swimming in Windows 8.1 C#/XAML documentation. From what I understand, there is a global Window object that hosts a Frame object which can have its content swapped out with the different Pages in the application. Having done some Android development, I'm familiar with the Activity model, where essentially different pages can be initiated through intents and can propagate results back up to the Activity which launched it. With this model, it seems like it would be easy to wrap this process in the model I described, but I'm a little confused of how to do the same thing in Windows. Would I need to stand up significant architecture to achieve this pattern?
Is there a simple answer to this, or am I in over my head?
Have you checked out the WebAuthenticationBroker yet? Perhaps that could turn out to be an easy solution to your problem.
Otherwise - you can use TaskCompletionSource to set up the authentication task that you can await, but I'd try to avoid navigating to other pages for your authentication dialog since navigation events could mess up your states and break whomever is waiting for the authentication to complete. A dialog overlay of some sort might be a better idea.

Can I share a calendar entry via NFC without starting my app on the destination phone

Is it possible to create an NFC message that will create a calendar entry on the destination phone, without requiring my app to be installed on the destination phone?
For the moment, I've implemented it by using a custom protocol URI, but I'd like this to work even when the destination phone does not have my app installed. It would be even better if it could work on an Android destination.
You cannot do this on any phone if I am not mistaken. NFC messages at the end of the day are just bytes being transferred from one mobile device to another. If the destination device does not know how (and most of them do NOT have a build in handler for calendar event) to parse that message in order to open a native application what you are trying to do will not work.
Even if you get an NFC reader and read an NFC message design for that purpose and then recreate this in your application it will NOT be universal; meaning it will not work on any device, especially on different OS devices.
Again this is what I know so far. Don't take this for granted.
Have you tried to store a vcalendar item on a tag to see whether/how a phone reacts to this?
text/x-vcalendar or text/calendar
might be the right format to use for the NDEF record. See also this so question.
If this would not work, you might write an app that takes intents from such calendar data and sends it to the calendar app on the phone (however I don't know how that step would work).

App Dev ::Windows 8::how to provide news updates

I am trying to build a Sports App for Windows Store .I am not able to understand how am I suppose to change the data day on day. The App initially will have some static content and the data has to be updated daily. Kindly guide me how to do this , since this is my first App for windows store.
I assume you have the data on a server somewhere. So use the HttpClient control to access the data, use Linq/XML to parse it, and then load it in your UI however you've designed it to do so.
You can show updates in a live tile with this sample: http://code.msdn.microsoft.com/Simple-Live-Tiles-575b1d66
Also look at Windows Azure Mobile Services: http://www.windowsazure.com/en-us/develop/mobile/

Passing fields to a silverlight app from the command line

Silverlight is made so that you can run it in a browser. That is cool. So, if I want to pass variables from the command line so that it can be used to populate fields in the silverlight app, how is that done?
You know, like you can with javascript (I think) or php (I am pretty sure).
Silverlight has Init parameters which is a key/value map that you can fill in your Html page and get the values in Application_Startup event. However this does not apply to Silverlight apps running in OOTB (out of the browser) mode.
This MSDN page shows a trick if you run your app both in and out of browser, so that you can save the InitParams values when running in the browser and save it back to Isolated Storage for when you run the application in OOTB mode.