Wednesday, April 8, 2015

[iOS] How to get top most view controller?

+ (UIViewController *)topViewController
{
    return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

+ (UIViewController *)topViewController:(UIViewController *)rootViewController
{
    if (rootViewController.presentedViewController == nil) {
        return rootViewController;
    }
    if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [self topViewController:lastViewController];
    }
    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
    return [self topViewController:presentedViewController];
}

+ (UINavigationController *)navigationController
{
    UIViewController *topViewController = [self topViewController];
    UINavigationController *navigationController = nil;
    if ([topViewController isKindOfClass:[UINavigationController class]])
    {
        navigationController = (UINavigationController *)topViewController;
    }else
    {
        navigationController = topViewController.navigationController;
    }

    return navigationController;
}

[iOS] How to open a native app or Appstore from Safari or UIWebView


1. Promoting App Banner:
smartbanner_2x.png

2. Take user to Appstore from Safari link:

Thursday, March 5, 2015

[iOS] Draw Dashed Border Around View

+ (void)drawDashedBorderAroundView:(UIView *)v
                      cornerRadius:(CGFloat)cornerRadius
                       borderWidth:(CGFloat)borderWidth
                      dashPattern1:(NSInteger)dashPattern1
                      dashPattern2:(NSInteger)dashPattern2
                         lineColor:(UIColor*)lineColor
{
    //border definitions
//    CGFloat cornerRadius = 10;
//    CGFloat borderWidth = 2;
//    NSInteger dashPattern1 = 4;
//    NSInteger dashPattern2 = 8;
//    UIColor *lineColor = [UIColor grayColor];

    CGRect newR = v.frame;
    newR.origin.x += borderWidth/2;
    newR.origin.y += borderWidth/2;
    newR.size.width -= borderWidth;
    newR.size.height -= borderWidth;
    v.frame = newR;

    //drawing
    CGRect frame = v.bounds;

    CAShapeLayer *_shapeLayer = [CAShapeLayer layer];

    //creating a path
    CGMutablePathRef path = CGPathCreateMutable();

    //drawing a border around a view
    CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
    CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
    CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
    CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
    CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
    CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
    CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);

    //path is set as the _shapeLayer object's path
    _shapeLayer.path = path;
    CGPathRelease(path);

    _shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
    _shapeLayer.frame = frame;
    _shapeLayer.masksToBounds = NO;
    [_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
    _shapeLayer.fillColor = [[UIColor clearColor] CGColor];
    _shapeLayer.strokeColor = [lineColor CGColor];
    _shapeLayer.lineWidth = borderWidth;
    _shapeLayer.lineDashPattern = @[@(dashPattern1), @(dashPattern2)];
    _shapeLayer.lineCap = kCALineCapRound;

    //_shapeLayer is added as a sublayer of the view, the border is visible
    [v.layer addSublayer:_shapeLayer];
    v.layer.cornerRadius = cornerRadius;
}