Wednesday, December 31, 2014

Core Graphics Drawing



1.)Drawing a Line Segment
----------------------------------
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextMoveToPoint(context, 50, 200);
CGContextAddLineToPoint(context,100,100);
CGContextMoveToPoint(context, 50, 200);
CGContextAddLineToPoint(context,200,400);
CGContextStrokePath(context);


2.)Drawing a Filled Circle
--------------------------------
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 255, 0, 255, 0.9);
CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, 0.9);
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 50, 50));


3.)Drawing a Hollow Circle
----------------------------------
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 255, 0, 255, 0.9);
CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, 0.9);
CGContextStrokeEllipseInRect(contextRef, CGRectMake(200, 100, 50, 50));


4.)Drawing a circle with boundary
------------------------------------------
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0, 255, 0, 1);
CGContextFillEllipseInRect(context, CGRectMake(100, 200, 25, 25));
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextStrokeEllipseInRect(context,CGRectMake(100, 200, 25, 25));


5.)Drawing a Hollow Rectangle
---------------------------------------
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 255, 255, 0, 1);
CGContextStrokeRect(ctx, CGRectMake(195, 195, 60, 60));


6.)Drawing a Solid Rectangle
------------------------------------
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 255, 255, 0, 1);
CGContextFillRect(ctx, CGRectMake(260, 195, 60, 60));


7.)Drawing a Triangle
----------------------------
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 255, 0, 255, 1);
CGPoint points[6] = { CGPointMake(200, 200), CGPointMake(250, 250),
CGPointMake(250, 250), CGPointMake(100, 250),
CGPointMake(100, 250), CGPointMake(200, 200) };
CGContextStrokeLineSegments(ctx, points, 6);







Tuesday, December 30, 2014

Resize Image using ImageIO.framework


-(UIImage*)resizeImageToMaxSize:(CGFloat)max
{
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], NULL);
    if (!imageSource)
        return nil;

    CFDictionaryRef options = (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
        (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
        (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
        (id)[NSNumber numberWithFloat:max], (id)kCGImageSourceThumbnailMaxPixelSize,
        nil];
    CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);

    UIImage* scaled = [UIImage imageWithCGImage:imgRef];

    CGImageRelease(imgRef);
    CFRelease(imageSource);

    return scaled;
}

Blocks in UITableViewCell


@interface MyTableViewCell

@property(nonatomic, copy) void (^checkboxHandler)(void);

@end


@implementation MyTableViewCell

- (IBAction)checkboxPressed:(UIButton *)sender {
self.checkboxHandler();
}

@end



@implementation MyTableViewController

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"cell"
    forIndexPath:indexPath;
    cell.checkboxHandler = ^{
    // Perform the desired work in response to checkbox
    };
    return cell;
}

@end


ImageIO.framework Example

#import <ImageIO/ImageIO.h>

NSURL *imageFileURL = [NSURL fileURLWithPath:...];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
    // Error loading image
    ...
    return;
}

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
                         nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
if (imageProperties) {
    NSNumber *width = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
    NSNumber *height = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
    NSLog(@"Image dimensions: %@ x %@ px", width, height);
    CFRelease(imageProperties);
}
CFRelease(imageSource);

========================================================

CFDictionaryRef exif = CFDictionaryGetValue(imageProperties, kCGImagePropertyExifDictionary);
if (exif) {
  NSString *dateTakenString = (NSString *)CFDictionaryGetValue(exif, kCGImagePropertyExifDateTimeOriginal);
  NSLog(@"Date Taken: %@", dateTakenString);
}

CFDictionaryRef tiff = CFDictionaryGetValue(imageProperties, kCGImagePropertyTIFFDictionary);
if (tiff) {
    NSString *cameraModel = (NSString *)CFDictionaryGetValue(tiff, kCGImagePropertyTIFFModel);
    NSLog(@"Camera Model: %@", cameraModel);
}

CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary);
if (gps) {
    NSString *latitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitude);
    NSString *latitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitudeRef);
    NSString *longitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitude);
    NSString *longitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitudeRef);
    NSLog(@"GPS Coordinates: %@ %@ / %@ %@", longitudeString, longitudeRef, latitudeString, latitudeRef);
}


