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

标签 ios xcode

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

最佳答案

正如 this answer 中指出的那样,您可以设置 UITextViewdataDetectorTypes 属性:

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

您还应该能够在 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

关于ios - 单击 UITextView 中的电子邮件链接时如何打开 iPhone 的邮件应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399439/

相关文章:

ios - viewDidLoad后UIButtons的IBOutletCollection为空

python - 通过 sh 导入模块错误运行 .py 脚本

ios - Unity 3.5 到 Unity 4.5.4

ios - UITableViewCell中的Braintree信用卡

ios - 如何修复具有 +1 保留计数 (ARC) 的 Core Foundation 对象?

android - 客户端应用程序架构

ios - PFUser登录后如何检索ObjectID?

ios - Swift 4 @escaping 类型作为参数

ios - (iOS) 内购失败?

ios - 在应用程序中保存默认数据 ios swift 核心数据