发送短信/邮件/打电话:
+ (void)alert:(NSString *)msg
{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:msg message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
[alertView showWithBackground];
}
+ (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 *)phoneNumber
{
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]];
}
2.。。发短信
iphone发送短信内容 (在iOS代码库中浏览本帖)
IOS程序内发短信
iOS4.0新加入了MFMessageComposeViewController和MFMessageComposeViewControllerDelegate,
提供了发送短信的接口,可以像发送邮件那样不用跳出程序来发送短信. 介绍可参阅Message UIFramework Reference
一些笔记:
MFMessageComposeViewController
提供了操作界面
使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.
界面不能自行定制
要发送的短信的内容(body)和收件人(recipients)在展现这个controller前需初始化好,展现了之后短信内容不能通过程序来进行修改.不过用户仍然可以手工修改短信内容和选择收件人
用户点了发送或者取消,或者发送失败时,MFMessageComposeViewControllerDelegate 的– messageComposeViewController:didFinishWithResult:方法都能得到通知,在这里进行相应的处理
若在iOS3.0上运行的话,会提示dyld: Symbol not found: _OBJC_CLASS_$_MFMessageComposeViewController .解决方案:
MessageUI.framework的引入类型应选择weak(在target -> Get Info -> General -> Linked Libraries -> MessageUI.framework -> Type 里修改)
不要在.h文件里直接import MessageUI/MFMessageComposeViewController.h,改为import <MessageUI/MessageUI.h>
主要代码:
导入框架:MessageUI.framework
#import <MessageUI/MessageUI.h>
添加协议:<MFMessageComposeViewControllerDelegate>
#pragma mark -
#pragma mark MFMessageComposeViewController
- (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {
UIAlertView*alert = [[UIAlertViewalloc] initWithTitle:title
message:msg delegate:nil cancelButtonTitle:@"确定“ otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)displaySMS:(NSString *)message {
MFMessageComposeViewController*picker = [[MFMessageComposeViewControlleralloc] init];
picker.messageComposeDelegate= self;
picker.navigationBar.tintColor= [UIColorblackColor];
picker.body = message; // 默认信息内容
// 默认收件人(可多个)
//picker.recipients = [NSArray arrayWithObject:@"12345678901", nil];
[selfpresentModalViewController:picker animated:YES];
[picker release];
}
- (void)sendsms:(NSString *)message {
ClassmessageClass = (NSClassFromString(@"MFMessageComposeViewController"));
NSLog(@"can send SMS [%d]", [messageClass canSendText]);
if (messageClass != nil) {
if ([messageClass canSendText]) {
[self displaySMS:message];
} else {
[selfalertWithTitle:nilmsg:@"设备没有短信功能"];
}
} else {
[selfalertWithTitle:nilmsg:@"iOS版本过低,iOS4.0以上才支持程序内发送短信"];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
NSString*msg;
switch (result) {
case MessageComposeResultCancelled:
msg = @"发送取消";
break;
case MessageComposeResultSent:
msg = @"发送成功";
[self alertWithTitle:nil msg:msg];
break;
case MessageComposeResultFailed:
msg = @"发送失败";
[self alertWithTitle:nil msg:msg];
break;
default:
break;
}
NSLog(@"发送结果:%@", msg);
[selfdismissModalViewControllerAnimated:YES];
}