Labels

January 10, 2014

Mixing ARC with Non-ARC in iOS

Apple announced support for ARC (Automatic Reference Counting) from iOS 5.

Prior to iOS 5, (i.e. Non-ARC) developer had to keep track of each object allocation manually and release after that object was not needed. Non-ARC also know as MRC (Manual Reference Counting).

ARC is not a runtime mechanism rather, it is a compiler level feature that automatically inserts release/retain statement during the compilation.

Developer can mixed up ARC and MRC files in a project as well as code snippet in a single source file.

  1. Exclude Non-ARC file from being compiled with ARC

    You can do that by setting the flag on the .m file:

    Steps :~
  1. Click the Project
  2. Target
  3. Build Phases Tab
  4. Compile Sources Section
  5. Double Click on the file name and add -fno-objc-arc to the popup window.

Note: If you want to include a file in ARC, you can use the -fobjc-arc flag.


  1. Mixing ARC with Non-ARC code in a source file.

    You can use __has_feature, like this:

    #if __has_feature(objc_arc)
       // ARC is On
       NSLog(@"ARC is On");
    #else
       // ARC is Off
       NSLog(@"ARC is Off");    
    #endif

January 9, 2014

Email address validation in iOS

Background :~


Unlike other technologies, in iOS sometimes we need to verify email address entered by user, especially in registration & login screen. Following function will check the entered email address is valid or not and return the value based on validation.
Implementation :~


Utility.h


#import <Foundation/Foundation.h>


@interface Utility : NSObject


+(BOOL)isValidEmailAddress:(NSString *)emailAddress ;


+(BOOL) validateEmail:(NSString*) emailAddress ;


@end



Utility.m


#import "Utility.h"




@implementation Utility


// Using NSPredicate
+(BOOL)isValidEmailAddress:(NSString *)emailAddress {


   //Create a regex string
   NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" ;


   //Create predicate with format matching your regex string
   NSPredicate *emailTest = [NSPredicate predicateWithFormat:
                             @"SELF MATCHES %@", stricterFilterString];


   //return true if email address is valid
   return [emailTest evaluateWithObject:emailAddress];
}



// Using NSRegularExpression
+(BOOL) validateEmail:(NSString*) emailAddress {
   
   NSString *regExPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";


   NSRegularExpression *regEx = [[NSRegularExpression alloc]
                                 initWithPattern:regExPattern
                                 options:NSRegularExpressionCaseInsensitive
                                 error:nil];



   NSUInteger regExMatches = [regEx numberOfMatchesInString:emailAddress
                                                    options:0
                                                      range:NSMakeRange(0, [emailAddress length])];


   return (regExMatches == 0) ? NO : YES ;
}



How to Use :~


Validate email address by calling any of the above method. For example,


[Utility isValidEmailAddress:@"milan@example.com"]; OR


[Utility validateEmail:@"sam@example.com"];





January 8, 2014

Detect First Run of Application

Background :~

There are some scenarios where we need to check whether our application is running first time or not. If it is running first time then and only then execute that code. For Example,

  • Showing help screen
  • Register device for push notification
  • Database creation
  • Server selection and so on...

Implementation :~


Utility.h


#import <Foundation/Foundation.h>


@interface Utility : NSObject


+ (BOOL)isFirstRun ;


@end



Utility.m


#import "Utility.h"

#define USER_DEFAULTS_FIRST_RUN @"isFirstRun"


@implementation Utility


+ (BOOL)isFirstRun {

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

   if ([defaults objectForKey:USER_DEFAULTS_FIRST_RUN]) {
       return NO;
   }
   
   [defaults setObject:[NSDate date] forKey:USER_DEFAULTS_FIRST_RUN];
   [defaults synchronize];
   
   return YES;

}


How to Use :~


Call “isFirstRun” method from didFinishLaunchingWithOptions from AppDelegate.m class.

AppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
                  :(NSDictionary *)launchOptions {


   //Check is application is running first time of not
   
   if ([[Utility sharedHandler] isFirstRun]) {

       // Application is running first time.
       // Do your stuff here


   }
   return YES;

}