MFMessageComposeViewController issues in iOS8

徘徊边缘 提交于 2019-12-06 09:58:19

问题


I've run into a strange bug with MFMessageComposeViewController in iOS8, that is only reproducible within my own app. It's affecting the app right now in the App Store, built with the iOS7 SDK running on iOS8, as well with the iOS8 SDK in my own test devices (iPhone and iPad)

The issue is that when I bring up the MFMessageComposeViewController, it shows me the controller without a text field or a Send button anymore. I haven't changed my code between iOS7 and iOS8, so not sure why this is happening. The code itself is very simple:

MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
[picker setRecipients: @[@"5551112222"]];
[picker setBody: @"Test"];

[self presentViewController:picker animated:YES completion: ^{
    NSLog(@"MFMessageComposeViewController completion handler"); 
}];

This is what it looks like:

Any ideas for what I can try for a work-around? I've tried setting the textField and recipients in the completion handler; tried calling becomeFirstResponder on the top-most view controller; no luck. Again, this was/is working perfectly fine in iOS7.

EDIT:

So I found Apple's own sample code for MFMessageComposeViewController from this link: https://developer.apple.com/library/ios/samplecode/MessageComposer/Listings/MessageComposerViewController_m.html

When I build and run this app, the MFMessageComposeViewController shows up perfectly, and pre-filling the phone number and text fields works as well. But when I copy their files into my app, make their storyboard my main storyboard, press the "Compose SMS" button and I see the same exact problem!

What is going on here? Very confused. Could it be some configuration in my own app that is preventing the message composer from displaying correctly?


回答1:


Finally, after tearing down most of my app, I was able to figure out the issue. Turns out, I was overriding a UIViewController system method in a category (instead of sub-classing):

@implementation UIViewController (UIViewController_Additions)
-(BOOL)canBecomeFirstResponder {
    return YES;
}

This has been working fine till iOS7, but something must have changed internally in iOS8 and `MFMessageComposeViewController. A case of the "4-year-old-hack coming to bite you in the ass"

This practice is discouraged by Apple as well, according to this link (though I couldn't find the original Apple source): https://stackoverflow.com/a/14259595/145552



来源:https://stackoverflow.com/questions/25924574/mfmessagecomposeviewcontroller-issues-in-ios8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!