我不認爲你可以直接訪問郵件控制,而無需創建自己的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;
}
}
好的,那麼如果UIActivityType是郵件消息,也可以使用該方法添加附件嗎? – melbournejohn
是的,iOS擁有圖像的UTI,因此它可以識別圖像作爲數據傳入並附加。那麼,實際上它會在電子郵件的情況下將其添加到內聯中。 – rmp
我想我的意思是添加任何類型的附件的一般情況。具體來說,我使用視圖控制器上各個字段的數據創建.TXT文檔,並且希望將該文本文件附加到郵件消息中。 – melbournejohn