按钮复制与发短信

1.点击按钮复制

UIPasteboard *pab = [UIPasteboard generalPasteboard];

NSString *string = @"测试";

[pab setString:string];

if (pab == nil) {

[SVProgressHUD showErrorWithStatus:@"复制失败"];

[SVProgressHUD dismissWithDelay:.3];

}else

{

[SVProgressHUD showSuccessWithStatus:@"已复制"];

[SVProgressHUD dismissWithDelay:.3];

}

2.调用系统发送短信

实现此功能,一般使用程序内调用系统。首先将头文件引入

#import

实现代码

if( [MFMessageComposeViewController canSendText]) {

MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];

controller.recipients = @[@"10086"];//发送短信的号码,数组形式入参

controller.navigationBar.tintColor = [UIColor redColor];

controller.body = @"body"; //此处的body就是短信将要发生的内容

controller.messageComposeDelegate = self;

[self presentViewController:controller animated:YES completion:nil];

[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"title"];//修改短信界面标题

}

else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"

message:@"该设备不支持短信功能"

delegate:nil

cancelButtonTitle:@"确定"

otherButtonTitles:nil, nil];

[alert show];

}

如要获取发送状态,遵守代理MFMessageComposeViewControllerDelegate并实现代理方法

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

[self dismissViewControllerAnimated:YES completion:nil];

switch (result) {

case MessageComposeResultSent:

//信息传送成功

break;

case MessageComposeResultFailed:

//信息传送失败

break;

case MessageComposeResultCancelled:

//信息被用户取消传送

break;

default:

break;

}

}

如果想要发送短信之后留在短信页面直接用openUrl

实现代码

NSString *phoneStr = [NSString stringWithFormat:@"10086"];//发短信的号码

NSString *smsContentStr = [NSString stringWithFormat:@"短信内容"];

NSString *urlStr = [NSString stringWithFormat:@"sms://%@&body=%@ ", phoneStr, smsContentStr];

NSURL *url = [NSURL URLWithString:urlStr];

[[UIApplication sharedApplication] openURL:url];

你可能感兴趣的:(按钮复制与发短信)