在很多APP里面有拨号功能,比如在美团外卖上面可以点击商家电话后给商家打电话,今天要做的就是这个功能.这个功能其实非常非常简单,但需要真机运行才可以实现,虚拟机不行.看看代码你们就知道多简单了.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 175, 50);
[button setTitle:@"打电话" forState:UIControlStateNormal];
button.backgroundColor = [UIColor cyanColor];
[button addTarget:self action:@selector(call) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(100, 200, 175, 50);
[button1 setTitle:@"发短信" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor cyanColor];
[button1 addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
-(void)call
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
}
-(void)sendMessage
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10010"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
是不是非常简单?
当你按下打电话button时,手机就会给10086打电话,当你按下发短信button时,手机就会跳转到短信编辑界面,而发送人就是10010.
希望这个能对你们有用.祝大家开心😄
网友评论