Namaste IPhone
Tuesday, 8 August 2017
Saturday, 13 April 2013
#P6 Location and Map Application
Description
Develop
Map application to display map location based on the specified location.
Project Creation
- Create new Xcode project and select “Single View project” template under iOS->Application
- Add CoreLocation and MapKit Frameworks, which are necessary to handle Core location and Map functionalities.
- Open ViewController.xib file and add MapView UI object from the object library. Connect mapView object with the File Owner. The mapView is defined in the ViewController.h interface
Do method implementation
1) ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
IBOutlet MKMapView *mapView;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@end
2) ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize locationManager, mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled]) {
locationManager.delegate = self;
[locationManager startUpdatingLocation];
} else {
NSLog(@"Location Service
exception...");
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"latitude=%f,
longitude=%f",
[newLocation
coordinate].latitude,
[newLocation
coordinate].longitude);
MKCoordinateRegion region = MKCoordinateRegionMake([newLocation coordinate], MKCoordinateSpanMake(.2, .2));
[mapView setCenterCoordinate:[newLocation coordinate]];
[mapView setRegion:region];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"Error");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources
that can be recreated.
}
- (void)dealloc {
[locationManager release];
[super dealloc];
}
@end
Testing
#P5 Music Player Application
Description
Develop
Sitar music player application which can run on iOS platform. The application
will run sitar music when user click on the sitar image.
Screens
Project Creation
- Create new Xcode project and select “Single View project” template under iOS->Application
- Add AVFoundation Framework
- Open ViewController.xib file by double clicking it and add button and set its method name and image
Do method implementation
ViewController.h
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
-(IBAction)playToggele;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
Boolean playing = NO;
@implementation ViewController
@synthesize player;
-(void)playToggele{
if (playing==NO) {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"sitar" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player prepareToPlay];
player.delegate=self;
[player play];
playing=YES;
} else if(playing==YES){
[player pause];
playing=NO;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup
after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources
that can be recreated.
}
- (void)dealloc {
[player release];
[super dealloc];
}
@end
#P4 Touch Me Application
Description
Develop
Touch based application which can run on iOS platform. The application will
display different images based on the touch event.
For
every touch to iOS device screen will generate UIEvent object, this
object contains UITouch objects with
details of associated fingers.
Screens
Project Creation
Create new
Xcode project and select “Single View project” template under
iOS->Application
Do method implementation : ViewController.m
(No changes to ViewController.h)
(No changes to ViewController.h)
#import "ViewController.h"
@implementation ViewController
//Below touch Merhods are pre-defined in UIResponder
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Called
TouchesBegan");
UIImage *image = [UIImage imageNamed:@"cheetah.png"];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage
setImage:image];
myImage.opaque = YES; // explicitly opaque for
performance
[self.view addSubview:myImage];
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
myImage.center = touchLocation;
[myImage
release];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Called
TouchesMoved");
UIImage *image = [UIImage imageNamed:@"lion.png"];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage
setImage:image];
myImage.opaque = YES; // explicitly opaque for
performance
[self.view addSubview:myImage];
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
myImage.center = touchLocation;
[myImage
release];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touches Ended");
UIImage *image = [UIImage imageNamed:@"dog.png"];
CGRect myImageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage
setImage:image];
myImage.opaque = YES; // explicitly opaque for
performance
[self.view addSubview:myImage];
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
myImage.center = touchLocation;
[myImage
release];
}
//ORGINAL METHODS
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup
after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources
that can be recreated.
}
@end
Subscribe to:
Comments (Atom)










