Monday, September 10, 2012

iPhone Application Icon example

iPhone Application Icon

The example will illustrate how to add an icon on the iPhone application in Xcode. Before adding the application icon in iPhone make sure that the icon size is 57x57 and it should be a png one.
To get an application icon in iPhone simulator.. add the "png" icon that you want to be displayed as your application icon under resources folder.
Then open up the "info.plist" file that resides under resources folder, there you will find a key Icon file. In its value specify the name of your "png"(app icon name). It would then be visible on you simulator.


Download Code

MapView Example in iPhone example

MapView Example in iPhone

The example illustrate how to embed or display a MapView in iPhone SDK UIView based application.
To embed a Map in your iphone application just follow the given steps..
1. Create a new project (ViewBased application )
2. Import the MapKit in header file...
#import <MapKit/MapKit.h>
also add the MapKit.framework under resources folder from the existing frameworks.
3. In the same viewController's header file call the <MKMapViewDelegate> and define the MKMapView *myMapView; synthesize and release the myMapView object into implementation file.
4. In a viewcontroller.xib file ..add the map view on UIView and connect it with corresponding object in file owner. Further see the code given below….
Header
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface myMapViewController : UIViewController <MKMapViewDelegate> {

IBOutlet MKMapView* myMapView;
}
@property (nonatomic, retain) IBOutlet MKMapView* myMapView;
-(void)displayMYMap;
@end
Implementation
#import "myMapViewController.h"
@implementation myMapViewController
@synthesize myMapView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
myMapView.delegate=self;

[self.view addSubview:myMapView];
[NSThread detachNewThreadSelector:@selector(displayMYMap) toTarget:self withObject:nil];

}

-(void)displayMYMap
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;

CLLocationCoordinate2D location;

location.latitude = 22.569722 ;
location.longitude = 88.369722;


region.span=span;
region.center=location;

[myMapView setRegion:region animated:TRUE];
[myMapView regionThatFits:region];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
[super dealloc];
[myMapView release];
}
@end
Build the application to display the map view on UIView. It should look like the given image.

Download code of MapView Example in iPhone

iPhone MapView Current Location in iphone example

iPhone MapView Current Location

This is a simple MKMapView example in iPhone for the beginners. In the example we'll discuss about how to display a map on UIView. You can also display the map in different styles by assigning different mapTypes such as Standard, Satellite and Hybrid.
And apart from it, we can also show the current location on the map. By default on simulator it will show the US as current location but on running the application in iPhone it will give you the correct location.
To get the MKMapView in our application we'll have to include the MapKit.framework in frameworks folder and also required to import the mapkit in header file as given below...
#import <MapKit/MapKit.h>
In the same .h file create the IBOutlet and property for the MKMapView as given below..
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface mapviewViewController : UIViewController {
IBOutlet MKMapView *mapview;
}
@property(nonatomic, retain) IBOutlet MKMapView *mapview;
@end
and in the .m, viewDidLoad method just add the given line of codes..
- (void)viewDidLoad {
[super viewDidLoad];
//mapview.mapType = MKMapTypeSatellite;
//mapview.mapType=MKMapTypeStandard;

//mapType
mapview.mapType=MKMapTypeHybrid;
//will show the current location
mapview.showsUserLocation = YES;

}
do not forget to add the MapView on UIView in interface builder. Also link it to the file owner otherwise it'll not show anything.
On building the application your application should look alike the given image.

Download Code

scheduledTimerWithTimeInterval Example in iphone example

scheduledTimerWithTimeInterval Example

scheduledTimerWithTimeInterval is a one of the class method of the NSTimer class, which is used to create timers. In general, we can say that NSTimer creates a object that sends a message to another object telling it to when to fire a timer or update it in a certain time intervals. And "scheduledTimerWithTimeInterval" schedules the timer into RunLoops that has two different methods to write it...
scheduledTimerWithTimeInterval:invocation:repeats: and
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
that basically creates the timer and schedules it on the current run loop in default mode. We will see this through a example...
In the example, first of all we'll create a timer, will pass the NSTimer object to our method and then we can stop the timer.
Creating a timer with the scheduledTimerWithTimeInterval
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(moveRect) userInfo:nil repeats:YES];
"scheduledTimerWithTimeInterval:.1" setting the duration of time for the timer, and if you set "repeats:YES" timer will repeat calling the selector every time in the given duration.
"moveRect" is the name of method that is called by @selector.. and to stop the timer we can simply write
[timer invalidate];
Example Code:
#import <UIKit/UIKit.h>
@interface timerViewController : UIViewController {
NSTimer* timer;
}
@end
---------------
#import "timerViewController.h"-
@implementation timerViewController
- (void)dealloc {
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];

