Wednesday, April 04, 2012

How to Create a Universally Unique Identifier (UUID)

- (NSString *)CreateUUID
{
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    NSString *uuid = [NSString stringWithString:(NSString *)uuidStringRef];
    CFRelease(uuidStringRef);
    return uuid;
}

How to remind users to Restart App

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
   
    // Current date
    NSDate *date = [NSDate date];
   
    // Add interval to the current time
    NSDate *dateToOpen = [date dateByAddingTimeInterval:timeInterval];
   
    // Set the fire date/time
    [localNotification setFireDate:dateToOpen];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];   
   
    // Setup alert notification
    [localNotification setAlertBody:@"Tap to return to MyApp" ];
    [localNotification setAlertAction:@"Open MyApp"];
    [localNotification setHasAction:YES];
   
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

How to Parse an NSURL Object in iOS

NSURL *url = [NSURL URLWithString:
 @"http://maheswarancm.com:999/2012/April;URLParsing?
                    url=testURL&purpose=testing"];
 
NSLog(@"URL Scheme: %@", [url scheme]); 
NSLog(@"URL Host: %@", [url host]); 
NSLog(@"URL Port: %@", [url port]);     
NSLog(@"URL Path: %@", [url path]);     
NSLog(@"URL Relative path: %@", [url relativePath]);
NSLog(@"URL Path components as array: %@", [url pathComponents]);        
NSLog(@"URL Parameter string: %@", [url parameterString]);   
NSLog(@"URL Query: %@", [url query]);       
NSLog(@"URL Fragment: %@", [url fragment]);
 
output:
URL Scheme: http
URL Host: maheswarancm.com
URL Port: 999
URL Path: /2012/April
URL Relative path: /2012/April
URL Path components as array: (
    "/",
    2012,
    April
)
URL Parameter string: URLParsing
URL Query: url=testURL&purpose=testing
URL Fragment: (null) 

Proximity Sensor use in iPhone

// Enabled monitoring of the sensor
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
 
// Set up an observer for proximity changes
[[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(sensorStateMonitor:) 
         name:@"UIDeviceProximityStateDidChangeNotification" 
                object:nil];
 
- (void)sensorStateMonitor:(NSNotificationCenter *)notification
{
  if ([[UIDevice currentDevice] proximityState] == YES)
    NSLog(@"Device is close to user.");
  else 
    NSLog(@"Device is not closer to user.");
}
 

How to convert Radians to Degrees and Degrees to Radians in Objective c

Radians to Degrees
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
 
...
 
NSLog(@"Output radians as degrees: %f", RADIANS_TO_DEGREES(0.584));
 
 
Degrees to Radians
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
 
...
 
NSLog(@"Output degrees as radians: %f", DEGREES_TO_RADIANS(60));
 
 
 

How to get Mobile Network Code (MNC) and Mobile Country Code (MCC) in iOS

#import < CoreTelephony/CTCarrier.h >
#import < CoreTelephony/CTTelephonyNetworkInfo.h >
 
// Setup the Network Info and create a CTCarrier object
CTTelephonyNetworkInfo *networkInfo = [[ 
                                     [CTTelephonyNetworkInfo alloc]
                                                init] autorelease];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];
 
// Get carrier name
NSString *carrierName = [carrier carrierName];
if (carrierName != nil)
  NSLog(@"Carrier: %@", carrierName);
 
// Get mobile country code
NSString *mcc = [carrier mobileCountryCode];
if (mcc != nil)
  NSLog(@"Mobile Country Code (MCC): %@", mcc);
 
// Get mobile network code
NSString *mnc = [carrier mobileNetworkCode];
if (mnc != nil)
  NSLog(@"Mobile Network Code (MNC): %@", mnc);
 
NSString *iso = [carrier isoCountryCode];
if (iso != nil) 
NSLog(@"ISO country Code (ISO): %@", iso);

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...