【问题标题】:How to open iPhone's mail app when clicking an email link in UITextView?单击 UITextView 中的电子邮件链接时如何打开 iPhone 的邮件应用程序?
【发布时间】:2012-01-14 00:39:07
【问题描述】:

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

【问题讨论】:

    标签: ios xcode


    【解决方案1】:

    正如this answer中指出的,你可以设置UITextView的dataDetectorTypes属性:

    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,需要在设备上安装 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

    【讨论】:

      【解决方案2】:

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

      至少需要以下代码:

      - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
      {
      
          // Close the Mail Interface
          [self dismissViewControllerAnimated:YES completion:NULL];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-30
        • 2017-12-25
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-30
        • 1970-01-01
        相关资源
        最近更新 更多