Friday, February 25, 2011

Welcome to iPhone Native Application Development site !!!

Welcome !!!. This blog will record my experiences in learning iphone programming which I hope will be beneficial to those who are interested in iphone/iPad programming.

I hope my articles will be in very simple language, that anyone who have some basic programming background can follow and develop his or her own application.

So await for more exciting articles.

Archie :)

Next….

Difference between COCOA,COCOA touch and objective C.

Objective C is a dynamic programming language - a bit like C++ and a bit like Java.

Cocoa is the application framework for Mac OS X.

Cocoa Touch is the application framework for iPhone and iPod Touch - very similar to Cocoa.

Cocoa is commonly referred to as the combination of the Foundation and AppKit frameworks, while Cocoa Touch is the combination of the Foundation and UIKit frameworks.

Cocoa and Cocoa Touch sit on top of other collections of frameworks to create the API stacks. The other layers are Media, Core Services and Core OS.

The main difference between Cocoa and Cocoa touch is that the UI classes and APIs aren't the same as Mac OS X, so instead of NSTextField, you have UITextField. Many of the classes share the same functionality and can be ported quite easily by simply changing the class name, though most will require some more changes, but usually nothing too heavy.

There are also some differences between the Foundation frameworks in Cocoa and Cocoa Touch, most commonly missing classes, eg, Cocoa has NSHost and Cocoa Touch doesn't.

Next….

How to view pdf and other files.


How to view PDF/DOC/HTML and other files on your iPhone and iPad.
Its an application in which there will be more than one buttons and respective file will open on button click.It will look like this:
 






you can add more buttons for more files according to your requirement.


Step1:  Repeat step1 & step2 of "Introduction to Xcode -How to Switch Views through Button" to create new project and name it as "viewFile"
 





We will get 4 files in class folder named as:
"viewFileAppDelegate.h"
"viewFileAppDelegate.m"
"viewFileViewController.h"
"viewFileViewController.m"
 and "viewFileViewController.xib" file in Resource folder.



Step2: Now to create one more view to see files repeat step4 of "Introduction to Xcode -How to Switch Views through Button" and named it as "viewFileOnButtonClick"

You will get 3 new files in class folder named as: 
viewFileOnButtonClick.h
viewFileOnButtonClick.m
viewFileOnButtonClick.xib

Step3: Open viewFileViewController.xib and Repeat step3 of "Introduction to Xcode -How to Switch Views through Button"  to create UI till it will look like this:















Here, we have taken 2 "UIButtons" and set their titles as "PDF" and "DOC", and set background colour. To set set background colour open inspector window-> view attribute tab-> view-> Background.

Note: Set the Tag Attribute of "PDF" & "DOC" buttons as 1 and 2 respectively from inspector window. See the below figure: 












Step4: Open "viewFileOnButtonClick.xib" and create UI so that, It will look like this:
Here we will use one "UIWebView" to view files and adjust its size (upto the area in which u want to see pdf file.)  and one "UIButtons" and set its title as "Back".














Remember, To Fit the pdf(any file) page of different size to the screen of the iPad/iPhone just open respective .xib file then go to inspector of UIwebView then web view attributes and there tick mark scales page to fit option.















Step5: Next, let's type out the code for the "viewFileViewController.h" file. The code is listed below. Open viewFileViewController.h file and and make these changes:
#import <UIKit/UIKit.h>

@interface viewFileViewController : UIViewController {

NSInteger bTag;
}
@property(nonatomic,readwrite) NSInteger bTag;

-(IBAction)showFile: (id)sender;

@end
Explanation of code:
NSInteger bTag
bTag is the integer variable to hold the value of pressed button's tag.

-(IBAction)showFile: (id)sender;
This is the action which will be call when the PDF or DOC button will be pressed.


Step6:  Open "viewFileViewController.m" file and make these changes:

#import "viewFileViewController.h"
#import "viewFileOnButtonClick.h"



@implementation viewFileViewController

