Tuesday, December 4, 2012

Parsing JSON With SBJSON


This post will focus on parsing the JSON returned from the web-service.
My personal choice for parsing JSON is the SBJON library. There are others out there such as TouchJSON and YAJL so check them put and decide for yourself.
Let’s start with a quick recap on what JSON is and how it can be used.
Wikipedia says:
JSON (an acronym for JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for virtually every programming language.
JSON presents its data as key-value pairs. Each value is referenced by a key name, which is a string. If you were to represent a person in JSON, their name would be referenced by the key “name” like so: “name” : “James”.
So, JSON represents data in a way that can be passed between applications easily. Perfect.
So when parsing data from a webservice, the first thing you should do is figure out your model. Look at an example of the webservice’s response and figure out which bits represent objects, arrays of objects, fields that belong to an object, etc.
But what kinds of data can JSON represent?
Objects are everything between the braces ( { } ).
Strings are enclosed in quotes.
Numbers aren’t enclosed by anything and just appear as numbers, e.g. 12345.
Arrays are enclosed in square brackets ( [ ] ).
Booleans take the form of the words ‘true’ or ‘false’ (without quotes).
Null values are represented as ‘null’ (without quotes).
So an example of JSON using all these data types:
{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address": 
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber": 
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
 }
And it’s representation in Objective-C:
#import <Foundation/Foundation.h>
 
@interface Person : NSObject
{
    NSString *firstName;
    NSString *lastName;
 
    NSInteger age;
 
    NSDictionary *address;
 
    NSArray *phoneNumbers;
}
 
@end
You may think we’ve missed out some information, such as the details of the address, and phone numbers. It’s you decision how you model your objects. I’ve chosen to store the address details in a dictionary, each value being referenced by a key name, just as it is in JSON. The phone numbers also stored in dictionaries, but then those dictionaries put into an array.
If you wanted to, you could create another Class named Address and use it to store the address details. This may be a more object-oriented approach and useful if addresses are used in other places throughout your application without needing to be tied to a Person.
So now that you have your object model, you need to get the data out of the JSON and create instances of your model.
SBJSON has a useful SBJsonParser class that can parse an entire JSON object in one line:
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSError *error = nil;
NSArray *jsonObjects = [jsonParser objectWithString:jsonString error:&error];
[jsonParser release], jsonParser = nil;
SBJSON treats JSON objects as dictionaries in Objective-C. Depending on the webservice you may get a JSON object as the top level object or you may get an array. For this reason, objectWithString:error: has id as it’s return type. You can use Objective-C’s dynamism to determine which type of data store the parser as returned, like this:
//replace all ' with ".
jsonObject = [jsonObject stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
id jsonObject = [jsonParser objectWithString:jsonString error:&error];
 
if ([jsonObject isKindOfClass:[NSDictionary class]])
    // treat as a dictionary, or reassign to a dictionary ivar
else if ([jsonObject isKindOfClass:[NSArray class]])
    // treat as an array or reassign to an array ivar.
If the webservice only ever returns one of the two representations as it’s top level then you can go ahead and assume it will be either an Array or Dictionary, and not have to worry about checking.
Now you have your JSON data in a format that you can manage via Objective-C. All you need to do now is iterate over the contents of the dictionary/array and create instances of Person to represent them.
One thing that’s important to remember is that literal numbers such as the age value in our Person example will be wrapped in NSNumber objects, so we’ll need to call ‘intValue’ on them to get the number.
NSMutableArrary *people = [NSMutableArray array];
 
for (NSDictionary *dict in jsonObjects)
{
    Person *newPerson = [[[Person alloc] init] autorelease];
    [newPerson setFirstName:[dict objectForKey:@"firstName"]];
    [newPerson setLastName:[dict objectForKey:@"lastName"]];
    [newPerson setAge:[[dict objectForKey:@"age"] intValue]];
    [newPerson setAddress:[dict objectForKey:@"address"]];
    [newPerson setPhoneNumbers:[dict objectForKey:@"phoneNumber"]];
 
    [people addObject:newPerson];
}
And there we have it.
One final thing to take note of. You’ll notice that I used literal strings as key names while creating the Person objects. It might be better for you to determine each key name that you’ll be using and create a string constant for them. That way, when you’re parsing your data, if you misspell firstName, the compiler can throw an error (because it won’t match the name of the constant you created) and save you a lot of time debugging when, for some damn reason, you’re not getting any value for @”firtName”!

Source:http://jasarien.com/?p=428
Thanks to Jasarien 

Thursday, October 18, 2012

Why mobile apps platform ?


Why mobile apps platform ?
________________________________________________      
It’s necessary to understand why mobile applications are so important. Organizations and individual users often buy smartphones because they want to stay in touch via email and maintain their calendars and to-do lists while they’re away from the office. Some enterprise customers and many consumers buy smartphones because they’re cool, and they want to stay hooked into current technology. This has led to great opportunities in mobile platform.

1. Ubiquity of smartphones:
Smartphones will increasingly replace feature phones in stores. Nearly everyone will be able to use smartphone apps, not just – as of now – business people, social networkers and gamers.The main driver will be a reduction in handset prices.

2. Manifoldness of possibilities:
Smartphones are small computers which are becoming more and more powerful.
They will be suitable for an increasing number of tasks which have previously been restricted to laptops or desktops.

3. Ubiquity of app stores:
For the foreseeable future, Smartphones will always depend on the app stores.
If you have a smartphone or advanced feature phone you will have easy and convenient access to the world of apps.

4. Unmatched user-experience:
Apps offer a user-experience which mobile Websites or widgets are unable to provide.

  • Mobile Campaigns and Ads
Mobile marketing presents a distinct and unique way to create interactive dialogues with customers. Research indicates that mobile ads perform about five times better than Internet ads. The most common mobile ads are simple text links and display  adds that are sold based on cost per clicks, cost per acquisition and cost per thousand. These ads are much like the  paid search campaigns on Google, Bing or Yahoo! Use mobile marketing solutions to drive participation at exhibitions   or to drive traffic to retail environments.

  • Location-based Services
Location-based shopping coupons using mobile devices are gaining popularity. As mobile users become more acclimated to sharing their whereabouts via mobile devices.In fact, more than 50 percent of respondents indicated that they wanted to receive location-specific advertising.

  • Monetize Mobile Apps
Consider offering a free version of your app and then let users decide whether or not they are willing invest in a premium version with more features and content. Take for example the widely popular Angry Birds iPhone game.  Its ongoing promotion was to offer a free version, while paid subscribers were given access to more challenging   levels and other free add-ons.





5. Proximity to customers:
Mobile devices, especially Smartphones, are much more personal and intimate devices than a laptop or desktop device.  For most users their phones are never further than 1 meter away 24/7. Imagine how attractive it is for consumer goods,   food, and myriad other companies across the economy to be able to place their products and services so close to consumers.




Mobile OS
Desktop OS
Lightweight
Many features
Targeted to ARM
Targeted to x85
Less Powerfull
More Powerful
More concerned with battery life
More concerned with power




List of Mobile Operating System   
________________________________________________                            

  1. Symbian
    1. Developed by the Symbian Foundation
    2. Used to be the largest market share
    3. Nokia is abandoning Symbian for smartphones and only using it for feature phones
    4. Phased out by 2016.


  1. Bada
    1. Developed by Samsung
    2. Samsung plans to convert their feature phones to Bada making them smart phones
    3. Samsung still ships many Android smartphones

  1. webOS
    1. Launched by Palm, in 2009
    2. Bought by HP in 2010
    3. webOS hardware was discontinued in August 2011
    4. Possibly will make its way into printers and HP PCs
    5. HP announced that it will open source webOS


  1. Blackberry
    1. Developed by Research In Motion (RIM)
    2. Originally designed for business use
    3. Highly Secure
    4. Integration with corporate network (Email, contacts, network, etc).
    5. Switching to using QNX OS instead of the
    6. longstanding Blackberry OS


  1. Windows Phone / Windows Mobile
    1. Developed by Microsoft
    2. Replaced Windows Mobile in 2010
    3. New UI (Metro)
    4. Integration with Microsoft services
    5. Zune, Bing, SkyDrive, etc.

  1. Android
    1. Developed by a small startup, Android Inc.
    2. Purchased by Google in 2005
    3. Linux derived
    4. Open source and part of the Open Handset Alliance
    5. Developers have almost complete control

  1. iOS
    1. Developed by Apple Inc.
    2. Runs iPhones, iPads, iPod Touches, and
    3. AppleTV
    4. Based on the OSX Kernel



Why to be an iOS Developer

Sometimes I ask myself the question: why do I develop for iOS? I could also do Haskell development, do cool HTML5 stuff, or build compilers. As an indie developer, these are some important reasons for me.

  1. High-end platform
The iPhone and iPad are the high-end of the smartphones and tablets. This means that the people who use it are willing to pay more money for a better experience. As a side effect, they are also willing to pay for apps, and willing to pay for in-app purchases. For an indie developer, this is great. The more willing your customers are to spend, the better it is for us. Ultimately, it comes down to the developer making something that is worth the money, but it’s a lot easier to sell to high-end customers.

  1. Closed environment
There are a lot of disadvantages to having a closed environment, which is beyond the scope of this post. However, for customers it is mostly a big advantage: they can trust the applications. Because every single application has been reviewed by Apple, they know they can trust it. Because in-app purchases are built into the iOS Platform, they know they can trust the payments, which again makes it easier for developers to charge for something.

  1. Lots of possibilities for innovation
Innovation can definitely be everywhere, but I have the feeling we’re still only seeing the tip of the iceberg when it comes to innovation on mobile devices.  There is so much more we can still do with the location sensors, the fact that we always carry our phones, always have an internet connection, et cetera.  With the web or on the desktop there is still a lot of room for innovation too, but it is a lot harder to come up with new ideas.

  1. More difficult than web development
It is more difficult to get started with iOS programming than with web development. You need to do memory management, need to work with design patterns, learn a new language, and more. In web development, you can gradually ease in to it by first modifying HTML, then building your own HTML site, adding PHP, and so on. This makes the barrier to entry harder, which means it is easier to get noticed as an iOS developer.

  1. Great libraries
Apple provides awesome libraries. Even though I don’t like Objective-C so much, the frameworks they provide are really great. For example, animation is super-easy, and everything is very well thought out. This makes it a pleasure to work with.




  1. Culture of quality
On Mac and iOS, there has always been a culture of quality. Applications are very polished, and you can see that developers really put effort into building their products. This is a lot of fun and very inspiring for a developer. It pushes you to raise the bar and have higher standards. The defaults provided by Apple are very high quality too, which means it’s easy to build something that works and looks good.


  1. iOS Developers earn more  than Android Developers
As per Distimos Annual Report for 2011, iOS Developers made more money than Android Developers (source http://bit.ly/z3Bj0E). As per the report Apple App Store for iPhone generates about four times the revenue that is generated in the Google Android Market. That's a good reason to choose iOS over Android.

  1. Easy to test your apps thoroughly
There are only a handful of iOS Devices that you will need to test your apps. Thus, you can be sure that when you release your app it is going to run smoothly on each and every iOS Device. Also, all these devices are very popular, that's why it's easy to find someone using an iphone/ipod to test your app.

Again match that with the diversity of android devices which make it impossible to check your app on each and every device available in market. Even if the device features are same, yet the hardware inside the phone may belong to different manufacturers. Some hardware may not support the functionality that your app requires, as smoothly as other manufacturers hardware. Thus, the same app may not run similarly on two devices even with same technical specifications.

  1. Less Resources for Development
If you are going to develop for iOS platform you have to consider only two possible screen sizes (320x480 and 640x960) unless you are also developing for iPad. Also, even if you support just one screen size (320x480) it would run perfectly on higher resolution iPhone/iPod.

Compare that to the thousands of Android Devices available with so many of them having specific resolutions. Though you can shrink/stretch an image to fit a particular resolution, but that requires extra work on your part. iOS just reduces your work by maintaining screen size across different versions.

  1. Using Your App will Always be a Great Experience
The touch response of all the iOS Devices is great and you will be very satisfied when you use your app on them. Apple products  are always built using the best quality display and hardware components. Thus, you can be sure that your app will look great and respond quickly.

You will also find some excellent mobile phones running Android. But there are also a lot of android based handsets that are built using cheap components and use very poor quality  display material. So even though you make a terrific app, but using your apps on such low quality handsets will deeply disappoint you.

It’s like “Being with the winner make you the winner”.

  1. Think Different
Ever since the apple started it has always followed this very inspiring thought Think Different. Here’s a sample video narrated by lated Steve Jobs.



     

Friday, September 21, 2012

App Icons on iPad and iPhone


iPhone-only Apps

Include the following in your application's Resources group in the Xcode project:

Table 1  iPhone-only apps icon requirements.
Image Size (px)
File Name
Used For
Required Status
512x512iTunesArtworkAd Hoc iTunesOptional but recommended
57x57Icon.pngApp Store and Home screen on iPhone/iPod touchRequired
114x114Icon@2x.pngHome screen for iPhone 4 High ResolutionOptional but recommended
72x72Icon-72.pngHome screen for iPad compatibilityOptional but recommended
29x29Icon-Small.pngSpotlight and SettingsOptional but recommended
50x50Icon-Small-50.pngSpotlight for iPad compatibilityRecommended if you have a Settings bundle, otherwise optional but recommended
58x58Icon-Small@2x.pngSpotlight and Settings for iPhone 4 High ResolutionRecommended if you have a Settings bundle, otherwise optional but recommended

iPad-only Apps

Include the following in your application's Resources group in the Xcode project:

Table 2  iPad-only apps icon requirements.
Image Size (px)
File Name
Used For
Required Status
512x512iTunesArtworkAd Hoc iTunesOptional but recommended
72x72Icon-72.pngApp Store and Home screen on iPadRequired
50x50Icon-Small-50.pngSpotlight on iPadOptional but recommended
29x29Icon-Small.pngSettings on iPadRecommended if you have a Settings bundle, otherwise optional but recommended

Universal Apps

Include the following in your application's Resources group in the Xcode project:

Table 3  Universal apps icon requirements.
Image Size (px)
File Name
Used For
Required Status
512x512iTunesArtworkAd Hoc iTunesOptional but recommended
57x57Icon.pngApp Store and the Home screen on iPhone/iPod touchRequired
114x114Icon@2x.pngHome screen for iPhone 4 High ResolutionOptional but recommended
72x72Icon-72.pngApp Store and Home screen on iPadRequired
50x50Icon-Small-50.pngSpotlight on iPadOptional but recommended
29x29Icon-Small.pngSettings on iPad and iPhone, and Spotlight on iPhoneRecommended if you have a Settings bundle, otherwise optional but recommended
58x58Icon-Small@2x.pngSpotlight and Settings for iPhone 4 High ResolutionRecommended if you have a Settings bundle, otherwise optional but recommended

Add Icon files in Info.plist

To add the Icon files entry you will need to manually edit your Info.plist.
Open your Info.plist in Xcode, and locate the "Icon file" entry. Click on the plus (+) button at the end of the entry to make a new key entry. as shown in Figure 2.
Figure 2  Add a new entry under "Icon file" key

Then, type "Icon files" in the key column (notice the "s" added), or choose "Icon files" from the drop down list, as shown in Figure 3.
Figure 3  Add "Icon files" key in Info.plist

Next, turn down the disclosure triangle on "Icon files" and add the entries for the icons for your application, as shown in Figure 4 and Figure 5:
Figure 4  Add icon file entries for iPhone-only or Universal apps.
Figure 5  Add entries for iPad icon files.