Making calls from iPhone App

Making calls from iPhone App

This application allows to make call within app , after call completion User can get back to the running application.


// label for "Contact Number"
UILabel *headerLbl = [[UILabel alloc]initWithFrame:CGRectMake(20,105,100,40)];
headerLbl.text = @"Contact # :";
headerLbl.textAlignment = UITextAlignmentCenter;
headerLbl.backgroundColor = [UIColor clearColor];
[self.view addSubview:headerLbl];
[headerLbl release];

compPhoneHead = [[UIButton alloc]initWithFrame:CGRectMake(133,105,146,39)];
UIColor *background_Color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"phoneNoButton.png"]];
compPhoneHead.backgroundColor = background_Color;
[compPhoneHead addTarget:self action:@selector(callAlert) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:compPhoneHead];

compPhone = [[UILabel alloc]initWithFrame:CGRectMake(120,115,193,20)];
compPhone.backgroundColor = [UIColor clearColor];
compPhone.font = [UIFont fontWithName:@"Times New Roman" size:15];
compPhone.text = @"(012)345-6789";
compPhone.textColor = [UIColor blackColor];
compPhone.textAlignment = UITextAlignmentCenter;
[self.view addSubview:compPhone];


Phone Number on screen


Following code is used to implement the Calling functionality.

-(void)callAlert{

alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Are you sure to make Call" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}
// alert view delegate
- (void) alertView:(UIAlertView *)alertV clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0 && alertV==alert){
[self makeCall];
}
}
// method to make call if “OK” button is clicked on AlertView
-(void)makeCall{

NSLog(@"this is calling phone");
NSString *phoneNumber = [NSString stringWithFormat:@"tel:%@",compPhone.text];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@") " withString:@"-"];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
NSURL *phoneUrl = [[NSURL alloc] initWithString:phoneNumber];

Opening an app within another app is managed in iOS through the "url scheme" mechanism. If an app defines an url scheme and this scheme is public, then we can use it to run the app. Basic rule is to first check that the device supports that scheme like we cannot make a phone call on an iPad because the phone app is not supported on iPad). If the scheme is supported then we can call it. This concept is used in making call through app.


// Check If device is able to make a call or not

if([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
NSURLRequest *phoneRequest = [NSURLRequest requestWithURL:phoneUrl];
UIWebView *phoneWebView = [[UIWebView alloc]init];
[phoneWebView loadRequest:phoneRequest];
} else {

// do something else, e.g. inform the user that he/she cannot open the app
UIAlertView *alerts = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Unable to make Call" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alerts show];
[alerts release];
}
}

0 comments:

Post a Comment