【代码笔记】短信分享

代码:

ViewController.h

复制代码
#import <UIKit/UIKit.h>
//短信分享
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MFMessageComposeViewController.h>

@interface ViewController : UIViewController<MFMessageComposeViewControllerDelegate>


@end
复制代码

 

ViewController.m

复制代码
#import "ViewController.h"


@interface ViewController ()

@end


@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//点击任何处,短信分享
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  
    //调用系统发送信息功能
    BOOL canSendSMS = [MFMessageComposeViewController canSendText];
    if (canSendSMS) {
        
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;
        
        NSArray *array = [[NSArray alloc] initWithObjects:nil];
        picker.recipients=array;
        picker.view.frame=CGRectMake(0, 0, 320, 640);
        [self presentViewController:picker animated:YES completion:nil];
        
    }else{
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@""
                                                      message:@"当前设备不支持短信功能"
                                                     delegate:nil
                                            cancelButtonTitle:@"确定"
                                            otherButtonTitles:nil];
        [alert show];
    }

}
#pragma -mark -系统消息回调
//系统发送信息的回调函数
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    //[self dismissModalViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
复制代码

 

 

 
 

你可能感兴趣的:(【代码笔记】短信分享)