Search Posts in my Blog

Monday, 11 July 2011

Sending Email Example


EMailAppDelegate.h

#import <UIKit/UIKit.h>

@class EMailViewController;

@interface EMailAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet 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 alloc] initWithTitle:@"EMailsendung fehlgeschlagen!" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    [controller dismissModalViewControllerAnimated:true];
}

-(IBAction)composeMail {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"This device can not send emails. Please set up a valid email account." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] 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.


Tuesday, 21 June 2011

Customized Radio Button in iPhone

1) Create a new project in Xcode
2) Add new file SelectRadioButton.h and SelectRadioButton.m  with SelectRadioButton.xib file.

SelectRadioButton.xib





SelectRadioButton.h


#import <UIKit/UIKit.h>


@interface SelectRadioButton : UIViewController {

IBOutlet UILabel *lbl1;
IBOutlet UILabel *lbl2;
IBOutlet UILabel *lbl3;
IBOutlet UILabel *lbl_Selected;
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
IBOutlet UIButton *btn3;
}

-(IBAction)btn1Clicked;
-(IBAction)btn2Clicked;
-(IBAction)btn3Clicked;

@property (nonatomic,retain) IBOutlet UILabel *lbl1;
@property (nonatomic,retain) IBOutlet UILabel *lbl2;
@property (nonatomic,retain) IBOutlet UILabel *lbl3;
@property (nonatomic,retain) IBOutlet UILabel *lbl_Selected;

@property (nonatomic,retain) IBOutlet UIButton *btn1;
@property (nonatomic,retain) IBOutlet UIButton *btn2;
@property (nonatomic,retain) IBOutlet UIButton *btn3;

@end


3) Make all connection in xib fine accordingly
4) radioButtonDisabled.png and radioButtonActiveHover.png are two png's included in the project.
 Open and save the images. Drag these images to your project.







SelectRadioButton.m

#import "SelectRadioButton.h"


@implementation SelectRadioButton

@synthesize lbl1;
@synthesize lbl2;
@synthesize lbl3;
@synthesize lbl_Selected;
@synthesize btn1;
@synthesize btn2;
@synthesize btn3;




/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    [super viewDidLoad];
}

-(IBAction)btn1Clicked
{
[btn1 setSelected:YES];

[btn1 setImage:[UIImage imageNamed:@"radioButtonActiveHover.png"] forState:UIControlStateSelected];
[btn2 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
[btn3 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
lbl_Selected.text = lbl1.text;

}


-(IBAction)btn2Clicked
{
[btn2 setSelected:YES];
[btn2 setImage:[UIImage imageNamed:@"radioButtonActiveHover.png"] forState:UIControlStateSelected];
[btn3 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
[btn1 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
lbl_Selected.text = lbl2.text;
}


-(IBAction)btn3Clicked
{
[btn3 setSelected:YES];
[btn3 setImage:[UIImage imageNamed:@"radioButtonActiveHover.png"] forState:UIControlStateSelected];
[btn2 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
[btn1 setImage:[UIImage imageNamed:@"radioButtonDisabled.png"] forState:UIControlStateSelected];
lbl_Selected.text = lbl3.text;
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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.
}

- (void)viewDidUnload {
    [super viewDidUnload];
lbl1=nil;
lbl2=nil;
lbl3=nil;
lbl_Selected=nil;
btn1=nil;
btn2=nil;
btn3=nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
[lbl1 release];
[lbl2 release];
[lbl3 release];
[lbl_Selected release];
[btn1 release];
[btn2 release];
[btn3 release];
    [super dealloc];
}


@end


OUTPUT:



NOW Build and Run n' enjoy the new creation :)

Download the Source Code HERE.