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

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

解决方法

正如 this answer中所指出的,您可以设置UITextView的dataDetectorTypes属性
textview.editable = NO;
textview.dataDetectorTypes = UIDataDetectorTypeAll;

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

Apple documentation开始:

UIDataDetectorTypes

06001

单击UITextView中的电子邮件地址应该会自动打开Mail应用程序.

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

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

所以你可以用[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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...