以前在iOS7上,我测试过我的短信模块,效果很好。更新iOS版本后,我注意到SMS模块存在一些问题。
在我的.h文件中
#import <MessageUI/MFMessageComposeViewController.h>
@interface ViewController : UIViewController<UITextFieldDelegate,MFMessageComposeViewControllerDelegate>
在我的.m文件中
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
if (result == MessageComposeResultCancelled){
NSLog(@"Message cancelled");
}
else if (result == MessageComposeResultSent){
NSLog(@"Message sent");
}
else{
NSLog(@"Message failed");
}
}
按下发送后,在日志中显示“已发送消息”,但视图仍在消息屏幕上。我不知道为什么它不会回到我的应用程序。
需要帮助才能找到问题,说明为什么它不会返回我的应用程序。
提前致谢。
答案 0 :(得分:1)
在呈现之后,您似乎并未解雇mailcomposer
。您必须在以下方法中解除显示的MFMessageComposeViewController
:
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if (result == MessageComposeResultCancelled){
NSLog(@"Message cancelled");
}
else if (result == MessageComposeResultSent){
NSLog(@"Message sent");
}
else{
NSLog(@"Message failed");
}
[self dismissViewControllerAnimated:YES completion:nil]; //<---- This line
}
此外,自iOS 6以来,- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
已弃用。请使用- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
代替:
[self presentViewController:mailComposer animated:YES completion:nil];