CGRect RectFrame;
RectFrame.origin.x = 25;
RectFrame.origin.y = 300;
RectFrame.size.width = 20;
RectFrame.size.height = 20;

for(int i = 0; i < 10; i++)
{
UIView *myView = [[UIView alloc] initWithFrame:RectFrame];
[myView setTag:i];
[myView setBackgroundColor:[UIColor orangeColor]];

RectFrame.origin.x = RectFrame.origin.x + RectFrame.size.width + 10;
[self.view addSubview:myView];
}

timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(moveRect) userInfo:nil repeats:YES];
}
-(void)moveRect
{
int r = rand() % 10;

for(UIView *aView in [self.view subviews])
{
if([aView tag] == r)
{
int movement = rand() % 100;
CGRect RectFrame = aView.frame;
RectFrame.origin.y = RectFrame.origin.y - movement;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.2];
[aView setFrame:RectFrame];
[UIView commitAnimations];

if(RectFrame.origin.y < 0)
{
[timer invalidate];
}
}
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
On running the application... it should look like the given image

Download Code

UITableView Background Color in iphone example

UITableView Background Color

To change the background color of UITableView in iPhone application, you just need to write the given line of code into the tableView cellForRowAtIndexPath.
tableView.backgroundColor = [UIColor grayColor];
The above line will change the default background color into gray color. You can also set the gradient to it.
In this example we are simply changing the color of all the cells or table view background. But you can also draw different styles to it.. table view also provides properties that allows you to add image at background.
On building the application it should look like the given image..



In the image two you can see that on click, cell is showing different color or highlighted. This can be done by setting the cell color on click.
//To Set Selected Background Color
UIImageView *selectedBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320,100)];
selectedBackground.backgroundColor = [UIColor orangeColor];
[cell setSelectedBackgroundView:selectedBackground];
Download Code

iPhone Change Button Text in iphone example

iPhone Change Button Text

The example illustrate how to change the text of UIButton on click. Basically UIButton is a object that is handled by UIControl class, which defines the behavior of all the objects such as UIButton, UILabel, sliders etc..

UIControl has number of states ...that can be recognized depending on the control.
enum {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0,
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2,
UIControlStateApplication = 0x00FF0000,
UIControlStateReserved = 0xFF000000
};
In our example we are going to change the text on UIButton on click and selection. To get the desired output we are using two different states of UIControl class...
1. UIControlStateNormal
2. UIControlStateHighlighted
iPhone SDK App - Change UIButton Text on Different States

For Normal State
[button setTitle:@"Normal" forState:UIControlStateNormal];
On selection
[button setTitle:@"Selected" forState:UIControlStateHighlighted];
In the above line of code "setTitle" is setting the title for button in different states... and "forState" will set the desired state.
Change UIButton Text Color: a developer can also change the text color on different states with the help of given code...
[changeText setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
Download Code

UIWebView Background Color in iphone example

UIWebView Background Color

If you wanted to change the background color on UIWebView in iPhone SDK, then you required to set the background color of UIWebView using UIColor. UIColor object represents color and opacity (alpha value). Where as setBackgroundColor will allow you to change the color of background area of UIWebView or UIView.
This example illustrate you how to change the color of UIWebView in iPhone SDK based application. To change the color of background first we need to set the backgroundcolor as clearColor and then set the required color to it as given in the following code.
- (void)viewDidLoad {
[super viewDidLoad];

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]];

//Set the UIWebView background color and opaque:NO
[webView setBackgroundColor:[UIColor clearColor]];
[webView setBackgroundColor:[UIColor purpleColor]];
[webView setOpaque:NO];

}
On running the application you'll find the output similar to give image.

Download Code