Showing posts with label iPhone. Show all posts
Showing posts with label iPhone. Show all posts

Thursday, November 22, 2012

Send Email with Attachment in iphone

Send Email with Attachment in iphone

1) In Xcode 4 goto the build phases tab for your target. Make sure you see MessageUI.framework. If it's not there click + to add a new framework.

2)In your interface file


#import <MessageUI/MFMailComposeViewController.h>


@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> 


3)Add this Method


-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Check My Image!"];

    // Set up recipients
    // NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
    // NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
    // NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];

    // [picker setToRecipients:toRecipients];
    // [picker setCcRecipients:ccRecipients];  
    // [picker setBccRecipients:bccRecipients];

    // Attach an image to the email
    UIImage *myImage = YourImage;
    NSData *myData = UIImagePNGRepresentation(
myImage);
    [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"
myImage.png"];

    // Fill out the email body text
    NSString *emailBody = @"My Pic";
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentModalViewController:picker animated:YES];

    [picker release];
}


4) Implement the delegate method

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{  
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}




Thursday, November 15, 2012

Take SnapShot and save in gallery in iphone

Take SnapShot and Save in gallery in iphone

canvasView is UIView. if you want to take snapshot of whole screen use self.view.

    UIGraphicsBeginImageContext(canvasView.frame.size);
    [canvasView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

Wednesday, November 14, 2012

Drag UIImageVIew ( touch Event ) in iphone


Drag UIImageVIew ( touch Event ) in iphone

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
blockView.center = location;   // blockView = UIimageView which you want to drag
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}

Thursday, November 8, 2012

Custom Color in Iphone and Core Plot

Custom Color in Iphone and Core Plot



Add Custom Color in Iphone

[UIColor colorWithRed:0.47 green:0.77 blue:0.37 alpha:1.0



Add Custom Color in Core-Plot

[CPTColor colorWithComponentRed:0.47 green:0.77 blue:0.37 alpha:1.0];

Thursday, November 1, 2012

Merge two NSDictionary in one NSDictionary ( if keyvalues are same )

Merge two NSDictionary in one NSDictionary ( if keyvalues are same )


NSMutableDictionary *combined = [your_nsdictionary mutableCopy];
    for(NSString *key in [combined allKeys])
    {
        NSString *breakfastDate = [your_nsdictionary valueForKey:key];
        NSString *dinnerDate = [your_nsdictionarytwo objectForKey:key];
        if(dinnerDate){
            [combined setObject:[NSString stringWithFormat:@"%@,%@",breakfastDate, dinnerDate] forKey:key];
            } else {
                [combined setObject:[NSString stringWithFormat:@"%@,0",breakfastDate] forKey:key];
                }
    }
    NSLog(@"Combine %@",combined);

Remove Duplicate Values from NSMutableArray iPhone

Remove Duplicate Values from NSMutableArray iPhone

NSArray *copy = [mutableArray copy];
NSInteger index = [copy count] - 1;
for (id object in [copy reverseObjectEnumerator]) {
    if ([mutableArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
        [mutableArray removeObjectAtIndex:index];
    }
    index--;
}
[copy release];

Thursday, October 25, 2012

Split one string into different strings iPhone


Split one string into different strings



NSString *str = @"011597464952,01521545545,454545474,454545444|Hello this is were the message is.";
    
    NSArray *firstSplit = [str componentsSeparatedByString:@"|"];
    NSAssert(firstSplit.count == 2@"Oops! Parsed string had more than one |, no message or no numbers.");
    NSString *msg = [firstSplit lastObject];
    NSArray *numbers = [[firstSplit objectAtIndex:0componentsSepratedByString:@","];
    
    // print out the numbers (as strings)
    for(NSString *currentNumberString in number) {
        NSLog(@"Number: %@", currentNumberString);
    }

Wednesday, October 24, 2012

IPhone Custom Toolbar on Keyboard to Hide keyboard

IPhone Custom Toolbar on Keyboard

MY requirement was to work on numeric keys not alphabets so i have used Numeric Pad on UITEXTFIELD  and you all know that by default there is no button on numeric pad for hidden keyboard. You have to create Custom Button on ur numeric pad and call [numberTextField resignFirstResponder]; to hide numeric pad.

Solution

I have create a custom toolbar and have put two buttons on it 1) Apply and 2) Cancel. And created a method for both buttons.


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                           nil];
    [numberToolbar sizeToFit];
    numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField resignFirstResponder];
}

Thats it .. 
You can add Custom button on left side of 0 button. 



Tuesday, October 23, 2012

Core Plot Custom Themes in iPhone

