Skip to content

Instantly share code, notes, and snippets.

@eiffelqiu
Created May 25, 2011 01:10
Show Gist options
  • Save eiffelqiu/990122 to your computer and use it in GitHub Desktop.
Save eiffelqiu/990122 to your computer and use it in GitHub Desktop.
MFMailComposeViewController with availability check
-(void)sendMailByTitle:(BOOL)title andText:(NSString*)text byViewController:(UIViewController*)vCtr{
self.strText=text;
self.strTitle=title;
vRef=vCtr;
[self emailThisNote];
}
-(void)emailThisNote{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
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];
}
}
#pragma mark -
#pragma mark Compose Mail
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// load value from DocDetail.plist
NSDictionary *d=[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DocDetail" ofType:@"plist"]];
[picker setSubject:[d valueForKey:@"DocTitle"]];
NSString *strDocTitle=[d valueForKey:@"DocTitle"];
NSString *strDtlTitle=(self.strTitle)?[d valueForKey:@"NoteText"]:[d valueForKey:@"HighlightText"];
NSString *strDocLink=[d valueForKey:@"DocumentLink"];
NSString *strImgDocuApp=[d valueForKey:@"Image_zzzApp"];
NSString *strImgzzzCompany=[d valueForKey:@"Image_zzzCompany"];
NSString *strWebLink=[d valueForKey:@"Web_Link"];
NSString *strEmailLink=[d valueForKey:@"Email_link"];
NSString *strToMail=[[NSString alloc] initWithFormat:@"<html><body><p><font family='Arial' size='3pt' color='darkblue'>%@</font></p><h3 style='color:brown'><u>%@</u></h3><p><font family='Arial' size='3pt' color='brown' style='background-color:yellow'>%@</font></p><p><font family='Arial' size='3pt' color='darkblue'>You can download this DocuApps&trade; document App for free by clicking this link:<a href='%@'>%@</a></font></p><p><font family='Arial' size='3pt' color='green' style='font-weight:bold'><img src='%@' height='88px' width='125px'><br>DocuApps&trade; is a product of The APP Company</font></p><img src='%@' width='175px' height='43px'><br><font family='Arial' style='font-weight:bold' color='blue'>For more information:</font><br><font family='Arial' size='3pt' color='brown'>Web :<a href='http://%@'>%@</a><br>Email: <a href='mailto:%@'>%@</a><br></font><font family='Century Gothic' size='1px' color='brown'>&copy;2010 The APP Company. All Rights Reserved.</font><hr></body></html>",strDtlTitle,strDocTitle,self.strText,strDocLink,strDocLink,strImgzzzApp,zzzCompanympany,strWebLink,strWebLink,strEmailLink,strEmailLink];
[picker setMessageBody:strToMail isHTML:YES];
[strToMail release]; strToMail=nil;
[vRef presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[vRef dismissModalViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark Workaround
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice {
UIAlertView *av=[[[UIAlertView alloc] initWithTitle:@"Message" message:@"Mail composer not available." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[av show];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment