Showing posts with label Windows Phone. Show all posts
Showing posts with label Windows Phone. Show all posts

Tuesday, August 12, 2014

Javascript window.external.notify to communicate to WebBrowser control

How to communicate from web page loaded inside browser control to the hosting application

There are so many scenarios, where we may need to show web page inside our native applications such as Windows forms, WPF, Windows Phone, Android, iPhone etc... One of the scenario is to integrate with external authentication providers.

If we look at any third party authentication providers such as Google, Facebook, Azure ACS they don't let us take the user authentication details via our forms and get it authenticated using their web services. Instead the are strict in accepting the user name and password only through their web pages. The reason is security. If each and every application starts accepting user's Facebook or Google user name and password, there are chances that some of those apps will store the credentials locally and it might be miss used. That is the issue which is addressed by only allowing web page login even if we are in native applications.

The solution is, if we are developing even WPF or native mobile application and want to get external authentication we need to show their web page in our application. And once the authentication happens inside the browser, the native application can receive the security token and use it for subsequent operations.

For showing identity provider login page, we need to rely on the browser control available in the corresponding platform. If its WPF the browser control name is WebBrowser, in Android its named as WebView and in iOS its UIWebView. It is simple. First part is over, now our native application need to know when the authentication is completed, so that it can remove the browser control and obtain the token. This is the main topic of this post. In other words, the authentication happened inside the browser and browser need to to notify that event to the host application via some techniques.

The same technique, we can reuse the same technique if we want to notify any event happens inside the web page loaded inside the browser control.

Communication from HTML5 web page to WPF

WPF Browser control class has a property called ObjectForScripting. Assign any object to this property and it will be available as the window.external object in Javascript. So whatever methods are available in the class of the object assigned, those can be invoked from Javascript. For example, we created a class called HTMLInteropClass and that class has a method named MyMethod(). If we assign an object of that class to the WebBrowser.ObjectForScirpting property, we can invoke the MyMethod() written in C# from javascript using window.external.MyMethod() statement. Obviously the javascript should be downloaded from server or injected during run time to be running inside the browser control. Steps are as follows.

  1. Develop the interop class. Have the method inside it, which we are intended to call from javascript.
  2. Create object of that class and assign to the ObjectForScripting property of the WPF WebBrowser control.
  3. Point web browser control to the web page which is having invocation code. ie window.external.<methodname>()
  4. Develop the web page which is having the invocation code
More details here.

Communication from HTML5 web page to Android

In Android the web browser control name changes. But the handling is kind of same as of WPF browser control. Steps given below
  1. Develop a class in Java for Android application with method which will be called from javascript.
  2. In the onCreate() method of activity capture the webView reference and relate the object of above class with javascript using addJavascrtiptInterface() method.
  3. Make sure the Javascript is changed to call the method, present in the class created in step 1.
  4. Point the WebView to the page which contains the code to invoke our Android method.
More details here.

Communication from HTML5 web page to Windows phone

Windows phone uses a different way than WPF. It uses event based method invocation. ie We need to listen to the ScriptNotify event of WebBrowser. Steps below
  1. Subscribe to the ScriptNotify event.
  2. In the event handler e.Value gives the value sent from javascript
  3. The javascript code can invoke this method using window.external.notify("value");
This can be used in Silverlight as is. More details here.

Communication from HTML5 web page to iPhone

Here again the way is different. We cannot handle the window.external object directly. Need to follow a workaround. Whenever we want to communicate to the iOS UIWebView, we need to navigate to a dummy url and catch the navigation event in objective C code. We can use the webViewDidFinishLoad handler of UIWebView to capture the event. Steps below.

  1. Write code in JS to redirect the window.external invocations to a dummy url with data in its query string.
  2. Handle the webViewDidFinishLoad of UIWebView control and write the code in it.

More details here.

Tuesday, January 31, 2012

How can I use keyboard in Windows Phone 7 simulator ?

When I entered into the WP 7 development using the WP7 emulator, the first annoying behaviour noticed was the unavailability of machine keyboard support in the emulator. I had to use the visual keyboard popped up by the simulator. Our Windows Phone 7 application has integration with Azure and ACS which requires authentication every time I run the app. In other words that was the only part which really needs keyboard input. I don't know why I didn't google to get the keyboard support .May be I was in bad impression that the simulator always simulates in the real way.

Days passed and the schedule became tight .Accidently I press on a key and I could see that it is accepting the physical keyboard entries. This made the development easier and didn't get much time to bother about the key which made that magic. Later the week I had to restart my machine and could see that the emulator is not at all accepting the keyboard. This made me to google for that magic key and it was "Page Up". 

Yes really there are some keyboard short cuts. Have a look at the below page for details

Monday, November 28, 2011

Installing SSL certificates to Windows Phone 7 emulator

The days are again getting tight due to new technologies like Windows Azure, Windows Phone 7 & iPhone development. It is always interesting to work in new technologies where we learn so many new things ,hacks etc…My recent project uses Windows Azure as server side technology and mobile as client side. For the time being we are using iPhone as client.

When we start with Azure we could see that we can simulate the https scenario even in the emulator which is more like the production environment. Really like it. But after some time, it became trouble. There were not enough MAC machines where we can test the iPhone application. So I moved to WP 7 as client for testing purpose. I integrated the Azure tool kit for WP 7 and when tried to access the Azure ACS, it started throwing errors .Our REST based services hosted using HTTPS in emulator are not working. ie simply says network error.

After a google we could see that it is due to lack of certificates. If it was some other .Net client I can live with ServicePointManager and its validation callback. But unfortunately there is no such ServicePointManager in Silverlight. So it is proved that WP7 never allows you to access the https url without proper certificate. But how to install certs in WP7 .When I asked my fellow developer who has Windows Phone, he told that he normally send the certificate file (.cer)through mail and the opening of attachment in phone will prompt for installation.

But where is the mail client in simulator. Tried to send to my gmail id. But no luck.Then I tried the next way. Export the certificate into .cer format, host the .cer file in IIS and access it from Phone’s browser.Yes it worked. I was able to install the certificate. But again I got error in getting data through REST service. That was due to certificate URL issue .I should have used the exact certificate which is created for the IIS. ie for the localhost/127.0.0.1.

Below are the steps to install the certificate in WP7 and get the Azure REST services working which is hosted in Azure emulator.
  • Export the certificate as .cer
    • The certificate should be for the exact web address. ie export the certificate for 127.0.0.1 from IIS.
  • Host this certificate .Even in your http://localhost/<cert Name>.cer
  • Make sure your IIS has the proper MIME type for the .cer
    • MIME type for .cer certificate file is application/x-x509-ca-cert
  • Browse to this certificate path from Windows Phone 7 emulator.
  • Install the certificate.
Now you are all set to access https service hosted Windows Azure emulator from Windows Phone 7 with debugging support.
Enjoy… production environment in development.