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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8399439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:41:17  来源:igfitidea点击:

How to open iPhone's mail app when clicking an email link in UITextView?

iosxcode

提问by Arun

I am new in iPhone devlopment. I have an UITextView in a xib. There I displaying an email address link. I want to open iPhone's mail application while clicking on that email link. How can I achieve that?

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

回答by Mutix

As pointed out in this answer, you can set the dataDetectorTypesproperty of the UITextView:

如此答案中所指出的,您可以设置以下dataDetectorTypes属性UITextView

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

You should also be able to set the detectorTypes in Interface Builder.

您还应该能够在 Interface Builder 中设置检测器类型。

From 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;

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;

Clicking on the email address in your UITextView should then automatically open the Mail application.

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

On a side note, if you want to send the email from within your app itself, you can use the MFMailComposeViewController.

附带说明一下,如果您想从您的应用程序内部发送电子邮件,您可以使用 MFMailComposeViewController。

Note that for the MFMailComposeViewController to be shown, Mail app needs to be installed on the device, and have an account linked to it, otherwise your app will crash.

请注意,要显示 MFMailComposeViewController,需要在设备上安装邮件应用程序,并关联一个帐户,否则您的应用程序将崩溃。

So you can check this with [MFMailComposeViewController canSendMail]:

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

MFMailComposeViewController 类参考

回答by Chris Johnson

In addition to the code above, once the user has pressed the send or cancel buttons you will need to dismiss the modal email view. The MFMailComposeViewControllerDelegate protocol includes a method called "didFinishWithResult". This method will be automatically called as the view closes. However, if you don't implement it, nothing will happen & the modal view will remain, bringing your app to a standstill!

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

The following code is required as a minimum:

至少需要以下代码:

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

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