iPhone 应用中实现拨打电话功能的代码

本文介绍了一个简单的iOS应用程序功能,可以在应用内添加一个电话按钮,点击该按钮即可直接拨打电话号码。通过设置按钮样式并绑定拨号操作,实现了便捷的拨号功能。

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

下面的代码能在应用中添加一个电话按钮,点击即可拨打电话号码。对于 iPhone 开发者还是很有用的。

//添加电话图标按钮
    UIButton *btnPhone = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    btnPhone.frame = CGRectMake(280,10,30,30);
    UIImage *image = [UIImage imageNamed:@"phone.png"];    
    [btnPhone setBackgroundImage:image forState:UIControlStateNormal];
    
    //点击拨号按钮直接拨号
    [btnPhone addTarget:self action:@selector(callAction:event:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:btnPhone];  //cell是一个UITableViewCell

//定义点击拨号按钮时的操作
- (void)callAction:(id)sender event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.listTable];
    NSIndexPath *indexPath = [self.listTable indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath == nil) {
        return;
    }
    NSInteger  section = [indexPath section];
    NSUInteger  row = [indexPath row];
    NSDictionary *rowData = [datas objectAtIndex:row];

    NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number]; //number为号码字符串    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值