Tuesday, May 22, 2012

iPhone Tips and Tricks

How to disable ARC in Xcode for Latest iOS Applications

1. Select Project --> Builds Phases -->Compile Sources

double click and File for which you want to disable ARC --> In pop window paste this command
-fno-objc-arc
That's it, Build project, Enjoy!!


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

How to get Current Device Info (like it is iPhone,iPad or iPhone Simulator etc.)

Just use the Below code and you will get return string as

NSString *deviceType = [[UIDevice currentDevice] model];
NSLog(@"My Current Device is : %@",deviceType);

iPhone Device Name = " iPhone "
iPhone Simulator Name = " iPhone Simulator "
iPad Simulator Name = " iPad Simulator "
iPod Device Name = " iPod touch " 

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

Show/Hide Hidden Files on Your Mac

1. Show Hidden files
1.1 Launch the Terminal (you can find Terminal this way)

      Go --> Utilities - > Terminal

1.2  paste this command

defaults write com.apple.Finder AppleShowAllFiles TRUE

1.3 Hit Enter
1.4 now for restart Finder hit below command

killall Finder

2. Hide Hidden files
1.1 Launch the Terminal (you can find Terminal this way)

      Go --> Utilities - > Terminal

1.2  paste this command

defaults write com.apple.Finder AppleShowAllFiles FALSE

1.3 Hit Enter
1.4 now for restart Finder hit below command

killall Finder

How to Create Button programatically

1. add below code in viewDidLoad method of ViewController

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"My Button" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];

2. add this method and Go for Execute the code for desired output.

-(void) buttonPressed{
    NSLog(@"My Button pressed.");
}


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

How to get Current Device Orientation (like It's Portrait or Landscape etc.)

Just use the Below code and you will get result

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIInterfaceOrientation orientationType = [UIDevice currentDevice].orientation;
    
    NSLog(@"Device's Current Orientation is : %d",orientationType);
    
    if(orientationType == 0){
        //this is really a just a failsafe.
        NSLog(@"Orientation is in :Default Portrait :");
    }else if(orientationType == 1){        
        NSLog(@"Orientation is in : Portrait :");
        
    }else if(orientationType == 2){        
        NSLog(@"Orientation is in : Portrait UpsideDown :");
        
    }else if(orientationType == 3){        
        NSLog(@"Orientation is in : Landscape Left :");
        
    }else if(orientationType == 4){        
        NSLog(@"Orientation is in : Landscape Right :");
    }   

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


How to change Navigation Bar's Background colour and Title programatically

add below code in viewDidLoad :
1. update tint color
//set Green Colour
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:34/255.0f green:139/255.0f blue:34/255.0f alpha:1];

2. add title
self.title = @"Welcome Screen";


How to add Bar-Button on Navigation Bar

1. Make sure you have already added Navigation Bar controller in application

2. Create UIBarButton

UIBarButtonItem *signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:@" SIGN IN " style:UIBarButtonItemStyleBordered target:self action:@selector(signInUser)];
UIColor *button_color = [UIColor colorWithRed:109.0/255.0 green:152.0/255.0 blue:136.0/255.0 alpha:1.0];
signIn_BarButton.tintColor =button_color;

3. Add it to Navigation Bar Controller (on right side)

self.navigationItem.rightBarButtonItem =signIn_BarButton;

4. Add the method for Button click

-(void) signInUser{
    
    signInViewController = [[SignInViewController alloc] init];
    [self.navigationController pushViewController:signInViewController animated:YES];
}

5. here is the result screen




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

Handle Screen Orientations in iOS 5 and iOS 6 

//For up-to iOS 5.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported all orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


//For iOS 6.0
-(NSInteger)supportedInterfaceOrientations
{
    //Supporting only portrait orientation.
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return NO;
}

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


How to disable NSLog in Xcode for Production stage

Add #define NSLog  in appName-Prefix.pch file in Supporting Files Folder of your project
and result file code look like..

//
// Prefix header for all source files of the 'NSLog' target in the 'NSLog' project
//

#import <Availability.h>

#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

//Add this to disable NSLog
#define NSLog

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



How to Get Current Device Width and Hieght

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

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

No comments:

Post a Comment