Core Plot Custom Themes in iPhone


    CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme]; 
    graph = (CPXYGraph *)[theme newGraph];
    
    graph.fill = [CPFill fillWithColor:[CPColor clearColor]];
    graph.plotAreaFrame.fill = [CPFill fillWithColor:[CPColor clearColor]];

You can apply following Themes in Core Plot

themeNamed:kCPPlainWhiteTheme
themeNamed:kCPTDarkGradientTheme
themeNamed:kCPTPlainBlackTheme
themeNamed:kCPTSlateTheme
themeNamed:kCPTStocksTheme

Multiple PickerViews in one View


Multiple PickerViews in one View


Solution

if([pickerView isEqual:pickerView2])
    {
        return 3;
    }
    if ([pickerView isEqual:pickerView1]) {
        return 4;
    }


Complete Source Code

ViewController.h

@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
    IBOutlet UILabel *mlabel;
    NSMutableArray *arrayNo;
    IBOutlet UIPickerView *pickerView2;
    NSMutableArray *arrayNo1;
    IBOutlet UIPickerView *pickerView1;

}
@property (nonatomic, retain) UILabel *mlabel;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
@synthesize mlabel;

- (void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@" 100 "];
[arrayNo addObject:@" 200 "];
[arrayNo addObject:@" 400 "];
[arrayNo addObject:@" 600 "];
[arrayNo addObject:@" 1000 "];
[pickerView2 selectRow:1 inComponent:0 animated:NO];
    
    arrayNo1 = [[NSMutableArray alloc] init];
[arrayNo1 addObject:@" 1001 "];
[arrayNo1 addObject:@" 2001 "];
[arrayNo1 addObject:@" 4001 "];
[arrayNo1 addObject:@" 6001 "];
[arrayNo1 addObject:@" 10001 "];
[pickerView1 selectRow:1 inComponent:0 animated:NO];
    
    mlabel.text= [arrayNo1 objectAtIndex:[pickerView2 selectedRowInComponent:0]];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    if([pickerView isEqual:pickerView2])
    {
        return 1;
    }
    if ([pickerView isEqual:pickerView1]) {
        return 1;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if([pickerView isEqual:pickerView2])
    {
        mlabel.text= [arrayNo objectAtIndex:row];
    }
    if ([pickerView isEqual:pickerView1]) {
        mlabel.text= [arrayNo1 objectAtIndex:row];
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if([pickerView isEqual:pickerView2])
    {
        return 3;
    }
    if ([pickerView isEqual:pickerView1]) {
        return 4;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if([pickerView isEqual:pickerView2])
    {
        return [arrayNo objectAtIndex:row];
    }
    if ([pickerView isEqual:pickerView1]) {
        return [arrayNo1 objectAtIndex:row];
    }
}

- (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
{

}

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

@end

ViewController.XIB


2 UIPicker and 1 UILabel
Connect Datasource and delegate of both picker to file owner.
NOTE:-
Sorry for naming convention. You can achieve this multi picker by using one pickerView and divide it in number of columns.
In numberOfComponentsInPickerView return how many columns you want in one pickerView.

Loaded nib but the view outlet was not set



Loaded nib but the view outlet was not set

I was working on my iPhone project . And i copied Components from one nib file to another and when i run that project it gave this error. It happen when you copy nib components to another nib or sometime when you open new nib File.

Solution
  • Open the XIB file causing problems
  • Click on file's owner icon on the left bar (top one, looks like a yellow outlined box)
  • If you don't see the right-hand sidebar, click on the third icon above "view" in your toolbar. This will show the right-hand sidebar
  • In the right-hand sidebar, click on the third tab--the one that looks a bit like a newspaper
  • Under "Custom Class" at the top, make sure Class is the name of the ViewController that should correspond to this view. If not, enter it
  • In the right-hand sidebar, click on the last tab--the one that looks like a circle with an arrow in it
  • You should see "outlets" with "view" under it. Drag the circle next to it over to the "view" icon on the left bar (bottom one, looks like a white square with a thick gray outline
  • Save the xib and re-run



Monday, October 22, 2012

iPhone AlertView



AlertVIew in iphone with 2 buttons OK/YES/DELETE and NO/CANCEL .
// 2 Buttons
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this.  This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];

Performing Action on Both Buttons i.e Delete and Cancel.
buttonIndex 0 for Delete
buttonIndex 1 for Cancel

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
   if (buttonIndex == 0) { 
        NSLog(@"Delete Tapped."); 
    } 
    else if (buttonIndex == 1) { 
       NSLog(@"Cancel Tapped!"); 
    } 
}

Same u can perform action with single button alert view.

AlertVIew in iphone with 1 buttons OK/YES
// 1 Button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"No INTERNET CONNECTION" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];