之前在 iOS7 上,我测试了我的 SMS 模块,它运行良好。更新iOS版本后,发现短信模块出现了一些问题。
在我的 .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");
}
}
按发送后,日志中显示“消息已发送”,但 View 仍停留在消息屏幕上。我不知道为什么它不会返回到我的应用程序。
需要帮助找出为什么它不会返回到我的应用程序的问题。
提前致谢。
最佳答案
看来您并没有在 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
}
此外,- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated 自 iOS 6 起已弃用。使用 - (void)presentViewController:(UIViewController *)viewControllerToPresent动画:(BOOL)flag completion:(void (^)(void))completion 而不是像这样:
[self presentViewController:mailComposer animated:YES completion:nil];
关于ios - MFMessageComposeViewController 在发送消息 ios9 后不返回应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32687195/