Wednesday, January 30, 2013

Determining Internet Connection status in a WinRT app

Last week I received word from someone using the Scripture Box Windows 8 app that he continually received notifications that he wasn't connected to the internet. That is, the app thought he was disconnected and therefore wouldn't try to sync with the Azure mobile service used for cloud storage.

I was quite confused. In all my testing - both with connected and disconnect scenarios (on multiple machines) - the app always correctly identified my internet connection status. I was using code similar to the following:

         var ConnectionProfiles = NetworkInformation.GetConnectionProfiles();  
         foreach (var connectionProfile in ConnectionProfiles)  
         {  
           if (connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)  
           {  
             App.IsConnectedToInternet = true;  
             doSync = true;  
             break;  
           }  
         }  

While searching for potential problems, I ran across another slightly different method to discover one's connection status:

       ConnectionProfile conProfile = NetworkInformation.GetInternetConnectionProfile();  
       if (conProfile != null && conProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)  
       {  
         App.IsConnectedToInternet = true;  
         doSync = true;  
       }  

From the documentation I couldn't quite tell what the difference was, and which was the correct to use.

I also happened to stumble upon this helpful post that basically included a simple but effective manual approach to checking for internet connectivity:

     public static async Task<bool> IsConnectedToInternetManualCheck()  
     {  
       HttpWebRequest req;  
       HttpWebResponse resp = null;  
       Uri url = null;  
       url = new Uri("http://microsoft.com");  
       req = (HttpWebRequest)WebRequest.Create(url);  
       try  
       {  
         resp = (HttpWebResponse)await req.GetResponseAsync();  
         req.Abort();  
         req = null;  
         resp = null;  
         return true;  
       }  
       catch  
       {  
         req.Abort();  
         req = null;  
         return false;  
       }  
     }  

I figured this was a last resort effort to check for internet connectivity. So which method to use? I couldn't reproduce the issue, and I certainly didn't want to break what was working for others. I also didn't want to go through the release/certification process multiple times for the same issue. (For a few seconds I was tempted to see if this person would be interested in side-loading for testing purposes.)

I decided to take the safe approach and chain the three methods together: first checking with NetworkInformation.GetInternetConnectionProfile(). If that didn't work, then I'd loop through all network connections with NetworkInformation.GetConnectionProfiles(), and finally, if necessary, using the manual method above. Not necessarily pretty, but in the end one of the two additional methods worked. I just wish I knew which one.

And on an encouraging side note - the app update passed through the Microsoft Store certification process in approximately 12 hours(!) and was available in the store shortly thereafter. Good job, Microsoft.


Tuesday, January 1, 2013

WinRT App Development from Design to Certification

For the past six months I've worked on a Windows 8 (WinRT) app that started out as a request by my wife, but grew into something I planned to distribute via the Windows Store.  Lots of learning, planning and late nights were required.

I started to loose steam around the beginning of December, but I pushed through that last week until I was finally ready to submit to the store for certification.  I thought I had finished the hard part (design and coding) but then I came to the point of dealing with certification. There were a few items I didn't even consider in my design that I discovered were required for certification, and I had to go back and address those (like implementing certain interfaces).

Take the time up front to look at the certification requirements.  There are a number of resources on app submission guidelines and checklists.  Pete Brown's blog post is helpful, among others.  But Microsoft's own documentation is the most comprehensive and contains everything you need.  It is tempting to want to quickly get through it because of all the points it covers.  Don't.  Invest the time.  Otherwise, you may miss out on some opportunities or simply fail certification. 

Beyond that, here is a recap of the tools/services that I've relied on to get my app completed:
It feels good to be done (at least with initial release).  Hopefully the app will help many while paying for itself, but at the very least I've (a) given my wife and family a helpful tool and (b) learned a lot that I can apply in other areas of my work.

Check it out:  ScriptureBox.com