以下是群里有个兄弟回答的,我就顺便先把记下来,说不定日后会有用:
// 短信分享
- (void) shareBySms:(id)viewController withContent:(NSString*)content
{
//短信推荐
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil)
{
if ([messageClass canSendText])
{
[self displaySMSComposerSheet:viewController withContent: content];
}
else
{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"华商圈" message:@"您的设备不支持短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] autorelease];
[alertView show];
}
}
else
{
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"华商圈" message:@"您的设备不支持短信功能!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] autorelease];
[alertView show];
}
}
-(void)displaySMSComposerSheet:(id)viewController withContent:(NSString*)content
{
// 此处能autorelease
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if (controller)
{
controller.body = content;
controller.messageComposeDelegate = self;
[(UIViewController*)viewController presentModalViewController:controller animated:YES];
}
}
//短信发送 回调的协议方法
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(@"Result: SMS sending canceled");
if(_shareDelegate && [_shareDelegate respondsToSelector:@selector(onShareFault:)])
{
[_shareDelegate onShareFault:@"取消发送短信"];
}
break;
case MessageComposeResultSent:
NSLog(@"Result: SMS sent");
if(_shareDelegate && [_shareDelegate respondsToSelector:@selector(onShareSuccess:)])
{
[_shareDelegate onShareSuccess:@"短信已发送成功"];
}
break;
case MessageComposeResultFailed:
if(_shareDelegate && [_shareDelegate respondsToSelector:@selector(onShareFault:)])
{
[_shareDelegate onShareFault:@"短信发送失败"];
}
break;
default:
if(_shareDelegate && [_shareDelegate respondsToSelector:@selector(onShareFault:)])
{
[_shareDelegate onShareFault:@"短信发送失败"];
}
break;
}
[controller dismissModalViewControllerAnimated:YES];
}