我是 iPhone 开发的新手。我在 xib 中有一个 UITextView。在那里我显示了一个电子邮件地址链接。我想在单击该电子邮件链接时打开 iPhone 的邮件应用程序。我怎样才能做到这一点?
最佳答案
正如 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,需要在设备上安装邮件应用程序,并关联一个帐户,否则您的应用程序将崩溃。
因此您可以使用 [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];
}
关于ios - 单击 UITextView 中的电子邮件链接时如何打开 iPhone 的邮件应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399439/