Tuesday, July 22, 2014

Running multiple instances of Skype on MacOS


Open a Terminal window on MacOS and type the following:

Step 1: cd /Applications/Skype.app/Contents/MacOS/
Step 2: sudo ./Skype
Step 3: nohup /Applications/Skype.app/Contents/MacOS/Skype

Thursday, July 17, 2014

Google Translate API

+(void)googleTranslateText : (NSString *)inputText
             fromLang : (NSString *)fromLanguage
               toLang : (NSString *)toLanguage
withCompletionHandler : (void (^)(NSString *result))completionHandler {
 
 
  NSString *googleTranslateURLStr = [NSString stringWithFormat:@"https://www.googleapis.com/language/translate/v2?key=%@&source=%@&target=%@&q=%@&prettyprint=true",kGOOGLE_API_KEY, fromLanguage, toLanguage, inputText];
  googleTranslateURLStr = [googleTranslateURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
  NSLog(@"googleTranslateURLStr : %@", googleTranslateURLStr);
 
  NSURL *googleTranslateURL = [NSURL URLWithString:googleTranslateURLStr];
 
  dispatch_queue_t queue    = dispatch_queue_create("com.biranchi.touristGuide", 0);
  dispatch_async(queue, ^{
   
   
    NSData *data = [[NSData alloc] initWithContentsOfURL:googleTranslateURL];
   
//    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    NSLog(@"Google Translated Str : %@", str);
   
   
    NSError *err = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
    //NSLog(@"err : %@", err);
    NSLog(@"JSON Dict : %@", jsonDict);
   
    NSString *translatedText = @"";
    NSArray *translationsArr = [[jsonDict objectForKey:@"data"] objectForKey:@"translations"];
    if(translationsArr && [translationsArr count]){
      translatedText = [[translationsArr objectAtIndex:0] objectForKey:@"translatedText"];
    }
   
    dispatch_async(dispatch_get_main_queue(), ^{
     
      NSString *responseString = [NSString stringWithFormat:@"%@", translatedText];
      completionHandler(responseString);

    });
  });//end
 
}

Friday, July 11, 2014

UIColor from RGB Color


//RGB color macro
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

//RGB color macro with alpha
#define UIColorFromRGBWithAlpha(rgbValue,a) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]


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


self.view.backgroundColor = UIColorFromRGB(0xCECECE);
self.view.backgroundColor = UIColorFromRGBWithAlpha(0xCECECE, 0.8);


Wednesday, July 9, 2014

Reversing Arrays in ObjectiveC


@implementation NSArray (Reverse)

- (NSArray *)reversedArray {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
    NSEnumerator *enumerator = [self reverseObjectEnumerator];
    for (id element in enumerator) {
        [array addObject:element];
    }
    return array;
}

@end

@implementation NSMutableArray (Reverse)

- (void)reverse {
    if ([self count] == 0)
        return;
    NSUInteger i = 0;
    NSUInteger j = [self count] - 1;
    while (i < j) {
        [self exchangeObjectAtIndex:i
                  withObjectAtIndex:j];

        i++;
        j--;
    }
}

@end

Thursday, July 3, 2014

Running Tasks, UILocalNotifications in Background


- (void)applicationDidEnterBackground:(UIApplication *)application {
 
  if ([[UIDevice currentDevice] isMultitaskingSupported]) {
   
    UIApplication *application = [UIApplication sharedApplication];
   
    __block UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
     
      NSLog(@"ExpirationHandler about to expire...");
//      [application endBackgroundTask: background_task];
//      background_task = UIBackgroundTaskInvalid;
    }];
   
   
    NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init];
    [operationQueue addOperationWithBlock:^{
   
      // Perform long-running tasks without blocking main thread
     
      while(TRUE)
      {
        //backgroundTimeRemaining time does not go down.
       
        //NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
       
        UIDevice *device              = [UIDevice currentDevice];
        NSLog(@"Battey Level : %f", (device.batteryLevel));
       
        if(device.batteryLevel == 0.20f) {
          NSLog(@"Should show alert view.....for local notifications");
         
          int currentBatteryLevel      = (int)(device.batteryLevel * 100);

         
          NSDate *now = [NSDate date];
          NSCalendar *calendar = [NSCalendar currentCalendar];
          NSDateComponents *components = [[NSDateComponents alloc] init];
          [components setSecond:5];
          NSDate *dateTime = [calendar dateByAddingComponents:components
                                                              toDate:now
                                                             options:0];
         
         
          UILocalNotification *localNotification = [[UILocalNotification alloc] init];
          localNotification.fireDate = dateTime;
          localNotification.alertBody = [NSString stringWithFormat:@"Battery Level : %d percent", currentBatteryLevel];
          localNotification.soundName = UILocalNotificationDefaultSoundName;
          //localNotification.applicationIconBadgeNumber = 1;
          [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
         
         
            [application endBackgroundTask: background_task];
            background_task = UIBackgroundTaskInvalid;
        }
       
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIDeviceBatteryLevelDidChangeNotification" object:self userInfo:nil];
       
        [NSThread sleepForTimeInterval:10]; //wait for 1 sec

      }
     
    }];
   
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           
      // Wait until the pending operations finish
      [operationQueue waitUntilAllOperationsAreFinished];

     
      NSLog(@"ExpirationHandler is called");

    });
  }
}