Search This Blog

Tuesday, 8 April 2014

Check Email validation

- (BOOL)validateEmailWithString:(NSString*)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
    

}

ASIFormDataRequest UPLOAD IMAGE TO THE SERVER

NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        
        [dic setValue:@"Abdul Kareem" forKeyPath:@"user_name"];
        [dic setValue:@"signup" forKeyPath:@"action"];        
        [dic setValue:@"a.kareem.tn@gmail.com" forKeyPath:@"email"];
        [dic setValue:@"abc" forKeyPath:@"password"];
        [dic setValue:@"28" forKeyPath:@"age"];
        [dic setValue:@"Male" forKeyPath:@"gender"];
        [dic setValue:@"80" forKeyPath:@"weight"];
        [dic setValue:@"6" forKeyPath:@"height"];

        NSString *jsonRequest = [dic JSONRepresentation];
        jsonRequest =[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVIEC_URL,jsonRequest]];

        ASIFormDataRequest *request =[ASIFormDataRequest requestWithURL:url];

        [request setRequestMethod:@"POST"];
        
        [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
        [request addRequestHeader:@"Content-Type" value:@"application/json"];
        //[request appendPostData:[jsonRequest  dataUsingEncoding:NSUTF8StringEncoding]];

        if (imageData != nil) { ///for image data
            [request setData:imageData withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:@"picture"];
        }
       
        [request setDelegate:self];
        [request setShowAccurateProgress:YES];
        
        [request setUsername:"user name if you have on webservice"];
        [request setPassword:"password if you have on webservice"];

        [request startAsynchronous];

Thursday, 27 March 2014

SIMPLE PROTOCOL & DELEGATE EXAMPLE

PROTOCOL & DELEGATE
two controllers

FirstViewController
SecondViewController

SecondViewController.h
@protocol SecondViewControllerDelegate;
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;

@protocol SecondViewControllerDelegate <NSObject>

- (void)SecondViewController:(SecondViewController*)viewController
             didChooseValue:(CGFloat)value;

SecondViewController.m
-(IBAction)SecondTap:(id)sender
{
    id<SecondViewControllerDelegate> strongDelegate = self.delegate;
    
    // Our delegate method is optional, so we should
    // check that the delegate implements it
    
    if ([strongDelegate respondsToSelector:@selector(SecondViewController:didChooseValue:)]) {
        [strongDelegate SecondViewController:self didChooseValue:[sender tag]];
    }
}

FirstViewController.h

@interface FirstViewController : UIViewController<SecondViewControllerDelegate>

FirstViewController.m

-(IBAction)Add:(id)sender
{
    SecondViewController *objsecond =[[SecondViewController alloc]init];
    [objsecond setDelegate:self];
    [self.view addSubview:objsecond.view];

}

- (void)SecondViewController:(SecondViewController *)viewController didChooseValue:(CGFloat)value
{
    
    // Do something with value...
    
    // ...then dismiss the child view controller
    [viewController.view removeFromSuperview];

}

Tuesday, 18 March 2014

Generate PEM certificate from P12 certificate

Open terminal and go to the path where you place p12 certificate and run this complete below command:

openssl pkcs12 -in certificateName.p12 -out certificateName.pem -nodes

iOS Timer Counter !!


Here is the Timer counter simple example in which we will get seconds, minutes, and hours

Call the below method TimerCounter:

int secondsSpent;
NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(TimeCounter) userInfo:nil repeats:YES];

- (void)TimeCounter
{
    secondsSpent ++ ;
    hours = secondsSpent / 3600;
    minutes = (secondsSpent % 3600) / 60;
    seconds = (secondsSpent %3600) % 60;
    NSLog(@"Time spent: %02d:%02d:%02d", hours, minutes, seconds);

}

iOS Local Notifications

Here we have sample code for local notification

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody =@"Test Message";
    notification.soundName = UILocalNotificationDefaultSoundName;
    

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

How null,nill, and lenght 0 object can be checked ???

The simplest way to check NSNull value , nill value, and lenght =0 of object:

static inline BOOL IsEmpty(id object)
{
    return object == nil
    || ([object respondsToSelector:@selector(length)]
        && [(NSData *) object length] == 0)
    || ([object respondsToSelector:@selector(count)]
        && [(NSArray *) object count] == 0)
    || ([object respondsToSelector:@selector(count)]
        && object == nil)
    || ([object respondsToSelector:@selector(count)]
        && [ object isKindOfClass:[NSNull class]]);
    

}


How to call inline function:

 NSString *str =nil;
    
    if(!(IsEmpty(str)))
    {
        NSLog(@"The object is not null, nill,");

    }