Archive for April, 2012

Display PDF files in iphone app


UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 300, 500)];

NSURL *targetURL = [NSURL URLWithString:@”YOUR PDF LINK GOES HERE“];

NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];

[webView loadRequest:request];

[self.view addSubview:webView]; [webView release];

Leave a comment

Cut,Copy and Paste in iphone application


 -(IBAction)cut { 
  [self copy]; 
  textPad.text = @""; 
 } 
 -(IBAction)copy {    
     NSString *copyString = [[NSString alloc] initWithFormat:@"%@",[textPad text]];    
     UIPasteboard *pb = [UIPasteboard generalPasteboard];  
     [pb setString:copyString];
 }
 -(IBAction)paste {    
   UIPasteboard *pb = [UIPasteboard generalPasteboard];    
     textPad.text = [pb string];
 } 

Leave a comment

Playing vedio from the url in iphone


NSURL*url = [NSURL URLWithString:@”UrlOfVideo”];
MPMoviePlayerController  *player = [[ MPMoviePlayerController alloc] initWithContentURL:url];
player.scalingMode = MPMovieScalingModeAspectFill;
[player play];

12 Comments

Playing Audio and Vedio in iphone



1. Code for .h file.

#import
#import
#import

@interface MultiMediaViewController : UIViewController {

MPMoviePlayerViewController *player;
AVAudioPlayer *audioPlayer;
}

– (IBAction)btnPlayVideo_Clicked:(id)sender;
– (IBAction)btnPlayAudio_Clicked:(id)sender;
– (IBAction)btnPlayBackgroundAudio_Clicked:(id)sender;
– (IBAction)btnStopAudio_Clicked:(id)sender;

@end

2. Code for .m file.

#import “MultiMediaViewController.h”

@implementation MultiMediaViewController

– (void)viewDidLoad {
[super viewDidLoad];

}

– (IBAction)btnPlayVideo_Clicked:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@”Movie” ofType:@”mp4″ inDirectory:nil];
NSURL *movieURL = [NSURL fileURLWithPath:path];
player= [[ MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:player];
}

– (void)mediaPlayerPlaybackDidFinish:(id)sender
{
[player.moviePlayer stop];
[self dismissModalViewControllerAnimated:YES];
}

– (IBAction)btnPlayAudio_Clicked:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@”Sound” ofType:@”mp3″ inDirectory:nil];
NSURL *audioURL = [NSURL fileURLWithPath:path];
player= [[ MPMoviePlayerViewController alloc] initWithContentURL:audioURL];
[self presentMoviePlayerViewControllerAnimated:player];
}

– (IBAction)btnPlayBackgroundAudio_Clicked:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@”Sound” ofType:@”mp3″ inDirectory:nil];
NSURL *audioURL = [NSURL fileURLWithPath:path];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
}

– (IBAction)btnStopAudio_Clicked:(id)sender
{
[audioPlayer stop];
}

– (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 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

– (void)dealloc {
[super dealloc];
}

@end

Leave a comment

Find UIID (Device Id) for iPhone Device or iPhone Simulator.


NSString *udid = [[UIDevice currentDevice] uniqueIdentifier];
udid = [udid stringByReplacingOccurrencesOfString:@”-” withString:@””];

2 Comments

countdown timer in iPhone app.



1. Code for .h file.

@interface UIMyContoller : UIViewController {

NSTimer *timer;
IBOutlet UILabel *myCounterLabel;
}

@property (nonatomic, retain) UILabel *myCounterLabel;
-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;

@end

2. Code for .m file.

@implementation UIMyController
@synthesize myCounterLabel;

int hours, minutes, seconds;
int secondsLeft;

– (void)viewDidLoad {
[super viewDidLoad];

secondsLeft = 16925;
[self countdownTimer];
}

– (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 ){
secondsLeft — ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
myCounterLabel.text = [NSString stringWithFormat:@”%02d:%02d:%02d”, hours, minutes, seconds];
}
else{
secondsLeft = 16925;
}
}

-(void)countdownTimer{

secondsLeft = hours = minutes = seconds = 0;
if([timer isValid])
{
[timer release];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[pool release];
}

2 Comments

Alert View with a table view inside


– (void)showalert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Preferences”
message:@”\n\n\n\n\n\n\n”
delegate:self
cancelButtonTitle:@”Cancel”
otherButtonTitles:@”OK”, nil];

UITableView *myView = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 150)
style:UITableViewStyleGrouped] autorelease];
myView.delegate = self;
myView.dataSource = self;
myView.backgroundColor = [UIColor clearColor];
[alert addSubview:myView];

[alert show];
[alert release];
}

Leave a comment

Expand cell of a Table View with animation…


– (void)viewDidLoad {
[super viewDidLoad];
cellAccessArray = [[NSMutableArray alloc] initWithObjects:@”closed”,@”closed”,@”closed”,@”closed”,nil];
}

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cellAccessArray count];
}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellValue = [cellAccessArray objectAtIndex:indexPath.row];
cell.text = cellValue;

return cell;
}

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if([[cellAccessArray objectAtIndex:indexPath.row] isEqualToString:@”closed”])
{
[cellAccessArray replaceObjectAtIndex:indexPath.row withObject:@”open”];
}
else
{
[cellAccessArray replaceObjectAtIndex:indexPath.row withObject:@”closed”];
}
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewScrollPositionNone];
}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[cellAccessArray objectAtIndex:indexPath.row] isEqualToString:@”open”])
{
return 140;
}
return 70;
}

1 Comment

Make a call with in the ios app and return back to the app when ended


UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:@”tel:xxxxxxxxxx”];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

4 Comments

Determine if string is numeric only


In Objective-C there is no in-built method which determines whether the given string is numeric only or is an alphanumeric, but has provided ways to do that.

The objective can be achieved using the NSCharacterSet class, which provides a set of characters.
Using below method we can identify the whether the string is numeric or alphanumeric.

-(void)testNumeric{
NSString *result=@”2011″;
if([self isNumeric:result]){
// Numeric
}else{
// Alphanumeric
}
}
-(void)isNumeric:(NSString*)inputString{
BOOL isValid=NO;
NSCharacterSet *alphaNumbersSet = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:inputString];
isValid = [alphaNumbersSet isSupersetOfSet:stringSet];
return isValid;
}

decimalDigitCharacterSet is a character set containing the characters in the category of Decimal Numbers,i.e, the set with all numeric characters.

Method characterSetWithCharactersInString: returns the character set containing the characters in the given string, i.e, the set with characters ‘2’,’0′,’1′,’1′ in our case.

Using the two sets, we determine whether all characters of inputString is contained in alphaNumbersSet.

2 Comments