MFMessageComposeViewController issues in iOS8

2019-04-13 15:33发布

问题:

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