发送短信/邮件/打电话:

发送短信/邮件/打电话:

+ (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代码库中浏览本帖)



CocoaChina2013(春季)开发者大会报名开启!


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]; 

} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值