iphone 发送短信/邮件/打电话的源代码
1、iphone发送短信
//MFMessageComposeViewController 只在ios 4.0 后可用
Class smsClass = NSClassFromString(@”MFMessageComposeViewController”);
if (smsClass != nil)
{
if ([smsClass canSendText])
{
[self displaySMSComposerSheet];
}
else {
[self launchSmsAppOnDevice];
}
}
else {
[self launchSmsAppOnDevice];
}
-(void)launchSmsAppOnDevice
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string =@”sms body”;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:// "]];
}
#pragma mark -
#pragma mark Componse sms
-(void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.body = “sms body”;
picker.messageComposeDelegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
}
2、
+ (NSString*) cleanPhoneNumber:(NSString*)phoneNumber
{
NSString* number = [NSString stringWithString:phoneNumber];
NSString* number1 = [[[number stringByReplacingOccurrencesOfString:@" " withString:@""]
// stringByReplacingOccurrencesOfString:@"-" withString:@""]
stringByReplacingOccurrencesOfString:@"(" withString:@""]
stringByReplacingOccurrencesOfString:@")" withString:@""];
return number1;
}
+ (void) makeCall:(NSString *)phoneNumber
{
if ([DeviceDetection isIPodTouch]){
[UIUtils alert:kCallNotSupportOnIPod];
return;
}
NSString* numberAfterClear = [UIUtils cleanPhoneNumber:phoneNumber];
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", numberAfterClear]];
NSLog(@"make call, URL=%@", phoneNumberURL);
[[UIApplication sharedApplication] openURL:phoneNumberURL];
}
+ (void) sendSms:(NSString *)phonNeumber
{
if ([DeviceDetection isIPodTouch]){
[UIUtils alert:kSmsNotSupportOnIPod];
return;
}
NSString* numberAfterClear = [UIUtils cleanPhoneNumber:phoneNumber];
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"sms:%@", numberAfterClear]];
NSLog(@"send sms, URL=%@", phoneNumberURL);
[[UIApplication sharedApplication] openURL:phoneNumberURL];
}
+ (void) sendEmail:(NSString *)phoneNumber
{
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", phoneNumber]];
NSLog(@"send sms, URL=%@", phoneNumberURL);
[[UIApplication sharedApplication] openURL:phoneNumberURL];
}
+ (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
{
NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
to, cc, subject, body];
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}