I want to get email clients into an Action and when I click on an email client the email should send from that particular client. Can anyone tell me how to implement it?
Please click on this for image
It should look like above image.
Hope this will helps you.
UIActionSheet* sheet = [[[UIActionSheet alloc] init] autorelease];
sheet.title = #"Add Accounts";
sheet.delegate = self;
[sheet addButtonWithTitle:#"iCloud"];
[sheet addButtonWithTitle:#"Google"];
[sheet addButtonWithTitle:#"Yahoo"];
if (condition)
[sheet addButtonWithTitle:#"XYZ"];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:#"Cancel"];
then you have to create method for itemclick from actionsheet , you have to open mail app using following code
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];
}
Note, if you want to send the email from within your app itself, you can use the MFMailComposeViewController.
Apple doesnt provide an API to fetch the clients. You will have to design the ActionSheet icons on your own, and in the MFMailComposeViewController on tap of "From" field, you will have to auto populate the field with the suitable email ID.
Related
- (IBAction)showEmail:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init];
composeVC.mailComposeDelegate = self;
// Configure the fields of the interface.
[composeVC setToRecipients:#[#"address#example.com"]];
[composeVC setSubject:#"Hello!"];
[composeVC setMessageBody:#"Hello from California!" isHTML:NO];
// Present the view controller modally.
[self presentViewController:composeVC animated:YES completion:nil];
// composeVC.modalPresentationStyle = UIModalPresentationPageSheet ;
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
}
It always goes in the else condition even when tested on real device. please help. this works in other Xcode versions
I was implementing possibility to invite friends via SMS and small problem occur. Problem is connected with wrong focus on the field.
Image below describe problem :
And as it is possible to see focus is before word "кому:" but not after.
So could some one help me please to understand how it is possible to set proper focus in the MFMessageComposeViewController.
Code that show SMS view is below:
if ([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
[messageController setBody:#"I'm on Memry, come join me! =) \n https://itunes.apple.com/us/app/memry/id735465896?mt=8"];
messageController.navigationBar.titleTextAttributes = #{
NSForegroundColorAttributeName: [UIColor blackColor],
};
messageController.messageComposeDelegate = self;
[self.view.window.rootViewController presentViewController:messageController animated:NO completion:nil];
[messageController release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Memry"
message:#"Your device does not support SMS"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
[messageController setRecipients:#[#"+86 15011582532"]];
[messageController setBody:#"I'm on Memry, come join me! =) \n https://itunes.apple.com/us/app/memry/id735465896?mt=8"];
messageController.navigationBar.titleTextAttributes = #{
NSForegroundColorAttributeName: [UIColor blackColor],
};
messageController.messageComposeDelegate = self;
[self.view.window.rootViewController presentViewController:messageController animated:NO completion:nil];
add some default value in recipients.
I simply trying to add my passbook without sharing via email. How i can add my passbook on click button?
This code assumes that you know how to create the pass in the first place...
NSError * passerror;
PKPass * pass = [[PKPass alloc] initWithData:data error:&passerror];
if(!pass) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Pass Failed" message:#"Sorry there was a problem creating your Passbook." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
return;
}
//init a pass library
PKPassLibrary* passLib = [[PKPassLibrary alloc] init];
//check if pass library contains this pass already
if([passLib containsPass:pass]) {
//pass already exists in library, show an error message
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Pass Exists" message:#"Pass is already in Passbook." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
} else {
//present view controller to add the pass to the library
PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass];
[vc setDelegate:(id)self];
[self presentViewController:vc animated:YES completion:nil];
}
EDIT You'll need to import
#import <PassKit/PassKit.h>
I get to the Mail program fine, and when I press "Send" I hear the swoosh sound and Mail closes and returns me to my app, but the actual mail isn't sending.
Here's the code I'm using to mail. Any ideas about what I'm doing wrong?
(I'm using iOS6, on an actual device, not the simulator.)
-(void)openMail {
//Open Mail program and create email with haiku attached as image.
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithFormat:#"subject"]];
UIImage *myImage = [self createImage];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"xxxxx"];
NSString *emailBody = #"I thought you might like this haiku from the xxxxx iPhone app.";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:NULL];
}
//Unless it's not possible to do so, in which case show an alert message.
else
{
self.alert = [[UIAlertView alloc] initWithTitle:#"I'm sorry." message:#"Your device doesn't seem to be able to email this haiku. Perhaps you'd like to tweet it or post it on Facebook instead?" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[self.alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:Nil];
}
It looks like it's actually a problem between my server and the iOS 6.1 update. I'll leave this up in case other people have the same problem. If the issue is fixed and my email STILL won't send, then I'll post another question.
i am a newbie to iOS and am trying to add inapt email in my app. I have a screen in which pushing the email icon should open the inapp email. I have the code for the inapp email. However, the button is already an outlet on the controller. So, I don't know how to link the same button to a different class/file which has the code for the inapp email. I was thinking of setting up a delegate but don't know how to initialize the delegate in the mail class. Have been struggling for a few days...please help!
Sumit
Try MFMailComposeViewController.... here is some sample code:
Make sure you import the MEssageUI framework and import the MFMailComposeViewController/MessageUI in .h and also conform to its delegate
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
[mailView setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailView setSubject:#"Interesting Apple News Article!"];
NSString *mailString = [[NSString alloc] initWithFormat:#"Test!"];
[mailView setMessageBody:mailString isHTML:NO];
[mailView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:mailView animated:YES];
[mailString release];
[mailView release];
} else
[mailView release];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mailing Error" message:[error localizedDescription] delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self dismissModalViewControllerAnimated:YES];
} else {
[self dismissModalViewControllerAnimated:YES];
}
}