IOS拨打电话功能的实现

来源:互联网 发布:javascript call是什么 编辑:程序博客网 时间:2024/04/29 05:08

IOS拨打电话功能的实现


简介:本事例是点击一个UItableviewcell中的拨打电话按钮,所以电话号码的值是从model中获取,其中_dataArray为接口获取cell内容的数组。

1、在添加cell的时候,给拨打电话按钮添加事件,并添加Tag值(方便获取数组下标,从而得到没个cell中内容对应的电话号码字段)

[cell.phoneButton addTarget:self action:@selector(callPhoneAction:) forControlEvents:UIControlEventTouchUpInside];

    cell.phoneButton.tag=500+indexPath.section;


2、在创建点击事件之前需要添加事先声明个电话号码NSstring的全局变量,并添加UIActionSheetDelegate代理方法,我们需要用到UIActionSheet的代理事件

//添加全局电话号码

    NSString *_mobileStr;

在接口协议中添加UIActionSheetDelegate

@interface MyHotalController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>


3、添加拨打电话按钮的事件

#pragma mark cell中的拨打电话按钮事件

-(void)callPhoneAction:(UIButton *)btn{

    

    MyHotalModel *model=_dataArray[btn.tag - 500];

    _mobileStr=model.mobile;

    

    UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle:@"是否拨打电话?" delegate:self  cancelButtonTitle:@"取消" destructiveButtonTitle:_mobileStr otherButtonTitles:nil];

    actionSheet.tag=300;

    actionSheet.actionSheetStyle=UIActionSheetStyleDefault;

    [actionSheet showInView:self.view];


}

4、实现UIActionSheetDelegate代理方法,点击电话号码按钮时,检测设备是否支付通话,不支持则提示,支持就拨打电话

#pragma mark - UIActionSheetDelegate代理方法

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex==0&&actionSheet.tag==300) {

        BOOL flag=[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_mobileStr]]];

        

        if(!flag){

            UIAlertView *callalert=[[UIAlertView alloc]initWithTitle:@"亲,你的设备不支持通话功能哦" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

            [callalert show];

        }

        else{

            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_mobileStr]]];

        }

    }

}


0 0
原创粉丝点击