Reputation: 5038
I send mail using following code:
- (IBAction)sendMailPressed:(id)sender
{
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];
}
}
// 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:self.strMailSubject];
// Attach pdf to the email
NSURL *urlToLoad = [[NSBundle mainBundle] URLForResource:self.strSorce withExtension:self.strExtention];
NSData *myData = [NSData dataWithContentsOfURL:urlToLoad];
[picker addAttachmentData:myData mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"%@.%@", self.strSorce, self.strExtention]];
// [self presentModalViewController:picker animated:YES];
[[Singleton sharedInstance] pushModalViewController:picker whereCurrentController:self 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
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
break;
}
[[Singleton sharedInstance] popModalViewControllerAnimated:YES];
}
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
NSString *recipients = [NSString stringWithFormat:@"mailto:&subject=%@", self.strMailSubject];
NSString *email = [NSString stringWithFormat:@"%@", recipients];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
And I get this view:
But I need this view (please, pay attention only to From string, not to the image).
I don't know how to make From string appear
Upvotes: 2
Views: 1565
Reputation: 1780
You can use "setPreferredSendingEmailAddress" for iOS 11 and newer.
Upvotes: 0
Reputation: 334
There isn't any way to set the From: field of MFMailComposeViewController programmatically, probably for security reasons. The owner of the device has control over what accounts are registered on the device and therefore what the From: field is allowed to contain.
The From: field only shows if multiple email accounts have been configured on the device; that field then allows the user to pick which account to send from. If only one account exists on the device, it is not shown (it "goes without saying" which account is being used).
Upvotes: 0
Reputation: 51
If more than one mail account have been added, the from field will be visible in order to select the sender from multiple accounts.
Upvotes: 0
Reputation: 20551
just add this line in your displayComposerSheet
method after create MFMailComposeViewController
and also just set the NSArray
with Recipients names...
NSString *strFullName = [NSString stringWithFormat:@"From : %@",yourSenderEmailAddress];/// here write your email address which dynamic get from your string
NSArray *arrRecipients = [[NSArray alloc]initWithObjects:strFullName, nil];
[picker setCcRecipients:arrRecipients];
I Update code with one example also...
Also Vishal's Answer is also right, i just complete it..
Upvotes: 2