Search This Blog

Thursday, 15 March 2012

Sending Email in iPhone


EMailAppDelegate.h

#import <UIKit/UIKit.h>

@class EMailViewController;

@interface EMailAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomicretainIBOutlet UIWindow *window;

@property (nonatomicretainIBOutlet EMailViewController *viewController;

@end



EMailAppDelegate.m

#import "EMailAppDelegate.h"

#import "EMailViewController.h"

@implementation EMailAppDelegate


@synthesize window=_window;

@synthesize viewController=_viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    // Override point for customization after application launch.
     
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

@end




EMailViewController.h

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface EMailViewController : UIViewController <MFMailComposeViewControllerDelegate>{
    
}

- (IBAction)composeMail;

@end




EMailViewController.m

#import "EMailViewController.h"

@implementation EMailViewController

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    if (error) {
        UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"EMailsendung fehlgeschlagen!" message:[error localizedDescriptiondelegate:nil cancelButtonTitle:@"Ok"otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    [controller dismissModalViewControllerAnimated:true];
}

-(IBAction)composeMail {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Error!" message:@"This device can not send emails. Please set up a valid email account." delegate:nilcancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
MFMailComposeViewController *mailController = [[[MFMailComposeViewController allocinit]autorelease];
[mailController setMessageBody:@"Rahul Varma is cool, because he taught me how to send EMails with the iPhone!" isHTML:false];
mailController.mailComposeDelegate = self;
[self presentModalViewController:mailController animated:true];
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end



--> Add MessageUI.framework in Framework group

In MainWindow.xib file add UINavigationController and add EMailViewController as its View. Make the connection.



In EMailViewController.xib file add an UIButton and make the connection.



Now Build and Run the project.

No comments:

Post a Comment