Wednesday, 14 May 2014

Run timer in background thread

ViewController.H


@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;

ViewController.M

self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"Background handler called. Not running background taxsks anymore.");
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countup)userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    });

-(void)countup { 
  //Here i done another action run in main thread
 dispatch_async(dispatch_get_main_queue(), ^{  
               ViewController *Vc=[self.storyboard instantiateViewControllerWithIdentifier:@"view"];
                [self.navigationController pushViewController:Vc animated:YES];
            });
}

Assign placeholder image in tableview Cell(Load image asynchrous)

if u Load original image from server,it will take time at that time we show a another image still that original image loaded:


NSURL *imageURL = [NSURL URLWithString:Details.imageUrl];
    cell.imageView.image=[UIImage imageNamed:@"flower.png"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSData *data = [NSData dataWithContentsOfURL:imageURL];
        
        if (data) {
            UIImage *offersImage = [UIImage imageWithData:data];
            if (offersImage) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
                    
                    if (updateCell) {
                        updateCell.imageView.image = offersImage;
                    }
                });
            }
        }

    });

Tuesday, 13 May 2014

show html file in webview

 NSString * localHtmlFilePath =[[NSBundle mainBundle]pathForResource:@"frnd " ofType:@"html"];
    NSString *html = [NSString stringWithContentsOfFile:localHtmlFilePath encoding:NSUTF8StringEncoding error:nil];

   [webview loadHTMLString:html baseURL:nil];

Navigation concepts


//Change the Title Image in  navigation bar:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flower.png"]];

//Checking version 6 or 7 for changing background color of navigation bar
 NSString *version = [[UIDevice currentDevice] systemVersion];
    if ([version floatValue] < 7) { 
        self.navigationController.navigationBar.barTintColor=[UIColor blackColor];
    } else {
        self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    }

//Hide the back button

    [self.navigationItem setHidesBackButton:YES animated:YES];

//Create custom Back button:
UIButton* customBackButton = [UIButton buttonWithType:101];
    [customBackButton setTitle:@"Back" forState:UIControlStateNormal];
    [customBackButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem    *myBackButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
    self.navigationItem.backBarButtonItem=myBackButton;

//Create Right bar button: (more than one button)
UIBarButtonItem* allFriends = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"frnds.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(allfrnd:)];
    
    UIBarButtonItem* setting = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"options.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(setting)];

    self.navigationItem.rightBarButtonItems=@[setting,allFriends];



//Programmatically pushing a root view controller it embedded with navigation controller
ViewController *user=[self.storyboard instantiateViewControllerWithIdentifier:@"user"];
                UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:user];
                [self.navigationController presentViewController:navigationController animated:YES completion:nil];

Get current Location using googlemaps

[GMSServices provideAPIKey:@"AIzaSyAemkuHXhydxhR286ECVVtspCVTstmgBQM"]; // This key is used for Google Maps.

    // Codes to start tracking the location of the user.
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    return YES;
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation *clLocation = [locations lastObject];
    NSLog(@"Current latitude %f",clLocation.coordinate.latitude);
    NSLog(@"Current longitude %f",clLocation.coordinate.longitude);

}


Refer:https://developers.google.com/maps/documentation/ios/start

Convert TimeStamp into date

    double time=[timeStamp doubleValue]/1000;
    NSTimeInterval _interval=time;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd/MM/yyyy hh:mm"];

    NSString *dateString=[formatter stringFromDate:date];