Wednesday, April 04, 2012

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

Friday, January 20, 2012

Difference between a view’s bounds and frame

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.
The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

So, imagine a view that has a size of 100x100 (width x height) positioned at 50,50 (x,y) of its superview. The following code prints out this view's bounds and frame:
    NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
    NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
    NSLog(@"bounds.size.width: %f", label.bounds.size.width);
    NSLog(@"bounds.size.height: %f", label.bounds.size.height);

    NSLog(@"frame.origin.x: %f", label.frame.origin.x);
    NSLog(@"frame.origin.y: %f", label.frame.origin.y);
    NSLog(@"frame.size.width: %f", label.frame.size.width);
    NSLog(@"frame.size.height: %f", label.frame.size.height);
 
output: 
bounds.origin.x: 0
bounds.origin.y: 0
bounds.size.width: 100
bounds.size.height: 100

frame.origin.x: 25
frame.origin.y: 25
frame.size.width: 100
frame.size.height: 100

Monday, January 09, 2012

iOS versions and their mountain code names

  • 1.0: Alpine
  • 1.1: Little Bear
  • 2.0: Big Bear
  • 2.1: Sugarbowl
  • 2.2: Timberline
  • 3.0: Kirkwood
  • 3.1: Northstar
  • 3.2: Wildcat
  • 4.0: Apex
  • 4.1: Baker
  • 4.2: Jasper
  • 4.3: Durango
  • 5.0: Telluride
  • 5.1: Hoodoo

Tuesday, January 03, 2012

iOS: Adding Contacts Using AddressBook Framework

    ABRecordRef aRecord = ABPersonCreate();
   
    CFErrorRef  anError = NULL;
   
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
                      CFSTR("MAHES"), &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty,
                     CFSTR("CHELLAIAH"), &anError);
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, CFSTR("9751050790"), kABPersonPhoneMobileLabel, NULL); 
    ABRecordSetValue(aRecord, kABPersonPhoneProperty, multiPhone,nil);
    CFRelease(multiPhone);
   
    if (anError != NULL) {
       
        NSLog(@"error while creating..");
    }
   
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
    lastName  = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);
   
   
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
   
    BOOL isAdded = ABAddressBookAddRecord (
                                           addressBook,
                                           aRecord,
                                           &error
                                           );
   
    if(isAdded){
       
        NSLog(@"added..");
    }
    if (error != NULL) {
        NSLog(@"ABAddressBookAddRecord %@", error);
    }
    error = NULL;
   
    BOOL isSaved = ABAddressBookSave (
                                      addressBook,
                                      &error
                                      );
   
    if(isSaved){
       
        NSLog(@"saved..");
        [self handleCall];
    }
   
    if (error != NULL) {
        NSLog(@"ABAddressBookSave %@", error);
    }
   
    CFRelease(aRecord);
    CFRelease(firstName);
    CFRelease(lastName);
    CFRelease(addressBook);

Thursday, December 29, 2011

Add UIBarButton item into a UIToolBar








UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
// create a bordered style button with custom title
UIBarButtonItem *facebook = [[[UIBarButtonItem alloc] initWithTitle:@"facebook"
                                 style:UIBarButtonItemStyleBordered   target:self           
                                 action:@selector(fbAction:)] autorelease];

UIBarButtonItem *twitter = [[[UIBarButtonItem alloc] initWithTitle:@"twitter"
                              style:UIBarButtonItemStyleBordered
      target:self
      action:@selector(twAction:)] autorelease];
NSArray *items = [NSArray arrayWithObjects
  facebook,
  twitter,
  nil];
toolbar.items = items;
// size up the toolbar and set its frame
        // please not that it will work only for views without Navigation toolbars. 
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGRect mainViewBounds = self.view.bounds;
[toolbar setFrame:CGRectMake(0,0,320,40)];
[self.view addSubview:toolbar];

Create a list in SwiftUI with sticky section headers

 Sample Code import SwiftUI struct ContentView : View {     @State var isCustomViewControllerDisplayed = false     @State private va...