=======================================================

Date Taken: 2011:03:27 11:30:30
Camera Model: Canon EOS 20D
GPS Coordinates: 8.374788 E / 54.89472 N



Wednesday, December 24, 2014

Round two corners of UIView


-(void) setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners withRadius:(CGFloat)radius;
{
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];

    view.layer.mask = shape;
}


[self setMaskTo:view1 byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft withRadius:20.0];

CABasicAnimation Examples



UIView *AnimView1=[[UIView alloc] initWithFrame:CGRectMake(30, 50, 50, 50)];
AnimView1.backgroundColor=[UIColor greenColor];
[self.view addSubview:AnimView1];

UIView *AnimView2=[[UIView alloc] initWithFrame:CGRectMake(90, 75, 50, 50)];
AnimView2.backgroundColor=[UIColor orangeColor];
[self.view addSubview:AnimView2];

UIView *AnimView3=[[UIView alloc] initWithFrame:CGRectMake(170, 90, 50, 50)];
AnimView3.backgroundColor=[UIColor yellowColor];
[self.view addSubview:AnimView3];

UIView *AnimView4=[[UIView alloc] initWithFrame:CGRectMake(240, 150, 50, 50)];
AnimView4.backgroundColor=[UIColor blueColor];
[self.view addSubview:AnimView4];

//shrink
CABasicAnimation* shrink = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
shrink.toValue = [NSNumber numberWithDouble:0.5];
shrink.duration = 0.5;
shrink.delegate = self;
shrink.repeatCount=INFINITY;
shrink.autoreverses=YES;
[[AnimView1 layer] addAnimation:shrink forKey:@"shrinkAnim"];

//moving
CABasicAnimation *Animation1;
Animation1=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
Animation1.duration=0.2;
Animation1.repeatCount=INFINITY;
Animation1.autoreverses=YES;
Animation1.fromValue=[NSNumber numberWithFloat:0];
Animation1.toValue=[NSNumber numberWithFloat:-10];
[[AnimView2 layer] addAnimation:Animation1 forKey:@"shakeAnim"];

//rotating
CABasicAnimation *Animation2;
Animation2=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
Animation2.duration=0.2;
Animation2.repeatCount=INFINITY;
Animation2.autoreverses=YES;
Animation2.fromValue=[NSNumber numberWithFloat:0];
Animation2.toValue=[NSNumber numberWithFloat:M_PI/4];
[[AnimView3 layer] addAnimation:Animation2 forKey:@"rotateAnim"];

//fade
CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation3.duration=0.5;
animation3.repeatCount=INFINITY;
animation3.autoreverses=YES;
animation3.fromValue=[NSNumber numberWithFloat:1];
animation3.toValue=[NSNumber numberWithFloat:0];
[[AnimView4 layer] addAnimation:animation3 forKey:@"fadeAnim"]; 


Highlight Text in UIWebView using JavaScript:


[webView stringByEvaluatingJavaScriptFromString:@"var range = window.getSelection().getRangeAt(0);"
    @"var selectionContents = range.extractContents();"
    @"var span = document.createElement('span');"
    @"span.style.backgroundColor = 'yellow';"
    @"span.appendChild(selectionContents);"
    @"range.insertNode(span)" ];


Text field accept user required characters only


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

    NSString *acceptedCharacters=@"AEIOU12345"; // only these characters  will be allowed to be displayed in text filed.
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:acceptedCharacters] invertedSet];

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    return [string isEqualToString:filtered];
}


Taking screenshot programmatically in iOS 7



UIScreen *screen = [UIScreen mainScreen] ;
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

UIView *view = [screen snapshotViewAfterScreenUpdates:YES];

UIGraphicsBeginImageContextWithOptions(screen.bounds.size, NO, 0);
[keyWindow drawViewHierarchyInRect:keyWindow.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *data= UIImagePNGRepresentation(image);
[data writeToFile:[NSString stringWithFormat:@"%@/ScreenShot.png",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]] atomically:YES];



UIActivityIndicatorView at the Center of View with AutoLayout



UIActivityIndicatorView at the Center of View with AutoLayout


 UIActivityIndicatorView  *spinner;
