.h文件中
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController <MFMessageComposeViewControllerDelegate>
- (IBAction) sendMessagePressed:(id)sender;
@end
- (IBAction)sendMessagePressed:(id)sender {
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
else {
NSLog(@"设备没有短信功能");
}
}
else {
NSLog(@"iOS版本过低,iOS4.0以上才支持程序内发送短信");
}
}
-(void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
NSMutableString* absUrl = [[NSMutableString alloc] initWithString:@"http://www.baidu.com"];
[absUrl replaceOccurrencesOfString:@"http://i.aizheke.com" withString:@"http://m.aizheke.com"
options:NSCaseInsensitiveSearch range:NSMakeRange(0, [absUrl length])];
picker.body=[NSString stringWithFormat:@"我在爱折客上看到:%@ 可能对你有用,推荐给你!link:%@"
,@"http://www.baidu.com", @"http://www.baidu.com"];
picker.recipients = [NSArray arrayWithObjects:@"18079177441", nil];
//[self presentModalViewController:picker animated:YES];
[self presentViewController:picker animated:YES completion:nil];
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
switch (result)
{
case MessageComposeResultCancelled:
NSLog(@"Result: SMS sending canceled");
break;
case MessageComposeResultSent:
NSLog(@"Result: SMS sent");
break;
case MessageComposeResultFailed:
NSLog(@"短信发送失败");
break;
default:
NSLog(@"Result: SMS not sent");
break;
}
//[self dismissModalViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}