2011-12-06 76 views

回答

12

正如this answer指出的那樣,你可以設置UITextViewdataDetectorTypes屬性:

textview.editable = NO; 
textview.dataDetectorTypes = UIDataDetectorTypeAll; 

你也應該能夠設置detectorTypes在Interface Builder。

Apple documentation

UIDataDetectorTypes

Defines the types of information that can be detected in text-based content. 

enum {  
    UIDataDetectorTypePhoneNumber = 1 << 0, 
    UIDataDetectorTypeLink   = 1 << 1,  
    UIDataDetectorTypeAddress  = 1 << 2,  
    UIDataDetectorTypeCalendarEvent = 1 << 3,  
    UIDataDetectorTypeNone   = 0,  
    UIDataDetectorTypeAll   = NSUIntegerMax 
}; typedef NSUInteger UIDataDetectorTypes; 

只需點擊您的UITextView然後會自動打開郵件應用程序的電子郵件地址。

請注意,如果您想從應用程序本身發送電子郵件,則可以使用MFMailComposeViewController。

請注意,對於要顯示的MFMailComposeViewController,需要在設備上安裝郵件應用程序,並且有一個帳戶鏈接到它,否則您的應用程序將崩潰。

所以,你可以用[MFMailComposeViewController canSendMail]檢查:

// Check that a mail account is available 
    if ([MFMailComposeViewController canSendMail]) { 
     MFMailComposeViewController * emailController = [[MFMailComposeViewController alloc] init]; 
     emailController.mailComposeDelegate = self; 

     [emailController setSubject:subject]; 
     [emailController setMessageBody:mailBody isHTML:YES]; 
     [emailController setToRecipients:recipients]; 

     [self presentViewController:emailController animated:YES completion:nil]; 

     [emailController release]; 
    } 
    // Show error if no mail account is active 
    else { 
     UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"You must have a mail account in order to send an email" delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles:nil]; 
     [alertView show]; 
     [alertView release]; 
    } 

MFMailComposeViewController Class Reference

+0

謝謝Mutix。 – Arun

1

除了上面的代碼,一旦用戶按下發送或取消按鈕,你將需要關閉該模式的郵件視圖。 MFMailComposeViewControllerDelegate協議包含一個名爲「didFinishWithResult」的方法。這個方法將在視圖關閉時自動調用。 但是,如果你不實施它,什麼都不會發生&模態視圖將保持不變,將您的應用程序停頓!

下面的代碼是必需的最低限度:

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 

    // Close the Mail Interface 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 
相關問題