How to open iPhone app for iPhone when clicking email link in UITextView?

I am new to iPhone devlopment. I have a UITextView in xib. There I show the email address. I want to open the iPhone email application by clicking on this link via email. How can i achieve this?

+6
source share
2 answers

As pointed out in this answer , you can set the dataDetectorTypes property for a UITextView :

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

You should also be able to set type identifiers in 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; 

When you click on an email address in UITextView, the Mail application automatically opens.

On the side of the note, if you want to send an email from the application itself, you can use MFMailComposeViewController.

Please note that in order to display the MFMailComposeViewController, the device must have the Mail application installed and have an account associated with it, otherwise your application will fail.

So, you can check this using [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

+12
source

In addition to the code above, as soon as the user presses the send or cancel buttons, you will need to reject the email view mode. The MFMailComposeViewControllerDelegate protocol includes the "didFinishWithResult" method. This method will be automatically called as the view closes. However, if you do not implement it, nothing will happen, and the modal presentation will remain, bringing your application to rest!

The following code is required:

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

Source: https://habr.com/ru/post/903186/


All Articles