Archive for the ‘Mobile’ Category

New Mobile Blog

I haven’t been blogging regularly here, and most of the older content is related to .Net, which I don’t really work with anymore. I am mostly concerned with mobile development (Android, iOS, Mobile Web, Web Services). So I am curating new content about mobility at my new mobile blog:

Mobile Notes

Thanks for reading here and please visit me at my new blog!

Posted on:
Posted in Mobile | Comments Off on New Mobile Blog

HMAC Algorithm in Swift

In your Xcode project, make a bridging header and include this header file:

#import <CommonCrypto/CommonHMAC.h>

Then, create a Swift file. We will create the hashing function as an extension method to String.

import Foundation

extension String {

    func digestHMac256(key: String) -> String! {

        let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
        let strLen = self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

        let digestLen = Int(CC_SHA256_DIGEST_LENGTH)
        let result = UnsafeMutablePointer.alloc(digestLen)

        let keyStr = key.cStringUsingEncoding(NSUTF8StringEncoding)
        let keyLen = key.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

        let algorithm = CCHmacAlgorithm(kCCHmacAlgSHA256)

        CCHmac(algorithm, keyStr!, keyLen, str!, strLen, result)

        let data = NSData(bytesNoCopy: result, length: digestLen)

        let hash = data.base64EncodedStringWithOptions(.allZeros)

        return hash
    }
}

The function takes in the string to use as the key. In HMAC, this key is shared between the client and server.

Use the function like this, where result ends up being the Base64 encoded string:

    let data = "Message"
    
    let result = data.digestHMac256("secret")

GitHub Gist

Other references:

CommonHMAC in Swift

Examples of creating base64 hashes using HMAC SHA256 in different languages

Posted on:
Posted in Mobile | Comments Off on HMAC Algorithm in Swift

Michigan Google Developers DevFest 2014

I will again be speaking a the Michigan Google Developers DevFest this year. I enjoyed myself last year and made contacts that have affected my career.

This year I am talking about dependency injection in Android. Unit testing is important, and dependency injection is vital for designing testable classes. Come see me and learn about it.

Google Developers Logo

Posted on:
Posted in Mobile, Programming, Speaking | Comments Off on Michigan Google Developers DevFest 2014

Android Best Practices Book

As of this week I am a published technical author.

As a result of my speaking at Michigan Google Developers DevFest in April of 2013, I met Godfrey Nolan, who was also speaking at the event. In June he invited me to contribute to his upcoming book. The result is Android Best Practices, published by Apress. I contributed the chapter on Web Services. I am honored that Godfrey chose me, and I am pleased with the result. I got my physical copy of the book yesterday, it was published officially on the 14th.

Mobile Development Is New or Different?

The Mobile Problem

In a mobile device, we have four factors that are different from contemporary desktop computers. They are:

  • Less memory
  • Less CPU capacity
  • Constrained network bandwidth
  • Small screen size

These thing may seem difficult, but really we experienced this before in the early days of the web explosion. PC’s were much less powerful, common screen resolutions were 640 x 480, and network access was over a 14.4k modem or less. We’ve just gotten lazy in the days of blazing networks and a dozen iterations of Moore’s law, and some of us developing for mobile today never even lived through any of that.

Memory constraints certainly keep us on our toes. We can’t be sure our app will survive consistently in the phone OS, it could be terminated at any time due to memory constraints based on user activity. So we solve problems by making objects parcelable in Android, or persisting the state of any given network call in a database so we can pick up again at a later time if necessary.

These constraints are driving how we design apps and the systems that support them. REST + JSON gives us smaller network messages. We minimize CPU usage by preferring JSON over XML so there is less effort serializing, and we offload complex or data-intensive calculations to a server somewhere in the cloud.

Mobile has rightfully caused the rise of graphic design as a regular component in app development, not just something start-ups are doing. Apps are getting more user-friendly despite the small screen size, and focused in their functionality as a result of good design. Users like apps, no longer is it something they are forced to use because they have choices. Also, users finally get a vote (in $$) on whether or not an app succeeds. We are getting away from monolithic web apps. Smaller apps have fewer paths which make them more testable and have fewer bugs  as a result which is better for users.

This time around developers have the benefit of having lived through the web explosion and learned what not to do. I hope more companies build enterprise-level mobile apps keeping this in mind and don’t recreate the nightmarish internal web apps on phones.

