我认为如果不创建自己的 UIActivity 子类 (YourActivity) 就不能直接访问邮件控制器。
在“YourActivity”中设置您的 MFMailComposeViewController,它将像在您的主代码中一样运行。这是我的做法:
在 YourActivity.h 中:
让自己成为邮件控制器的委托,并为邮件视图控制器和选定的视图控制器设置方法范围的变量:
@interface YourActivity : UIActivity <MFMailComposeViewControllerDelegate>
{
MFMailComposeViewController *mailController;
UIViewController *activityViewController;
}
在 YourActivity.m 中:
(可选)我建议您尽早检查邮件服务的可用性。 (这将防止向用户提供无法完成的选项):
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
// If mail is unavailable, can't perform activity
if (![MFMailComposeViewController canSendMail]) {
return NO;
}
for (id item in activityItems) {
// whatever other checks you want to do
return YES;
}
return NO;
}
在 YourActivity -prepareWithActivityItems: 方法中设置您的 MFMailComposeViewController:
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
// See if we can send mail (shouldn't happen if we checked already in -canPerformActivityWithItems)
if (![MFMailComposeViewController canSendMail]) {
UIAlertController *mailAlertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Mail Unavailable", @"mail unavailable")
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"cancel")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
[self activityDidFinish:NO];
}];
[mailAlertController addAction:cancel];
// Set the alert as the view to return
activityViewController = mailAlertController;
}
// Create a mail view controller
mailController = [[MFMailComposeViewController alloc] init];
// Set Delegate
[mailController setMailComposeDelegate:self];
// Set mail controller as the view to return
activityViewController = mailController;
// Paste the rest of your MFMailComposeViewController code here
}
在 YourActivity -activityViewController 方法中返回您选择的视图控制器:
- (UIViewController *)activityViewController
{
return activityViewController;
}
记得实现邮件完成处理程序,至少要关闭作曲家视图:
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Send any messages, if desired, to the controller before dismissing
NSString *message = nil;
NSString *errorMessage = nil;
if (result == MFMailComposeResultFailed) {
message = NSLocalizedString(@"Unable to send email", @"Unable to send email");
}
if (error) {
errorMessage = [message stringByAppendingString:[NSString stringWithFormat:NSLocalizedString(@"Error:\n%@", @"error:\n%@"), [error localizedDescription]]];
}
// Send mail status alert message, if needed
if (message) {
UIAlertController *mailAlert = [UIAlertController alertControllerWithTitle:message
message:errorMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
// Dismiss the mail controller
[controller dismissViewControllerAnimated:true completion:^{}];
[controller release];
mailController = nil;
}];
[mailAlert addAction:cancel];
[controller presentViewController:mailAlert animated:YES completion:^{
//
}];
}
else {
// Dismiss the mail controller
[controller dismissViewControllerAnimated:true completion:^{}];
[controller release];
mailController = nil;
}
}