how to mail forget password in iphone - iphone

I am developing an application, i want to know how to achive forget password scenario in iPhone.
When user forget their password then I have a button in my app when i click on it UIAlertView open in which i have a textfield, user must enter their email address and the password get mail on that mail id.
How can I do that I have define a action for button this is the code:
-(IBAction)forgetpassword:(id)sender
{
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Forget Password" message:#"Please Enter your Email address " delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
}
but what i need is just require the code to which i can use and mail the password to the email id which user will entered.

just add delegate MFMessageComposeViewControllerDelegate to the .h file and then use this code when you want to email and also add framework MessageUI.framework in the project
-(IBAction)forgetpassword:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = #"your Message";
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"e-Mail Sending Alert"
message:#"You can't send a mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
and this bellow method is delegate method of MFMessageComposeViewControllerDelegate
#pragma mark - MFMessage Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
if (result == MFMailComposeResultSent)
{
NSLog(#"\n\n Email Sent");
}
[self dismissViewControllerAnimated:YES completion:nil];
}
you can also use SKPSMTPmessage web-service for send e-mail
i hope this help you...

Related

Using MFMailController in UITabbar controller

I am using UITabbarcontroller in one of the viewcontroller, In one tabbar i need to have only Mail controller, when i am doing so , then that is entering into infinite loop, how would i overcome it, The Code related to
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { NSString *message = #"";
// Notifies users about errors associated with the interface
switch (result) {
case MFMailComposeResultCancelled:
message = #"Mail: canceled";
break;
case MFMailComposeResultSaved:
message = #"Mail: saved";
break;
case MFMailComposeResultSent:
message = #"Mail: sent";
break;
case MFMailComposeResultFailed:
message = #"Mail: failed";
break;
default:
message = #"Mail: not sent";
break;
}
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[controller dismissModalViewControllerAnimated:YES];
}
This is how I would do it
Drag and drop a UITabBarButton object on your UITabBar and call it "Email". Now create this IBAction
-(IBAction)composeMyEmail
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mailViewController setToRecipients:[NSArray arrayWithObject:#"k.sourish.k#gmail.com"]];
[mailViewController setSubject:#"Subject Goes Here."];
[mailViewController setMessageBody:#"Your message goes here." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(#"Device is unable to send email in its current state.");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#" Please Configure Your Mail Account" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[mailViewController release];
}
Connect your IBAction to your "Email" button.
Remove all the code you have under viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

iPhone iOS 5 SDK in app SMS error

thanks in advance every time I call to create an in app SMS i get the following error...
Application tried to push a nil view controller on target .
This is the code I have that worked in OS 4 SDK...
MFMailComposeViewController *MailController;
MFMessageComposeViewController *SMScontroller;
NSError *error;
NSString *EmailMessage;
NSString *SubjectMessage;
-(IBAction)sendInAppSMS
{
if([MFMessageComposeViewController canSendText])
{
SMScontroller = [[MFMessageComposeViewController alloc] init];
SMScontroller.messageComposeDelegate = self;
NSString *MessageString = #"Hello";
SMScontroller.body = MessageString;
SMScontroller.navigationBar.tintColor = [UIColor blackColor];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[self presentModalViewController:SMScontroller animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
}
else{
// alertView to tell user to setup a mail account.
NSString *message = [[NSString alloc] initWithFormat:#"To use this feature, you need to have an iPhone with SMS abilities."];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SMS Account Not Detected" message:message delegate:nil cancelButtonTitle:#"Understood" otherButtonTitles:nil];
[alert show];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"User cancelled sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
case MessageComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"Error occured while sending the SMS"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
case MessageComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"I Like It" message:#"SMS sent successfully..!"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
I believe if you are running this in the simulator you'll get the error since it doesn't have sms. I get the same error in simulator but if i connect to my phone it works fine.

MFMailComposeViewController navigation bar buttons are disabled

I use MFMailComposeViewController to send mail in my app. But when present mail compose view controller, all of navigation buttons are disabled (except back button in select mail address screen), i must use Home button to quit app. Does anyone has idea?
Here is screen shot:
Code:
- (void)shareVieEmail
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"Test subject"];
[mailViewController setMessageBody:#"Mail message body" isHTML:NO];
NSData *imageData = [NSData dataWithContentsOfFile:photourl];
[mailViewController addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"example_photo"];
[self presentModalViewController:mailViewController animated:YES];
} else {
[[[UIAlertView alloc] initWithTitle:#"Cannot send mail" message:#"Device is unable to send email in its current state" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
}
Delegate method :
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
//NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
//NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Result" message:#"Mail Sent Successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
case MFMailComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Result" message:#"Mail Sent Failed" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
default:
//NSLog(#"Result: not sent");
break;
}
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Cannot send mail" message:[NSString stringWithFormat:#"ERROR:%#", [error userInfo]] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
[self dismissModalViewControllerAnimated:YES];
}
And in header file, I declared implement MFMailComposeViewControllerDelegate.
I had exactly the same problem. and it took me a while to figure this out but no surprise it came down to customized UIBarButtonItem
I bet in your UIBarButtonItem.h there is a method
-(void)setEnabled:(BOOL)enabled ;
and the implementation looks like this:
-(void)setEnabled:(BOOL)enabled {
if (self.customView) {
if ([[self.customView.subviews objectAtIndex:0] isKindOfClass:[UIButton class]]) {
((UIButton*)[self.customView.subviews objectAtIndex:0]).enabled = enabled;
}
}
}
and this is causing problem so as soon as you comment out this method your problem should go away.
I also had this problem, but it my case it was because I had overridden setNavigationBarHidden:animated: from UINavigationController as proposed in this workaround for a bug in CNContactViewController. One solution that would still include the workaround and solve the problem in MFMailComposeViewController would be to use method swizzling to be able to call either the original method or the overridden one, depending on the class of the current topViewController.
In your MFMailComposeViewController's delegate you need to implement didFinishWithResult: and dismiss the modal view controller from there.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// you can test the result of the mail sending here if you want
[self dismissModalViewControllerAnimated:YES];
}
For swift 4.0+
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}

Trouble opening the MFMailComposeViewController on the device, works in simulator.

I am doing this its working in simulator but when we try to open in device then program is terminating.
Plz suggess me fast.
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
//[self becomeFirstResponder];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
if ([MFMessageComposeViewController canSendText])
Your problem is here. You are trying to check whether device will be able to send Text messages and not email Message. you should try using
if([MFMailComposeViewController canSendMail])
The problem might be that your device is not configured to any accounts in the mail.Please check this once.
Have you implement MFMailComposeViewControllerdelegate methods in your code??
#pragma mark --------------------------------------------
#pragma mark MFMailComposeViewController delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Mail send canceled.");
/*
Execute your code for canceled event here ...
*/
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved.");
/*
Execute your code for email saved event here ...
*/
break;
case MFMailComposeResultSent: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sent" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 1;
alert.delegate = self;
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sending Failed" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 2;
alert.delegate = self;
[alert show];
[alert release];
break;
}
default:
break;
}
[controller dismissModalViewControllerAnimated:YES];//dismissing modal view controller
}
Your code looks ok, but did you check if the device can send mail:
if ([MFMailComposeViewController canSendText]) {
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
[mail release], mail = nil;
} else {
// show message to the use that he can't send an email.
}

