Friday 28 November 2014

NSTimer issue in iOS8 or you are finding alternative of NSTimer


// method in every one second execute recursive "methodWhichYouWantToRecursiveCall"
[self timer];

// methodWhichYouWantToRecursiveCall calls in every 1 sec
- (void)timer {
    __weak id weakSelf = self;
    __block void (^timer)(void) = ^{
        
        double delayInSeconds = 1.0; // time for recursive call
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            
            id strongSelf = weakSelf;
            
            if (!strongSelf) {
                return;
            }
            
            // Schedule the timer again
            [self timer];
            
            // Always use strongSelf when calling a method or accessing an iVar
            [self methodWhichYouWantToRecursiveCall];
        });
    };
    
    // Start the timer for the first time
    timer();
}

Wednesday 26 November 2014

Objective-C iOS/ iPhone: Create thread safe singleton reference object

// Thread safe option, always keeps a single object
// dispatch_once is used as  critical section for multiple thread
+ (instancetype)sharedManager
{
    static PhotoManager *sharedPhotoManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedPhotoManager = [[PhotoManager alloc] init];
        sharedPhotoManager->_photosArray = [NSMutableArray array];
    });
    return sharedPhotoManager;
}

// Thread unsafe option, which is usually we used
+ (instancetype)sharedManager    
{
    static PhotoManager *sharedPhotoManager = nil;
    if (!sharedPhotoManager) {
        sharedPhotoManager = [[PhotoManager alloc] init];
        sharedPhotoManager->_photosArray = [NSMutableArray array];
    }
    return sharedPhotoManager;
}

Wednesday 19 November 2014

iOS iPhone set status bar in white color {default is black}

Add this line in Info.plist
View controller-based status bar appearance  and set value : NO

Then add this line in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    return YES;
}

URL encoding in iPhone programming (iOS Progarmming) using NSString Category ARC enabled

@implementation NSString (URLEncoding)

- (NSString *)encodeString//:(NSStringEncoding)encoding
{
    
    return (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self,
                                                                                  NULL, (CFStringRef)@";/?:@&=$+{}<>,",
                                                                                  CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
}

@end

Thursday 6 November 2014

free iphone books and resources

http://www.scoop.it/t/ios-books-by-ivan-smirnov

Simple, interactive notifications in iOS 8

With iOS 8 came an exciting new API for creating interactive notifications. These allow you to provide additional functionality to your users outside of your application. I found a lack of clear examples online so I thought I would do a post to show you how easy it is to implement. Here is an example.

Monday 3 November 2014

Assertion failure in -[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:]

Assertion failure in -[UIPickerTableView _createPreparedCellForGlobalRow:withIndexPath:]
You are thinking that Ohh! what the hell is going on. App crashing at some other place, which could not happen. Oh I didn't use UITableView. What is wrong now. Then you can go with simple 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view layoutIfNeeded];
} 

What happen If it not works, then try 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view layoutIfNeeded];

    picker.frame = datePickerTargetFrame;
    [self.view addSubview:picker];


}


IF YOU WANT TO LEARN ABOUT MORE WHAT IS THE MAGIC IN layoutIfNeeded THEN READ PREVIOUS ARTICLE.

How is layoutIfNeeded used?

layoutIfNeeded forces the receiver to layout its subviews immediately if required.
Suppose you have overridden layoutSubviews, and UIKit feels that your view requires layout for whatever reason (e.g. you called setNeedsLayout when handling some user action). Then, your custom layoutSubviews method will be called immediately instead of when it would normally be called in the regular UIKit run loop event sequence (after event handling, but before drawRect:).
An example of why you might need to call layoutIfNeeded within a single run loop:
  1. You resize a custom view containing a table view with a custom layout. setNeedsLayout is set so that layoutSubviews will be called later.
  2. A controller object asks the table view to scroll to some particular cell when handling a user event.
  3. Your custom view performs some custom sizing of the table view in layoutSubviews that changes the table view size.
The problem is when the controller asked the table view to scroll (step 2), the table view had bounds that were stale. The updated bounds would only be set on the table view later (step 3). What the controller wanted the table view to scroll to may not actually be visible after layoutSubviews is done. A solution then would be for the controller to call layoutIfNeeded in situations where it knows this might occur.

Saturday 1 November 2014

Take screenshot of server screen and send this to request browser.

Welcome to fun programming. Here we are creating a socket to a simple http web server. This program is taking screen shot of server system to client request browser. If I wanna see my friend screen and my friend is running this program then I just type his IP:8989 on browser and can see his current screen. Improvement of this program as continuous  streaming is home assignment for you. Thanks to Vikram.