spinner =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setTranslatesAutoresizingMaskIntoConstraints:NO];
spinner.color=[UIColor blackColor];
    [self.view addSubview:spinner];

 NSLayoutConstraint *cont = [NSLayoutConstraint constraintWithItem:spinner attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
    [self.view addConstraint:cont];

 cont = [NSLayoutConstraint constraintWithItem:spinner attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];

    [self.view addConstraint:cont];

Display HTML with NSAttributedString



NSString *html = @"Wow! Now iOS can create

NSAttributedString

from HTMLs!";
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};

NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];

NSData *htmlData = [attrString dataFromRange:NSMakeRange(0, [attrString length]) documentAttributes:options error:nil];
NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

============================================================


NSString *htmlString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                      documentAttributes:nil error:nil];
                     
                     

Detect blinks and smiles with CoreImage


UIImage *image = [UIImage imageNamed:@"myImage"];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                          context:nil
                                          options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];

NSDictionary *options = @{ CIDetectorSmile: @YES, CIDetectorEyeBlink: @YES };

NSArray *features = [detector featuresInImage:image.CIImage options:options];

for (CIFaceFeature *feature in features) {
    NSLog(@"Bounds: %@", NSStringFromCGRect(feature.bounds));

    if (feature.hasSmile) {
    NSLog(@"Nice smile!");
    } else {
    NSLog(@"Why so serious?");
    }
    if (feature.leftEyeClosed || feature.rightEyeClosed) {
    NSLog(@"Open your eyes!");
    }
}

Base64 Encoding and Decoding



NSData* sampleData = [@"Some sample data" dataUsingEncoding:NSUTF8StringEncoding];


Base64 Encoding:
================
NSString * base64String = [sampleData base64EncodedStringWithOptions:0];
NSLog(@"Base64-encoded string is %@", base64String); // prints "U29tZSBzYW1wbGUgZGF0YQ=="


Base64 Decoding:
================
NSData* dataFromString = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSLog(@"String is %@",[NSString stringWithUTF8String:[dataFromString bytes]]); // prints "Some sample data"

Blur Effect using CoreImage


Add CoreImge.framework


UIImage *theImage = [UIImage imageNamed:@"Sample.png"];

    //create our blurred image
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage];

    //setting up Gaussian Blur (we could use one of many filters offered by Core Image)
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"];
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    //CIGaussianBlur has a tendency to shrink the image a little, this ensures it matches up exactly to the bounds of our original image
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

self.imageView.image=[UIImage imageWithCGImage:cgImage];

CoreImage Filters


First you need to import and link with the CoreImage.framework

UIImage* inputImage = [UIImage imageNamed:@"image.jpg"];
CIImage* filterInputImage = [CIImage imageWithCGImage:inputImage.CGImage];

CIFilter* filter = [CIFilter filterWithName:@"CIDotScreen"];
[filter setValue:filterInputImage forKey:kCIInputImageKey];

CIImage* filterOutputImage = filter.outputImage;

To make it back into a UIImage, a CIContext needs to be used, as so

CIContext* ctx = [CIContext contextWithOptions:nil];
CGImageRef createdImage = [ctx createCGImage:filterOutputImage fromRect:filterOutputImage.extent];

UIImage* outputImage = [UIImage imageWithCGImage:createdImage];

CGImageRelease(createdImage);
createdImage = nil;


==========================================================

NSLog(@"%@", [CIFilter filterNamesInCategory:kCICategoryBuiltIn]);

