iOS拨打电话

iOS 拨打电话常用2中方法,以下是代码

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong)UITextField *phoneNumTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UITextField *phoneNumTextField = [[UITextField alloc]init];
    phoneNumTextField.center = self.view.center;
    phoneNumTextField.bounds = CGRectMake(10, 0, self.view.bounds.size.width-10, 44);
    phoneNumTextField.layer.borderColor = [UIColor grayColor].CGColor;
    phoneNumTextField.placeholder = @"输入电话号码";
    phoneNumTextField.layer.borderWidth = 1.0f;
    self.phoneNumTextField = phoneNumTextField;
    [self.view addSubview:phoneNumTextField];
    
    UIButton *callButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [callButton setTitle:@"call" forState:UIControlStateNormal];
    [callButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    callButton.backgroundColor = [UIColor grayColor];
    callButton.center = CGPointMake(self.view.center.x, self.view.center.y+60);
    callButton.bounds = phoneNumTextField.bounds;
    [self.view addSubview:callButton];
    [callButton addTarget:self action:@selector(callButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)callButtonEvent:(UIButton *)sender {
    // 1.第一种:推荐
    NSMutableString *phoneNum = [[NSMutableString alloc] initWithFormat:@"tel:%@",self.phoneNumTextField.text];
    UIWebView *webView = [[UIWebView alloc]init];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:phoneNum]]];
    [self.view addSubview:webView];
    // 2.第二种:
//    NSString  *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.phoneNumTextField.text];
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
    
}

你可能感兴趣的:(iOS拨打电话)