In this post, I will create a sample code which will help to add multiple functions.
Output of the application is as follows.
It will include following feature.
- Show Map
- Show web-site
- Send email
- Make a phone call
Here, I have listed the functions/methods being used in the application.
Show Map
|
1 2 3 4 5 |
-(void)btnMapTapped { NSString *str=[NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",self.lblAddress]; str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; } |
Show web-site
|
1 2 3 |
-(void)btnWebTapped { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]]; } |
Make a phone call
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#define callMsg @"Do you want to make call to " -(void) btnPhoneTapped { NSString *str=lblPhone.text; str=[str stringByReplacingOccurrencesOfString:@"Tele : " withString:@""]; if([str length]>0) { UIAlertView *av=[[UIAlertView alloc] initWithTitle:@"Message" message:[NSString stringWithFormat:@"%@%@",callMsg,str] delegate:self cancelButtonTitle:@"YES" otherButtonTitles:@"NO", nil]; [av show]; } } - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if(buttonIndex==0 && [[alertView message] hasPrefix:callMsg]) { NSString *str=[alertView message]; str=[str stringByReplacingOccurrencesOfString:callMsg withString:@""]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",str]]]; } } |
Send email
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
-(void)btnEmailTapped { NSString *str=self.lblEmail.text; str=[str stringByReplacingOccurrencesOfString:@"Email : " withString:@""]; if([str length]>0) { Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (mailClass != nil) { if ([mailClass canSendMail]){ MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"An email from SugarTin.info - Sample App"]; [picker setToRecipients:[NSArray arrayWithObject:str]]; NSString *emailBody = @"An email using SugarTin.info - Sample app."; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; } else { [self launchMailAppOnDevice]; } } else { [self launchMailAppOnDevice]; } } } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { [controller dismissModalViewControllerAnimated:YES]; } -(void)launchMailAppOnDevice { UIAlertView *avx=[[UIAlertView alloc] initWithTitle:@"Message" message:@"Mail composer not available." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [avx show]; } |

thanks for sharing this. I’ve been asking those before.. thanks again and keep sharing!