(
    CIAdditionCompositing,
    CIAffineClamp,
    CIAffineTile,
    CIAffineTransform,
    CIBarsSwipeTransition,
    CIBlendWithMask,
    CIBloom,
    CIBumpDistortion,
    CIBumpDistortionLinear,
    CICheckerboardGenerator,
    CICircleSplashDistortion,
    CICircularScreen,
    CIColorBlendMode,
    CIColorBurnBlendMode,
    CIColorControls,
    CIColorCube,
    CIColorDodgeBlendMode,
    CIColorInvert,
    CIColorMap,
    CIColorMatrix,
    CIColorMonochrome,
    CIColorPosterize,
    CIConstantColorGenerator,
    CICopyMachineTransition,
    CICrop,
    CIDarkenBlendMode,
    CIDifferenceBlendMode,
    CIDisintegrateWithMaskTransition,
    CIDissolveTransition,
    CIDotScreen,
    CIEightfoldReflectedTile,
    CIExclusionBlendMode,
    CIExposureAdjust,
    CIFalseColor,
    CIFlashTransition,
    CIFourfoldReflectedTile,
    CIFourfoldRotatedTile,
    CIFourfoldTranslatedTile,
    CIGammaAdjust,
    CIGaussianBlur,
    CIGaussianGradient,
    CIGlideReflectedTile,
    CIGloom,
    CIHardLightBlendMode,
    CIHatchedScreen,
    CIHighlightShadowAdjust,
    CIHoleDistortion,
    CIHueAdjust,
    CIHueBlendMode,
    CILanczosScaleTransform,
    CILightenBlendMode,
    CILightTunnel,
    CILinearGradient,
    CILineScreen,
    CILuminosityBlendMode,
    CIMaskToAlpha,
    CIMaximumComponent,
    CIMaximumCompositing,
    CIMinimumComponent,
    CIMinimumCompositing,
    CIModTransition,
    CIMultiplyBlendMode,
    CIMultiplyCompositing,
    CIOverlayBlendMode,
    CIPinchDistortion,
    CIPixellate,
    CIRadialGradient,
    CIRandomGenerator,
    CISaturationBlendMode,
    CIScreenBlendMode,
    CISepiaTone,
    CISharpenLuminance,
    CISixfoldReflectedTile,
    CISixfoldRotatedTile,
    CISmoothLinearGradient,
    CISoftLightBlendMode,
    CISourceAtopCompositing,
    CISourceInCompositing,
    CISourceOutCompositing,
    CISourceOverCompositing,
    CIStarShineGenerator,
    CIStraightenFilter,
    CIStripesGenerator,
    CISwipeTransition,
    CITemperatureAndTint,
    CIToneCurve,
    CITriangleKaleidoscope,
    CITwelvefoldReflectedTile,
    CITwirlDistortion,
    CIUnsharpMask,
    CIVibrance,
    CIVignette,
    CIVortexDistortion,
    CIWhitePointAdjust
)


Interactive Notifications in iOS 8


In iOS 8 developers can now present interactive notifications in their apps.

To do so, first you need to register all possible notification “categories”. A category is defined by a set of actions. In my example I’m going to setup a category with reply and delete actions

Each action is shown to the user as a button. Actions have two possible activation modes: foreground or background. If your app can perform the action with no further interaction from the user then you want to use the background activation mode. If the action needs further user input then you can set the activation mode to foreground to have your app launched when actioned, for example, presenting a keyboard in your app when they reply button is tapped.




UIMutableUserNotificationAction* deleteAction = [[UIMutableUserNotificationAction alloc] init];
[deleteAction setIdentifier:@"delete_action_id"];
[deleteAction setTitle:@"Delete"];
[deleteAction setActivationMode:UIUserNotificationActivationModeBackground];
[deleteAction setDestructive:YES];

UIMutableUserNotificationAction* replyAction = [[UIMutableUserNotificationAction alloc] init];
[replyAction setIdentifier:@"reply_action_id"];
[replyAction setTitle:@"Reply"];
[replyAction setActivationMode:UIUserNotificationActivationModeForeground];
[replyAction setDestructive:NO];

UIMutableUserNotificationCategory* deleteReplyCategory = [[UIMutableUserNotificationCategory alloc] init];
[deleteReplyCategory setIdentifier:@"custom_category_id"];
[deleteReplyCategory setActions:@[replyAction, deleteAction] forContext:UIUserNotificationActionContextDefault];


NSSet* categories = [NSSet setWithArray:@[deleteReplyCategory]];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];


Now we can schedule a notification to use the previously setup category. This is the string identifier from before. This notification is scheduled after 10 seconds.

UILocalNotification* notification = [[UILocalNotification alloc] init];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[notification setAlertBody:@"Somebody sent you a message"];
[notification setCategory:@"custom_category_id"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];


When the user selects an option we get a callback to the app delegate.


- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
    if([notification.category isEqualToString:@"custom_category_id"])
    {
        if([identifier isEqualToString:@"delete_action_id"])
        {
            NSLog(@"Delete was pressed");
        }
        else if([identifier isEqualToString:@"reply_action_id"])
        {
            NSLog(@"Reply was pressed");
        }
    }
   
    //    Important to call this when finished
    completionHandler();
}






-viewWillAppear in iOS 7.0



- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    const BOOL isInteractivePop = (self.navigationController.interactivePopGestureRecognizer.state == UIGestureRecognizerStateBegan);

    if(isInteractivePop)
    {
        //    Interactive pop stuff
    }
    else
    {
        //    Back button stuff
    }
}

iOS7 interactivePopGestureRecognizer for UINavigationController with hidden navigation bar

iOS7 introduces interactivePopGestureRecognizer property which is used for popping current view controller stack by a gesture (swipe right gesture from left edge as default). However if the navigation bar is hidden or the app uses a custom back button for navigation bar, this feature will not work. A bit lines of code can make this feature works again for those cases. In viewDidLoad() of view controller (or application:didFinishLaunchingWithOptions: of AppDelegate), we set the interactivePopGestureRecognizer.delegate to nil as below:

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
  self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}


interactivePopGestureRecognizer in UINavigationController



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Disable iOS 7 back gesture
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // Enable iOS 7 back gesture
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}

Tuesday, December 23, 2014

iPhone UI Designs Tips:


http://www.lovelyui.com/

http://www.mobile-patterns.com/

http://www.pttrns.com/

Cocoapods


Installing/Updating Cocoapods on MacOSX:
=======================================
$sudo gem install cocoapods

$sudo gem update --system



Using Cocoapods:
================
1. Create a 'Podfile' and store in xcode project root folder
2. Write pod '{pod_module}' in the created Podfile.
3. Then from the Terminal command line execute the file as
    $pod install
4. The above command will create an ProjectName.xworkspace file. Use that file instead of ProjectName.xcodeproj file.


  
Contents of Podfile:
====================
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

pod 'AFNetworking', '~> 2.0'
pod 'ARAnalytics', '~> 2.7'


#import <Reachability/Reachability.h>

QRCode Generator


- (void)setUIElementsAsEnabled:(BOOL)enabled
{
    self.generateButton.enabled = enabled;
    self.stringTextField.enabled = enabled;
}

- (CIImage *)createQRForString:(NSString *)qrString
{
    // Need to convert the string to a UTF-8 encoded NSData object
    NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];

    // Create the filter
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // Set the message content and error-correction level
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

    // Send the image back
    return qrFilter.outputImage;
}


- (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image withScale:(CGFloat)scale
{
    // Render the CIImage into a CGImage
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:image.extent];

    // Now we'll rescale using CoreGraphics
    UIGraphicsBeginImageContext(CGSizeMake(image.extent.size.width * scale, image.extent.size.width * scale));
    CGContextRef context = UIGraphicsGetCurrentContext();
    // We don't want to interpolate (since we've got a pixel-correct image)
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    // Get the image out
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // Tidy up
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    return scaledImage;
}


- (IBAction)handleGenerateButtonPressed:(id)sender {
    // Disable the UI
    [self setUIElementsAsEnabled:NO];
    [self.stringTextField resignFirstResponder];

    // Get the string
    NSString *stringToEncode = self.stringTextField.text;

    // Generate the image
    CIImage *qrCode = [self createQRForString:stringToEncode];

    // Convert to an UIImage
    UIImage *qrCodeImg = [self createNonInterpolatedUIImageFromCIImage:qrCode withScale:2*[[UIScreen mainScreen] scale]];

    // And push the image on to the screen
    self.qrImageView.image = qrCodeImg;

    // Re-enable the UI
    [self setUIElementsAsEnabled:YES];
}

Sunday, December 21, 2014

SecItemAdd Example


Storing in Keychain:
===============
NSData *secret = [@"top secret" dataWithEncoding: NSUTF9StringEncoding];
NSDictionary *query = @{
    (id)kSecClass : (id)kSecClassGenericPassword,
    (id)kSecAttrService : @"myservice",
    (id)kSecAttrAccount : @"account name here",
    (id)kSecValueData : secret
};

