How to implement email for your apps in xcode?
I tried to implement 'mail composer' (sample code from dev) to my apps. They are a lot of hard coded in the sample code. For example: sent to recipient - first@example.com. I want to leave it blank like @""
, however it automatically prefix comma in front of the email address. I also have other concern, based on apple DEV readme.txt, launchMailAppOnDevice
will be triggered if displayComposerSheet
is failed. Should I replace all hardcoded in launchMailAppOnDevice
and how to do it?
Please advise.
-(IBAction)showPicker:(id)sender
{
// This sample can run on devices running iPhone OS 2.0 or later
// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later.
// So, we must verify the existence of the above class and provide a workaround for devices running
// earlier versions of the iPhone OS.
// We display an email composition interface if MFMailComposeViewController exists and the device can send emails.
// We launch the Mail application on the device, otherwise.
Class mailClass = (NSClassFromString(@"MFMailComposeViewController")开发者_Python百科);
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Visit my new apps in app store!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
[picker setToRecipients:toRecipients];
//Replace the email body with the content from my textview
[picker setMessageBody:TextView.text isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
Did you implement the mail class in your .h files
Replace your hard coded values in "launchMailAppOnDevice".. it works for mine
精彩评论