Wednesday, 16 May 2012

Access specifier in iOS / Objective C / iPhone / iPad programming

                    Access specifier in iOS programming


  • protected (by default)
  • private
  • public
  • package
protected :-
        default access specifier in objective c is protected.

Monday, 9 January 2012

How to add and retrieve NSMutableArray value dynamically

NSMutableArray parent class is NSArray


 //Add values dynamically

 NSMutableArray* arr_country = [[NSMutableArray alloc]init];
 [arr_country addObject:@"INDIA"];
 [arr_country addObject:@"RUSSIA"];

 //retrieve array value

id maybeNSString = [arr_country objectAtIndex:(NSUInteger)0]; //return value of the zero th position

//get array count
NSInteger arr_count  = arr_country.count;


Thursday, 10 November 2011

UINavigationBar and UINavigationItem background image

UINavigationBarItem Background Image


navigationController.parentViewController action:@selector(yourmethod:)];
             self.navigationItem.leftBarButtonItem.image = [UIImage imageNamed:@"leftbar.png"];

UINavigationBar Background Image

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header.png"] forBarMetrics:UIBarMetricsDefault];



iOS Application starts from main method

iOS application starts from main method


int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}



int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.



Handle Orientation in iPhone/iPad


  
By default iOS5 and earlier supports only portrait mode orientation, if we want to enable all orientation we have to implement shouldAutorotateToInterfaceOrientation method in UIViewController.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES; //YES for supporting all orientation. NO- disable orientation
}

iOS have four orientations are 
   UIInterfaceOrientationPortrait
   UIInterfaceOrientationPortraitUpsideDown
   UIInterfaceOrientationLandscapeLeft    
   UIInterfaceOrientationLandscapeRight
if dont want any one orientation, then check the orientation and return NO

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(UIInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    return NO; //disable UIInterfaceOrientationLandscapeLeft orientation
else
   return YES;
}

Find device orientation changes :-

//implement this method to find the device orientation changes - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { } If your application doesn't call didRotateFromInterfaceOrientation method then you have to add your controller as child view controller in then rootview controller. - (void)viewDidLoad { [super viewDidLoad]; [self addChildViewController:(UIViewController*) self.yourChildController]; }

Changing UIView when Orientation change :-

Here i created two UIView and assigned when orientation changed. I created two dashboard UIView for portrait and landscape , get the IBOutlet from XIB 
Views.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ) { self.view = self.LandscapeView; } else { self.view = self.portraitView; } return YES; }
Download the source code for better understanding : https://github.com/karthikprabhuA/iPhoneOrientationHandling



Launch iPhone/iPad email composer in your application


MFMailComposeViewController  class is used to editing and email, its present in MessageUI.framework.

canSendMail method is used to find whether the user has set up the device for sending email .

Here launched email composer in my application, presenting as a modal popover.

if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.navigationBar.tintColor = [UIColor redColor]; // your navigationbar color
mail.mailComposeDelegate = self;
[mail setSubject:@"Check out my iPhone/iPad app"];
[mail setMessageBody:@"<html><body>Check out <a href='http://karthik-prabhu.blogspot.in/'>iVerde</a></body></html>" isHTML:YES];
        //mail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
       // [self setModalPresentationStyle:UIViewAnimationOptionTransitionFlipFromTop];
            [self presentViewController:mail animated:YES completion:NULL ]; //presentModalViewController deprecated from iOS5 and above 
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"App name Message" message:@"Please setup at least One email account." delegate:self cancelButtonTitle:nil      otherButtonTitles: @"OK", nil];
[alert show];
[alert release];
}





Wednesday, 2 November 2011

How to get iPhone iPad System Properties

UIDevice Class is used to get the phone name, model, localizedModel, systemname, system version,orientation,battery level etc.

  [[UIDevice currentDevicename]
  [[UIDevice currentDevice] model]
  [[UIDevice currentDevicelocalizedModel]
  [[UIDevice currentDevicesystemName]
  [[UIDevice currentDevicesystemVersion]
  [[UIDevice currentDeviceorientation]
  [[UIDevice currentDevicebatteryLevel]


The main advantage is to check the battery level ;

float btlevel = [[UIDevice currentDevice] batteryLevel]; // range is from 0.0 to 1.0 UIDeviceBatteryState btstate = [UIDevice currentDevice] batteryStatus];