如何在UITextView中单击电子邮件链接时打开iPhone的邮件应用程序?

时间:2011-12-06 11:50:09

标签: ios xcode

我是iPhone开发的新手。我在xib中有一个UITextView。我在那里显示一个电子邮件地址链接我想在点击该电子邮件链接时打开iPhone的邮件应用程序。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:12)

正如this answer所指出,您可以设置dataDetectorTypes的{​​{1}}属性:

UITextView

您还应该能够在Interface Builder中设置detectorTypes。

来自Apple documentation

  

UIDataDetectorTypes

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

然后,点击UITextView中的电子邮件地址,即可自动打开邮件应用程序。

另外,如果您想从应用程序本身发送电子邮件,可以使用MFMailComposeViewController。

请注意,要显示MFMailComposeViewController,需要在设备上安装Mail应用程序,并将一个帐户链接到该应用程序,否则您的应用程序将崩溃。

因此,您可以使用 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;

进行检查
[MFMailComposeViewController canSendMail]

MFMailComposeViewController Class Reference

答案 1 :(得分:1)

除了上面的代码,一旦用户按下发送或取消按钮,您将需要关闭模态电子邮件视图。 MFMailComposeViewControllerDelegate协议包含一个名为“didFinishWithResult”的方法。视图关闭时将自动调用此方法。 但是,如果你没有实现它,什么都不会发生&amp;模态视图将保留,使您的应用程序停滞不前!

以下代码至少是必需的:

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

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}