MFMailComposer crashes

In my app I am using MFMailComposer. It crashes when I send mail without mail configuration (without having entered mail ID and password in the Mail app on the device).
This is the line that causes the crash:
[self presentModalViewController:picker animated:YES];
-(void) showEmailModalView
{
NSLog(#"Start method <ExportStatisticsController> : <showEmailModalView> --");
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the
//user did with your email sheet
[picker setSubject:SendEmailSubject];
NSArray *torec=[[NSArray alloc] initWithObjects:SendEmailToEmailID, nil];
[picker setToRecipients:torec];
[torec release];
//------ message body ---
NSString *body =#"";
body=[NSString stringWithFormat:#"%# From : %#\n",body, emailTextField.text];
body=[NSString stringWithFormat:#"%# Subject : %#\n",body,subjectTextField.text];
//email contents
body = [NSString stringWithFormat:#"%# Message : \n %#", body,messageBodyTextView.text];
[picker setMessageBody:body isHTML:NO]; // depends. Mostly YES, unless you want to send it as plain text (boring)
picker.navigationBar.barStyle = UIBarStyleBlack; // choose your style, unfortunately, Translucent colors behave quirky.
[self presentModalViewController:picker animated:YES];
[picker release];
NSLog(#"End method <ExportStatisticsController> : <showEmailModalView> --");
}
// 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
{
NSLog(#"Start method <ExportStatisticsController> : <didFinishWithResult> --");
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Message MFMailComposeResultCancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Message MFMailComposeResultSaved");
break;
case MFMailComposeResultSent:
NSLog(#"Message sent Successfuly");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Mail Sent Successfully!"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
case MFMailComposeResultFailed:
NSLog(#"Message MFMailComposeResultFailed");
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
NSLog(#"End method <ExportStatisticsController> : <didFinishWithResult> --");
}
You should call
[MFMailComposeViewController canSendMail]
before presenting the view controller, eg
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[self presentModalViewController:composer];
} else {
// Show error message maybe
}
That's true. You will have to configure your mail app and check whether the device can send mail or not. Since if it would have been possible without mail app then we would be able to send mail via simulator (which I think is not possible). The receiver of your mail must know from where he/she is getting the mail and I think the sender cannot be set from the code I might be wrong but these are my view since I was struggling the same situation.
Hope it helps.