Posted on:
Posted in Mobile | Comments Off on Mobile Development Is New or Different?

Using the Camera from Mobile Web

In the early days of mobile web, it was not possible to access the camera from a web page. While Android added the functionality a while ago in Honeycomb, it seems to have quietly slipped in to iOS in 6.0. So now, the majority of smartphone users can use the camera from a web site.

The HTML code is to access the camera is surprisingly simple:

<input type='file' accept="image/*" capture="camera" onchange="previewImage(this);" />

The accept and capture attributes do all the work. In my example I’ve also added an onchange handler to add a preview of the image to the HTML page:

<div style="height: 400px; width:200px"><img id="preview" src="" alt="your image here" /></div>
<script type="text/javascript">
     $('#preview').on('load',function() {
         var css;
         var ratio=$(this).width() / $(this).height();
         var pratio=$(this).parent().width() / $(this).parent().height();
         if (ratio<pratio) {
             css={width:'auto', height:'100%'};
         } else {
             css={width:'100%', height:'auto'};
         }
         $(this).css(css);
     });
     function previewImage(input) {
         if (input.files && input.files[0]) {
             var reader = new FileReader();
             reader.onload = function (e) {
                 $('#preview').attr('src',e.target.result)
             };
             reader.readAsDataURL(input.files[0]);
         }
      }
</script>

Try it out

Interestingly, on iOS the photo stays in memory and is not persisted to the device. On Android, the photo is automatically persisted to the gallery. I suspect this is for memory management.

I have tested this code successfully on the following platforms:

  • Android 3.0+ browser
  • Chrome on Android
  • Mobile Safari on iOS6+
  • BlackBerry 10

The only large group of smartphone users left out in the cold are Android users still on Gingerbread.

I tested this code on WP8, and it didn’t work. There may be a work around, but I haven’t had a chance to work that out yet.

Posted on:
Posted in JavaScript/AJAX, Mobile | Comments Off on Using the Camera from Mobile Web

Michigan Google Developers DevFest 2013

I will be speaking at this event on Saturday, March 23rd. I will be talking about how to consume web services using Android, covering two different patterns for doing this. This is a hands-on event, so I’ll have starter code ready for participants and even web services ready for consumption. I’m looking forward to another round of public speaking. You can register here.

Posted on:
Posted in Mobile, Speaking | Comments Off on Michigan Google Developers DevFest 2013

Hospice of Michigan Mobile App

My team built an app for Hospice of Michigan to help families stay connected when they have a loved one in hospice. The app keeps them connected with the care their loved one is receiving. Read about it at CBS Detroit. You can also see a demo video on YouTube.

Posted on:
Posted in Mobile | Comments Off on Hospice of Michigan Mobile App

Browser Stats

Here are the browser stats for my site from Jan 15 2013 to Feb 14 2013:

Browser Percent
Chrome 47.35
IE 26.41
Firefox 22.49
Safari 2.36
Opera 0.98
Android 0.41

And more detail for IE:

Version Percent
10.0 12.65
9.0 42.9
8.0 38.58
7.0 5.56
6.0 0.31

I am still seeing a lot of IE8, and more 7 than I expected.

And lastly, mobile devices:

Browser Percent
iPad 48
iPhone 26
Android 19
Blackberry 4
Posted on:
Posted in ASP.NET, Mobile | Comments Off on Browser Stats

Getting Started in Enterprise Mobility

If you are thinking about building enterprise mobile apps, you should be thinking about something else first:

Web Services

You are going to need a way to move data to and from devices, and you’ll need the services before you can build those apps. Start planning on those services now, leverage your existing team to build out WCF or JAX-RS APIs for the data you to need to share or accept. All that talk a few years back about SOA is now coming to fruition with an expanding new generation of consumers of web services.

Here’s another hint on that front:

JSON + REST

Unfortunately, most of that talk about SOA a few years back focused on SOAP services, and things like WS-*. Mobile devices have a constrained network pipe, so you want the smallest message possible. Sorry XML. Also, mobile devices tend to be CPU constrained, so you don’t want to do a bunch of parsing either. Sorry XML. As an added bonus, web apps are good at consuming JSON as well.

Start with services, then move on to the mobile apps.

Posted on:
Posted in Mobile | Comments Off on Getting Started in Enterprise Mobility