Monday, 24 June 2013

Swipe UIView left or right

- (void)viewDidLoad
    [super viewDidLoad];
    UISwipeGestureRecognizer *recognizerRight;

    recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
    [recognizerRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [scrollableView addGestureRecognizer:recognizerRight];
    UISwipeGestureRecognizer *recognizerLeft;
    recognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeleft:)];
    [recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [scrollableView addGestureRecognizer:recognizerLeft];


-(void)swipeleft:(UISwipeGestureRecognizer *)swipeGesture

        CATransition *animation = [CATransition animation];
        [animation setDelegate:self];
        [animation setType:kCATransitionPush];
        [animation setSubtype:kCATransitionFromRight];
        [animation setDuration:0.50];
        [animation setTimingFunction:
         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [scrollableView.layer addAnimation:animation forKey:kCATransition];
-(void)swipeRight:(UISwipeGestureRecognizer *)swipeGesture
        CATransition *animation = [CATransition animation];
        [animation setDelegate:self];
        [animation setType:kCATransitionPush];
        [animation setSubtype:kCATransitionFromLeft];
        [animation setDuration:0.40];
        [animation setTimingFunction:
         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [scrollableView.layer addAnimation:animation forKey:kCATransition];


Smooth a UIImage or resize

- (UIImage *)smoothImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect neuRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRefrence = image.CGImage;
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
    CGContextConcatCTM(context, flipVertical);
    // Draw into the context; this scales the image
    CGContextDrawImage(context, neuRect, imageRefrence);
    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    return newChangedImage; //  changed Image

Wednesday, 19 June 2013

Tuesday, 18 June 2013

Adding data to the plist dynamically and adding rows

NSMutableArray *array = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:plistPath];
    [array addObject:sender];
    [array writeToFile:[self.documentsDir stringByAppendingPathComponent:@"data.plist"] atomically:YES];   
        [array addObjectsFromArray:[NSArray arrayWithContentsOfFile:plistPath]];
        [array addObject:sender];
        [array writeToFile:[self.documentsDir stringByAppendingPathComponent:@"data.plist"] atomically:YES];


Device current date xcode

    NSDate *date=[NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd MM YYYYhh:mm:ss"];// set your required format
    NSString *dateTaken = [formatter stringFromDate:date];

    NSLog(@"strDateTaken %@",dateTaken);

Monday, 17 June 2013

To check internet availability xcode

Firstly add
SystemConfiguration/SystemConfiguration framework in your project and include in your class.
and also add Reachability.h and Reachability.m (apple developer help class) from  this link
in your project. 
and include in your class.m file.

#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

Add below method in your class.m file

- (BOOL)connected
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);

- (void)viewDidLoad
[super viewDidLoad];

  if (![self connected]) 
        // not connected
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Internet Connection Not Found" message:@"Please check your network settings!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    } else
        // connected, do some internet stuff


Post method to send request and get response using NSURLConnection xcode

First of all you need to add delegate name NSURLConnectionDataDelegate then

@property (retain, nonatomic) NSURLConnection *connection;

@property (retain, nonatomic) NSMutableData *receivedData;

in your class.h file

Then call below method on post button

    [self.connection cancel];
    //initialize new mutable data
    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;
    //initialize url that is going to be fetched.
    NSURL *url = [NSURL URLWithString:@""];
    //initialize a request from url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
    //set http method
    [request setHTTPMethod:@"POST"];
    //initialize a post data
    NSString *postData = @"mac_id=val2&";
//Here you can give your parameters value

    //set request content type we MUST set this value.
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    //set post data of request
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
    //initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connection = connection;
    //start the connection
    [connection start];
//Add below delegate methods
 this method might be calling more than one times according to incoming data size
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.receivedData appendData:data];
 if there is an error occured, this method will be called by connection
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"%@" , error);

 if data is successfully received, this method will be called by connection
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //initialize convert the received data to string with UTF8 encoding
    NSString *htmlSTR = [[NSString alloc] initWithData:self.receivedData
   // NSLog(@"%@" , htmlSTR);
    NSError *e = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: self.receivedData options: NSJSONReadingMutableContainers error: &e];  //I am using sbjson to parse
    NSLog(@"data %@",jsonArray);  //here is your output

    //show controller with navigation


To find Unique Device id iPhone or iPad

UIDevice *myDevice = [UIDevice currentDevice];
NSString *deviceUDID = [myDevice uniqueIdentifier];

NSLog(@"currentDevice %@",deviceUDID);