微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/iOS/330196.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