Monday, July 25, 2011

iPhone/iPad Technical Interview Question(FAQ) with answers(Part 2)


Q11. What is atomic and nonatomic property attribute? 
A.
Both are same unless we use our setter and getter methods.
If we create our own setter and getter method, then atomic is useful in thread processing, but it is slow.
Nonatomic does not give guarantee on thread processing but it is fast.

Q12. What is the life cycle of view controller?
A.  Press here....
  
Q13. Difference between viewDidLoad, viewWillAppear, viewDidAppear and viewLoad?
A.
  • ViewDidLoad - The viewDidLoad method is only called when the view is first loaded from the Nib file. If the view was already loaded and you push the view again it will not fire again. viewDidLoad is things you have to do once.Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the ViewDidLoad method. Basically this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.
  • ViewWillAppear: I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method, becuase when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).
when you are loading things from a server, you also have to think about latency. If you pack all of your network communication into viewDidLoad or viewWillAppear, they will be executed before the user gets to see the view - possibly resulting a short freeze of your app. It may be good idea to first show the user an unpopulated view with an activity indicator of some sort. When you are done with your networking, which may take a second or two (or may even fail - who knows?), you can populate the view with your data. Good examples on how this could be done can be seen in various twitter clients. For example, when you view the author detail page in Twitterrific, the view only says "Loading..." until the network queries have completed.
  • ViewDidAppear: Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice call to get extra data for the form above.The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.
  • ViewLoad: loadView is the method in UIViewController that will actually load up the view and assign it to the "view" property. This is also the location that a subclass of UIViewController would override if you wanted to programatically set up the "view" property.
          I've found that often when I add init code to loadView, I end up with an infinite stack trace
          Don't read self.view in -loadView. Only set it, don't get it.
          The self.view property accessor calls -loadView if the view isn't currently loaded. There's your infinite recursion.


All The Best :)


Next(Part 3)….
                                                                                                           
                                                                                                     

Wednesday, July 20, 2011

QR Code reader/scanner for iphone app in objective c (source code) using ZBarSDK

Hey, I have googled a lot and found very less material for how to implement a QR code reader in our iPhone app. Here I have integrated all material and implemented in my project.
So here I am sharing my experience of implementing a QR code reader in our iPhone app using ZBarSDK.


Step 1: Download  ZBarSDK from here.


Step 2: Create a new project in xcode and name it as QRscanner.


Step 3: Integrate this  ZBarSDK folder in your xcode project by drag and drop on equal level of class folder in left panel in xcode.


Step 4: Add these 7 frameworks in Frameworks folder: 

  1. AudioToolbox.framework
  2. CoreMedia.framework
  3. QuartzCore.framework
  4. SystemConfiguration.framework
  5. libiconv.dylib
  6. AVFoundation.framework
  7. CoreVideo.framework

Step 5: Now Go To this link and generate some QR codes.
(to generate QR codes only add the text which you want to attach with QR code at the end of the link (e.g. I have written archana) and press enter, and right click to save this image.) 


Step 6: Add these generated QR code's image in iphone library.


Step 7: Now open QRscannerViewController.h in your xcode and write this code:


#import <UIKit/UIKit.h>
#import "ZBarSDK.h"

@interface QRscannerViewController : UIViewController <UIImagePickerControllerDelegate,ZBarReaderDelegate>{

IBOutlet UITextView *resultTextView;
}
@property (nonatomic, retain) IBOutlet UITextView *resultTextView;
@property (nonatomic, retain) UIImagePickerController *imgPicker;

-(IBAction)StartScan:(id) sender;

@end

Step 8: Now open QRscannerViewController.m in your xcode and write this code:





#import "QRscannerViewController.h"

@implementation QRscannerViewController

@synthesize imgPicker,resultTextView;

-(IBAction)StartScan:(id) sender
{
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.readerView.torchMode = 0;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
  config: ZBAR_CFG_ENABLE
  to: 0];
// present and release the controller
[self presentModalViewController: reader
animated: YES];
[reader release];
resultTextView.hidden=NO;
}

- (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
                             withRetry: (BOOL) retry{
NSLog(@"the image picker failing to read");
}

- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
NSLog(@"the image picker is calling successfully %@",info);
// ADD: get the decode results
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
NSString *hiddenData;
for(symbol in results)
hiddenData=[NSString stringWithString:symbol.data];
NSLog(@"the symbols  is the following %@",symbol.data);
// EXAMPLE: just grab the first barcode
//  break;
// EXAMPLE: do something useful with the barcode data
//resultText.text = symbol.data;
resultTextView.text=symbol.data;
NSLog(@"BARCODE= %@",symbol.data);
NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:hiddenData forKey:@"CONSUMERID"];
NSLog(@"SYMBOL : %@",hiddenData);
resultTextView.text=hiddenData;
[reader dismissModalViewControllerAnimated: NO];
}


Step 9: Now open QRscannerViewController.xib and design it UI like this:






Step 10: Now make connection of above 2 objects(UITextView and UIButton) with IBOutlet resultTextView and IBAction StartScan.(Refer Step 7 of…) 


At last build and run this app.


How to run this app:


Press  "START SCAN" button, and as we are running our app in simulator you will get screen like this:






Now press and hold (alt key)+(left click), Photo Album (image gallery, photo library) will open.
Select any QR code image. It will take hardly 2-3 seconds for scanning and finally you will get home screen with scanned data associated with QR code image on text view.


Download source code from here.