iOS开发 调用系统发短信 打电话 功能

本文详细介绍了如何在iOS应用中集成短信和电话功能,包括使用MFMessageComposeViewController实现短信发送,以及通过UIWebView加载脚本实现拨打电话。文章提供了完整的代码示例,涵盖从初始化视图控制器到展示短信界面、处理发送结果的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.调用系统发短信  功能  并返回到 应用程序里 


//点击 smsButton(按钮)

-(void)smsButton:(UIButton*)sender

{

    

    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

    if (messageClass != nil) {

        // Check whether the current device is configured for sending SMS messages

        if ([messageClass canSendText]) {

            [self displaySMSComposerSheet];

        }

        else {


            UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"设备没有发短信 功能" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

            [alert show];

        }

    }

    else {

        UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"iOS版本过低,iOS4.0以上才支持程序内发送短信" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alert show];


    }




}



// 显示 短信 界面 

-(void)displaySMSComposerSheet

{

    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

    picker.messageComposeDelegate = self;

    

    NSArray *titleList = @[@"购置全款:",@"首付:",@"其他首付:", @"尾款:",@"期数:",@"月租金:"];

    NSString *cellstrTi = [NSString stringWithFormat:@"%@ %@",[titleList objectAtIndex:0],[self.smsList objectAtIndex:0]];

    NSString *cellstr1 = [NSString stringWithFormat:@"%@ %@",[titleList objectAtIndex:1],[self.cellList objectAtIndex:0]] ;


    NSString *cellstr2 = [NSString stringWithFormat:@"%@ %@",[titleList objectAtIndex:2],[self.cellList objectAtIndex:1]] ;

    NSString *cellstr3 = [NSString stringWithFormat:@"%@ %@",[titleList objectAtIndex:3],[self.cellList objectAtIndex:2]] ;

    NSString *cellstr4= [NSString stringWithFormat:@"%@ %@",[titleList objectAtIndex:4],[self.smsList objectAtIndex:1]] ;


    NSString *cellstrlast = [NSString stringWithFormat:@"%@ %@",[titleList objectAtIndex: 5],[self.cellList objectAtIndex:3]];


    picker.body=[NSString stringWithFormat:@" %@\n %@\n %@\n %@\n %@\n %@",cellstrTi, cellstr1,cellstr2,cellstr3,cellstr4,cellstrlast];

    NSLog(@"%@",picker.body);

    

    [self presentViewController:picker animated:YES completion:nil];

}



// 处理发送完的响应结果

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{

    [self dismissViewControllerAnimated:YES completion:nil];

    if (result == MessageComposeResultCancelled)

        NSLog(@"Message cancelled");

    else if (result == MessageComposeResultSent){

            NSLog(@"Message sent");

    

    UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"短信发送成功!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];

    }

    else

                NSLog(@"Message failed");

    

}




2,调用系统 打电话功能 并返回到 应用程序里


-(void)onPhoneBtn:(UIButton*)sender

{

    NSLog(@"%ld",(long)sender.tag);

    NSString *str = [[self.cellList objectAtIndex:sender.tag]objectForKey:@"MOBILE"];

    NSLog(@"%@",str);

    NSString *callNum = [NSString stringWithFormat:@"tel://%@",str];

    UIWebView *callPhoneWebVw = [[UIWebView alloc] init];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:callNum]];

    [callPhoneWebVw loadRequest:request];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值