Search This Blog

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,");

    }