例:代码如下:
#import"ViewController.h"
#define DD_WIDTH self.view.bounds.size.width
#define DD_HEIGHT45.f
@implementation ViewController {
UITextField *telTextField;
}
- (void)viewDidLoad {
[superviewDidLoad];
telTextField = [UITextField new];
[telTextField setFrame:CGRectMake(0.f, 80.f, DD_WIDTH, DD_HEIGHT)];
[telTextField setPlaceholder:@"请输入要拨打的号码..."];
[telTextField setTextColor:[UIColorblackColor]];
[telTextField setBackgroundColor:[UIColorgrayColor]];
[telTextField setKeyboardType:UIKeyboardTypeNumberPad];
[self.viewaddSubview:telTextField];
UIButton *callButton = [UIButton new];
[callButton setFrame:CGRectMake(0.f, 145.f, DD_WIDTH, DD_HEIGHT)];
[callButton setTitle:@"拨打电话"forState:UIControlStateNormal];
[callButton setBackgroundColor:[UIColorgrayColor]];
[callButton addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:callButton];
}
-------------------------------------** 点击时有下列三种方法可选 ** --------------------------------------------------------------
// 1. 会弹出提示,推荐这种
- (void)buttonClick:(UIButton *)click {
NSString *number = telTextField.text;
NSMutableString *mutableString = [[NSMutableStringalloc]initWithFormat:@"tel:%@",number];
UIWebView *webview = [[UIWebViewalloc]init];
[webview loadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:mutableString]]];
[self.view addSubview:webview];
}
// 2. 直接拨打,不弹出提示
//- (void)buttonClick:(UIButton *)click {
// NSString *number = telTextField.text;
// NSMutableString *mutableString = [[NSMutableString alloc] initWithFormat:@"tel:%@",number];
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mutableString]];
//}
// 3. telprompt,也会弹出提示
//- (void)buttonClick:(UIButton *)click {
// NSString *number = telTextField.text;
// NSMutableString *mutableString = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",number];
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mutableString]];
//}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
@end