OSStatus status = SecItemAdd((CFDictionaryRef)query, NULL);



Retrieving from Keychain:
====================
NSDictionary *query = @{
    (id)kSecClass : (id)kSecClassGenericPassword,
    (id)kSecAttrService : @"myservice",
    (id)kSecAttrAccount : @"account name here",
    (id)kSecReturnData : @YES
};

NSData *data = NULL;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef*)&data);


KeychainItemWrapper Example

Add KeychainItemWrapper.h/.m from Apple

https://developer.apple.com/library/ios/samplecode/GenericKeychain/Introduction/Intro.html

KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"KeychainTest" accessGroup:nil];
[keychain setObject:kSecAttrAccessibleWhenUnlocked forKey:kSecAttrAccessible];

NSLog(@"%@, %@", [keychain objectForKey:kSecAttrAccount], [keychain objectForKey:kSecValueData]);

[keychain setObject:@"example@email.com" forKey:kSecAttrAccount];
[keychain setObject:@"MySuperSecretPassword" forKey:kSecValueData];

[keychain release];
keychain = nil;

Tuesday, December 16, 2014

Hello World in node.js


var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello Http');
});
server.listen(8080);

node.js plist

/*------------------------------
1. npm install plist
2. sudo npm install express
3. node node.js
--------------------------------*/

var plist = require('plist'),
    express = require('express')

var host = "127.0.0.1"
var port = 8080

var app = express()

app.get("/", function(request, response) {
        response.send(plist.build(
            {
                'Greeting': "Hello, World",
                'Price': 4.20,
                'FeatureXIsLaunched': true,
                'Status': 1
            }
        ).toString())
})

app.listen(port, host)


Monday, December 15, 2014

Fetching from Multiple Entities in CoreData:


@interface Employee : NSManagedObject

@property (nonatomic, retain) NSString * dept;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Department *deptEmp;

@end


@interface Department : NSManagedObject

@property (nonatomic, retain) NSString * location;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Employee *deptEmp1;

@end

//==========================================================


NSMutableString *queryString = [NSMutableString stringWithFormat:@"deptEmp1.name == %@ AND deptEmp1.location == %@", @"Apple", @"Cupertino"];

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"Department",nil]];
[request setIncludesSubentities:YES];

NSArray* returnArray = [self.managedObjectContext executeFetchRequest:request error:&error];
if([returnArray count] > 0) {

   Employee* emp = [returnArray objectAtIndex:0];
   NSLog(@"%@ %@ %@", emp.name, emp.dept, emp.deptEmp.location);
}


Sunday, December 14, 2014

Core Data Migration:


1. Create a new version of the core data.
2. Add/Update the changes in the new core data model.
3. Set the new core data model as active in xcode.
4. Add the following options dictionary to the Persistance store in AppDelegate.

  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                           [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,  nil];

Friday, December 12, 2014

Cancel UILocalNotification:

