Recently, IOS's email sharing and SMS text message sharing functions are simple functions. However, I found that the opened email interface and SMS interface never responded to the buttons on navigationcontroller, I felt very strange. Finally, I found that I needed to close the opened view in the delegate implementation method of these interfaces. It was a bit awkward! I am sending a piece of code, and you may understand it!
Note: To call the function of sending text messages and emails, you must introduce a library: messageui. framework.
You also need to import some classes:
#import <MessageUI/MessageUI.h>#import <MessageUI/MFMailComposeViewController.h>
The following code is the text message sent by IOs:
-(void)sendSMSComposerSheet{ MFMessageComposeViewController *messageCtrl = [[MFMessageComposeViewController alloc] init]; messageCtrl.messageComposeDelegate = self; messageCtrl.body = @"share ...."; [self presentModalViewController:messageCtrl animated:YES];}
The delegation of this Code is mfmessagecomposeviewcontrollerdelegate.
However, my code also implements delegation. The Code is as follows:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ }
However, it is okay to open the text message interface. The key is that the cancel button on the text message interface is not matched. Finally, I thought about it. It is estimated that the open view should be closed in the delegate implementation method, the result is true!
The above code is modified:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ [controller dismissModalViewControllerAnimated:YES];}
Or:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ [controller dismissViewControllerAnimated:YES completion:^{}];}
Both methods are available, so that IOS can use presentmodalviewcontroller to open viewcontroller and corresponding buttons on viewcongroller!
At the same time, the Code for sending emails to iOS is appended:
-(Void) sendmailcomposersheet {mfmailcomposeviewcontroller * mailctrl = [[mfmailcomposeviewcontroller alloc] init]; mailctrl. mailcomposedelegate = self; [mailctrl setsubject: @ "mail title"]; [mailctrl setmessagebody: @ "Mail content" ishtml: No]; [self presentmodalviewcontroller: mailctrl animated: Yes];}
Related delegation: mfmailcomposeviewcontrollerdelegate
Interface Processing
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ [controller dismissModalViewControllerAnimated:YES];}