Sunday 20 December 2015

String Utilities

Let's have a look into for String categories with below features,

* Email validation
* Phone number validation
* String empty validation
* Remove number characterset

and more on..

  1. Create a New file in XCode. File -> New -> File (Cmd + N)
  2. Choose "Cocoa Touch Class" 
  3. Create the file name as "NSString+Utilities" and subclass it with NSString
  4. Then, click finish. Your category file is ready.
We are gonna see basic String category methods which are listed below


- (NSString *)removeNumbersFromString;
- (BOOL)isEmailValid;
- (BOOL)isMobileValid;
- (BOOL)validateString;

Let's have a look into "NSString+Utilities.h"

#import <Foundation/Foundation.h>

@interface NSString (StringValidations)

- (NSString *)removeNumbersFromString;
- (BOOL)isEmailValid;
- (BOOL)isMobileValid;
- (BOOL)validateString;


@end

And, your "NSString+Utilities.m"

#import "NSString+Utilities.h"

@implementation NSString (StringValidations)

- (NSString *)removeNumbersFromString
{
    NSString *trimmedString = nil;
    NSCharacterSet *numbersSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    trimmedString = [self stringByTrimmingCharactersInSet:numbersSet];
    return trimmedString;
}

/**
 * Method to validate the email
 * @param email variable from NSString class type
 * @return email returns BOOL result
 */
- (BOOL)isEmailValid {
    NSString *emailRegex = @"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}+\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}+\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

/**
 * Method to validate the email
 * @param number variable from NSString class type
 * @return number returns BOOL result
 */
- (BOOL)isMobileValid {
    NSError *error = NULL;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
    
    NSRange inputRange = NSMakeRange(0, [self length]);
    NSArray *matches = [detector matchesInString:self options:0 range:inputRange];
    
    // no match at all
    if ([matches count] == 0) {
        return NO;
    }
    // found match but we need to check if it matched the whole string
    NSTextCheckingResult *result = (NSTextCheckingResult *)[matches objectAtIndex:0];
    
    if ([result resultType] == NSTextCheckingTypePhoneNumber && result.range.location == inputRange.location && result.range.length == inputRange.length) {
        // it matched the whole string
        return YES;
    }
    else {
        // it only matched partial string
        return NO;
    }
}

- (BOOL)validateString {
    id stringValue = self;
    if ([stringValue length] == 0 && stringValue == [NSNull null] && stringValue == nil)
        return NO;
    else
        return YES;
}

@end

That's it. You can start using these category methods in your ViewControllers just by importing import "NSString+Utilities.h"

Cheers

Thursday 17 December 2015

UIScrollView + UILabel (Dynamic Height) in AutoLayout

Hello Everyone,

This is my first post in Software Development tips blog.

All of us know that Scrollview has a biggest problem while working it with AutoLayout.
ScrollView has ambiguous scrollable content height



Fix this issue through this fantastic answers

iOS Introduction

iOS (originally iPhone OS) is a mobile operating system created and developed by Apple Inc. and distributed exclusively for Apple hardware. It is the operating system that presently powers many of the company's mobile devices, including the iPhoneiPad, and iPod touch. In October 2015, it was the most commonly used mobile operating system, in a few countries, such as in Canada, the United States, the United Kingdom, Norway, Sweden, Denmark, Japan, and Australia, while iOS is far behind Google's Android globally; iOS had a 19.7% share of the smartphone mobile operating system units shipped in the fourth quarter of 2014, behind Android with 76.6%. However, on tablets, iOS is the most commonly used tablet operating system in the world, while it has lost majority in many countries (e.g. the Africa continent and briefly lost Asia).
For More: iOS