NSString *myIDToCancel = @"some_id_to_cancel";
UILocalNotification *notificationToCancel=nil;
for(notificationToCancel in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
  if([[notificationToCancel.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) {
     break;
  }
}
[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

UILocalNotification in Background

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    __block UIBackgroundTaskIdentifier bgTask ;
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
    pollingTimer2 = [NSTimer scheduledTimerWithTimeInterval:4.0f target:self  selector:@selector(process)  userInfo:nil repeats:YES];
}
-(void) process
{
   [self didLocalNotification];

}

-(void)didLocalNotification
{

    [[UIApplication sharedApplication]cancelAllLocalNotifications];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (localNotification == nil)
    {
        return;
    }

    NSLog(@"calling didLocalNotification");
    localNotification.applicationIconBadgeNumber =0;
    localNotification.alertBody =@"Message!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

UILocalNotification Example

UILocalNotification *locNot = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
NSInterval interval;
switch( freqFlag ) {     // Where freqFlag is NSHourCalendarUnit for example
    case NSHourCalendarUnit:
        interval = 60 * 60;  // One hour in seconds
        break;
    case NSDayCalendarUnit:
        interval = 24 * 60 * 60; // One day in seconds
        break;
}
if( every == 1 ) {
    locNot.fireDate = [NSDate dateWithTimeInterval: interval fromDate: now];
    locNot.repeatInterval = freqFlag;
    [[UIApplication sharedApplication] scheduleLocalNotification: locNot];
} else {
    for( int i = 1; i <= repeatCountDesired; ++i ) {
        locNot.fireDate = [NSDate dateWithTimeInterval: interval*i fromDate: now];
        [[UIApplication sharedApplication] scheduleLocalNotification: locNot];
    }
}
[locNot release];

Tuesday, December 9, 2014

Binary Search

 NSArray  *arr = @[@1, @2, @5, @10];

  NSLog(@"arr : %@",arr);
 
  int result = [self searchIndexOfNumber: 5];
  if(result == -1) {
    NSLog(@"Number not found");
  } else {
    NSLog(@"Number found at index : %d", result);
  }


----------------------------------------

-(int)searchIndexOfNumber: (int)number{
  int result = -1;
 
  int lower = 0;
  int upper = (int)[arr count]-1;
 
  while (lower <= upper) {
    int mid = (lower + upper) / 2;
   
    if([arr[mid] integerValue] < number) {
     
      lower = mid + 1;
     
    } else if([arr[mid] integerValue] == number){
     
      result = mid;
      break;
     
    } else {
   
      upper = mid - 1;
    }
   
  }

  return result;
}

Friday, December 5, 2014

EKEventKit Example


To add an event:
================

EKEventStore *store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = @"Event Title";
        event.startDate = [NSDate date]; //today
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
        [event setCalendar:[store defaultCalendarForNewEvents]];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
        NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
    }];
  



To remove an event:
==================

 EKEventStore* store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent* eventToRemove = [store eventWithIdentifier:savedEventId];
        if (eventToRemove) {
            NSError* error = nil;
            [store removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&error];
        }
    }];
  
  
----------------------------------------------


Event With Alarm Example:
=========================

EKEvent *event = [EKEvent eventWithEventStore: eventStore];
event.calendar = eventStore.defaultCalendarForNewEvents;
event.title = @"Some title";
event.allDay = YES;
event.startDate = self.date;
event.endDate = self.date;


EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:0];
event.alarms = @[alarm];

NSError *err = nil;
BOOL saved = [store saveEvent:event span:EKSpanThisEvent error:&err];
if(!saved && err){
    NSLog("Error : %@", [err localizedDescription];
} else {
   int eventId = event.eventIdentifier;
}




Thursday, December 4, 2014

Location Manager Changes in iOS 8.0



Add these in info.plist
---------------------------
NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription


-(void)initLocationManager {
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager setDistanceFilter:100];
  
    if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        //[self.locationManager requestWhenInUseAuthorization]; // Add This Line
    }
  
  
    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
      [self.locationManager requestAlwaysAuthorization]; // Add This Line
    }
  
    [self.locationManager startUpdatingLocation];

}


#pragma mark - LocationManager Delegate

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
  
    NSLog(@"locationManager didUpdateLocations");
 
    [locationManager stopUpdatingLocation];
  
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
  
    [geoCoder reverseGeocodeLocation:[locations objectAtIndex:0] completionHandler:^(NSArray *placemarks, NSError *error) {
      
        CLPlacemark *placemark    = [placemarks objectAtIndex:0];
        NSString* countryName    = placemark.country;
        NSLog(@"Country Name: %@",countryName);
      
        self.countryNameString    = countryName;
    
    
        NSString *countryCode    = placemark.ISOcountryCode;

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: countryName, @"countryName",
                              countryCode, @"countryCode", nil];
    
    
        if(self.geoLocationDelegate && [self.geoLocationDelegate respondsToSelector:@selector(receivedGeoLocationData:)]) {
          [self.geoLocationDelegate receivedGeoLocationData:dict];
        }
    
    
    }];
  
}


- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSLog(@"Location Manager did fail : %@", [error localizedDescription]);
}

Country Code and Country Name from NSLocale

NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

NSString *country = [usLocale displayNameForKey:NSLocaleCountryCode value:countryCode];