@synthesize bTag;

-(IBAction)showFile: (id)sender
{
self.bTag=[sender tag];
NSLog(@"button tag value in its own view: %d", self.bTag);
viewFileOnButtonClick *buttonFileview=[[viewFileOnButtonClick alloc] initWithNibName:nil bundle:nil buttontag:self.bTag ];
buttonFileview.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:buttonFileview animated:YES];
}
Explanation of code:

-(IBAction)showFile: (id)sender


{
self.bTag=[sender tag];
NSLog(@"button tag value in its own view: %d"self.bTag);
viewFileOnButtonClick *buttonFileview=[[viewFileOnButtonClick allocinitWithNibName:nil bundle:nil buttontag:self.bTag ];
buttonFileview.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:buttonFileview animated:YES];
}

when this action will be called bTag will hold tag value of sender i.e. button pressed, and then an another view i.e. viewFileOnButtonClick will load.


Step7:  Open "viewFileOnButtonClick.h" file and make these changes:
#import <UIKit/UIKit.h> 

@interface viewFileOnButtonClick : UIViewController {



IBOutlet UIWebView *pdfview;
NSInteger btnTag;
}
@property(nonatomic,retain) IBOutlet UIWebView *pdfview;
@property(nonatomic,readwrite) NSInteger btnTag;

-(IBAction)backButtonPressed:(id) sender;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil buttontag:(NSInteger)bTag;


@end

Explanation of code:
Here, we have declared 2 variables pdfview and btnTag, to show the pdf(any) file and hold tag value respectively.

-(IBAction)backButtonPressed:(id) sender;
This is Action. Typically, we use buttons to activate actions. We have to code these actions later on in the .m file. Here we have 1 button. when we will click on it application will switch to previous view.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil buttontag:(NSInteger)bTag;
This is the method which hold the button's tag value from previous view to its own view.


Step8:  Open "viewFileOnButtonClick.m" file and make these changes:

 #import "viewFileOnButtonClick.h"
 #import "viewFileViewController.h"




@implementation viewFileOnButtonClick

@synthesize pdfview;
@synthesize btnTag;

-(IBAction)backButtonPressed:(id) sender
{
viewFileViewController *buttonTagview=[[viewFileViewController alloc] initWithNibName:nil bundle:nil];
buttonTagview.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:buttonTagview animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"button tag value in another view: %d", btnTag);
if(btnTag==1)
{
[self.pdfview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ObjC" ofType:@"pdf"]]]];
}
else if(btnTag==2)
{
[self.pdfview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"imp" ofType:@"doc"]]]];
}
}


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil buttontag:(NSInteger)bTag{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
btnTag=bTag;
    }
    return self;
}

Remember "ObjC.pdf" and "imp.doc"should be in resource folder.


Explanation of code:
When the Back button will be pressed, -(IBAction)backButtonPressed:(id) sender method will be called and viewFileViewController will appear.

 -(void)viewWillAppear:(BOOL)animated
 { is the method where, it is defined that when the view will appear what appearance should be there.


In our case, when viewFileOnButtonClick will invoke its appear will be decide on the basis of button's tag value. If the tag value is 1 then pdf file will load and if the tag value is 2 then doc file will load.

Tag value is passed through - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil buttontag:(NSInteger)bTag{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
btnTag=bTag;}
    return self;
}


Step9: Open "viewFileViewController.xib"  and make connections of UIControlles(here, PDF & DOC buttons) with IBActions of code. Like this:
 







































Finally right click on File's Owner, the connections should be like this :


Step10:  Open "viewFileOnButtonClick.xib" file. Now, Give Iboutlet connection to it(here select pdfview) & Delegate it to FilesOwner.
It will look like this:



























Finally right click on File's Owner, the connections should be like this :














Close all IB files.

Now its time to view your file in iPhone...
Just run this app...and press PDF button and see your file....:)



CLICK HERE to download source code.

Happy Coding :)