2015-07-01 27 views
0

第一篇文章,所以我會盡量做到直接和儘可能詳細...我使用UIActivityViewController和繼承UIActivityItemProvider來處理共享不同的文本方法取決於選擇共享方法(此線程幫助了很多:How to know which icon is clicked in UIActivityViewController before activityController setCompletionHandleris called?)。在Xcode中使用UIActivityViewController填充電子郵件標題(和附加文件)

我想我已經想出了讓我的源數據通過然後根據itemForActivityType選擇進行處理的機制,但對於電子郵件共享,我想預先填充標題(主題,正文等)爲以及附加文件。我知道如何做到這一點與獨立MFMailComposeViewController,但我不知道如何使用ActivityViewController做到這一點。在這裏:UIActivityViewController - Email and Twitter sharing,有使用像初始化值/密鑰對中提到:

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities]; 
[activityViewController setValue:@"My Subject Text" forKey:@"subject"]; 

但什麼是設置收件人和郵件正文中的其他標準鍵?

附加文件是我有的另一個大問題。我知道如何創建一個文檔並將其保存在文件系統中並將其附加到該文件 - 再次使用MFMailComposeViewController - 但我想使用UIActivityViewController獲得相同的行爲。

(遺憾的冗長......)

回答

0

要設置你應該使用 - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType

爲您的數據(文本和圖像)的主題,您可以通過activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType{通過他們這將返回一個UTI爲您的數據提供服務,以便更好地處理數據。您使用的鑰匙並不重要,因爲UTI有助於識別物品。 Apple Doc:

提供UTI允許服務以適當的方式處理特定的數據類型,例如將電子郵件服務格式化爲可以在線顯示的圖像。 More info here

+0

好的,那麼如果UIActivityType是郵件消息,也可以使用該方法添加附件嗎? – melbournejohn

+0

是的,iOS擁有圖像的UTI,因此它可以識別圖像作爲數據傳入並附加。那麼,實際上它會在電子郵件的情況下將其添加到內聯中。 – rmp

+0

我想我的意思是添加任何類型的附件的一般情況。具體來說,我使用視圖控制器上各個字段的數據創建.TXT文檔,並且希望將該文本文件附加到郵件消息中。 – melbournejohn

0

我不認爲你可以直接訪問郵件控制,而無需創建自己的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; 
    } 
} 
相關問題