Friday 3 June 2011

How to get the current orientation on iPhone/iPad


UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

The UIInterfaceOrientation is an enum:
typedef enum {
  UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
  UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeLeft,
  UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeRight
} UIInterfaceOrientation;



Get UDID

NSString *uniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];

Detect carrier information on iPhone

With the release of iOS 4, Apple introduced two new frameworks for obtaining carrier information. CTCarrier offers information about the cellular provider including the carrier name, Mobile Network Code and Mobile Carrier Code. CTTelephonyNetworkInfo is the channel to access information through CTCarrier, this class also provides an update notifier if you need to detect changes to a changes in cellular provider information, for example, if the user swaps out there SIM card.


#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);




Get the current iPhone TimeZone

NSTimeZone *localTime = [NSTimeZone systemTimeZone];
NSLog(@"Current local timezone  is  %@",[localTime name]);

Get the current iPhone Language Code

NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSLog(@"The device's specified language is %@", language);

Thursday 2 June 2011

NSLog format strings with example

//Integers: %i or %d
int int_val = -10;
NSLog(@"%i, %d", int_val, int_val);
unsigned int uint_val = 5;
//unsigned integers use %u
NSLog(@"%u", uint_val);
//float and double use %f
float f_val = 12.75;
NSLog(@"%f", f_val);
// Hexadecimal %x or %X
int hex = 0x15;
NSLog(@"%x", hex);
// Character %c
char char_val = 'A';
NSLog(@"%c", char_val);