Tuesday, November 15, 2011

UINavigationController Customization Tutorial


Creating a UINavigationController programmatically

Creating a UINavigationController programmatically is trivial. Assuming that you’ve already created a class called RootViewController that subclasses UIViewController, you would use something like this, typically in your AppDelegate file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// rootViewController can't be an instance of UITabBarController
// remember to include RootViewController in your class!
RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

[self.window addSubview:navigationController.view];

// Don't bother releasing navigationController as it needs to be around all the time

   [self.window makeKeyAndVisible];

   return YES;
}


Basically, we just create an instance of root controller and then we use it to create the navigation controller. Finally, we need to add the navigation controller’s view to the main window.

Adding a title to a navigation controller

Actually you don’t add a title directly to the navigation controller, instead you add it to the view controller that’s being displayed:


- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";
}



Hiding the Navigation Bar

Either in your view controller or where you initiated your navigation controller:


- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";
// hide the navigation bar
// use setNavigationBarHidden:animated: if you need animation
self.navigationController.navigationBarHidden = YES;
}



Showing the navigation toolbar

Either in your view controller or where you initiated your navigation controller:


- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";
// hide the navigation bar
// use setToolbarHidden:animated: if you need animation
self.navigationController.toolbarHidden = NO;
}



Changing the Navigation Bar background color

Either in your view controller or where you initiated your navigation controller:


- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";

self.navigationController.navigationBar.tintColor = [UIColor
colorWithRed:102.0/255
green:52.0/255
blue:133.0/255
alpha:1];
}



Changing the Navigation Bar style

Default style is UIBarStyleDefault, but you can use UIBarStyleBlack and eventually set the translucent propriety to YES.

- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}



- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationController.navigationBar.translucent = YES;
}

Using a Navigation Bar background image

To use a background image for you navigation bar, you need to subclass UINavigationBar and declare your own drawRect method. You can add this code on its own file or at the top of your application delegate (after the import declarations):


@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:@"bg.png"];
[image drawInRect:rect];
}
@end

Changing Navigation Bar back button text

Here’s how to change the text of the back button:



- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";

self.navigationController.navigationBar.backItem.title = @"Custom";
}



Adding items to the right of your Navigation Bar

In your view controller:


- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";

UIBarButtonItem *item = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
target:self 
action:@selector(doSomething)];
self.navigationItem.rightBarButtonItem = item;

[item release];
}

Adding items to the Navigation Toolbar

To add items to your toolbar:


- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";

// Show toolbar
self.navigationController.toolbarHidden = NO;

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] 
   initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
 target:self 
 action:nil];

UIBarButtonItem *item2 = [[UIBarButtonItem alloc] 
   initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
 target:self 
 action:nil];


UIBarButtonItem *item3 = [[UIBarButtonItem alloc] 
   initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
 target:self 
 action:nil];

NSArray *items = [NSArray arrayWithObjects:item1, item2, item3, nil];

self.toolbarItems = items;

[item1 release];
[item2 release];
[item3 release];
}


Note that the items are not properly aligned. To align them, use flexible space:

- (void)viewDidLoad {
   [super viewDidLoad];

self.title = @"My View Controller";

// Show toolbar
self.navigationController.toolbarHidden = NO;

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] 
 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
 target:nil 
 action:nil];

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] 
   initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
 target:self 
 action:nil];

UIBarButtonItem *item2 = [[UIBarButtonItem alloc] 
   initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
 target:self 
 action:nil];


UIBarButtonItem *item3 = [[UIBarButtonItem alloc] 
   initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
 target:self 
 action:nil];

NSArray *items = [NSArray arrayWithObjects:item1, flexibleSpace, item2, flexibleSpace, item3, nil];

self.toolbarItems = items;

[item1 release];
[item2 release];
[item3 release];
}


Conclusion

Take a look at the UINavigationController documentation to find out more about it.

Wednesday, November 9, 2011

Xcode 4 : Select and Edit All Variables in Scope


The Jump Bar is the navigation area at the top of the source editor pane, which is highlighted in red in the figure below:


There is a popup menu available by clicking on each separate section of the Jump Bar. For example, clicking on the title SandboxAppDelegate.m will bring up a list of all the other files stored at the same location in the project hierarchy. Notice in the screenshot below that the file list shown in the popup is the same as shown in the project navigator.


Jump Bar Tip #1 – Sorting
You can sort the entries in the popup list by Command-clicking the Jump Bar. In the screenshot below is the same popup shown above, however, all the entries are now sorted.


Jump Bar Tip #2 – Type Ahead
Once a popup is displayed, you can type ahead to narrow the list of entries. This is really handy if you have a long list of files or methods and are looking for a specific entry.
The screenshot below shows the popup for the file list:

As I type characters, the list of the entries narrows based on my search:


Jump Bar Tip #3 – Open in Assistant Editor
To open a file in the assistant editor, from within the popup showing the file list, Option-click the desired file. If you are viewing a popup of the methods in a file, if you Option-click, the file containing the method will be opened in the assistant editor, and the selected method will be highlighted:

Xcode 4 : Select and Edit All Variables in Scope


Xcode offers a nice way to highlight all references of a variable within the current scope. First, make sure you have the setting shown below enabled within the Text Editing tab in the Xcode Preferences:


Now, select a variable within your code, and notice that all references within the current scope will be highlighted. For example, below I selected (double tapped) username and all references are now underlined.

In the above figure notice the drop down arrow next to the first instance that I referenced. Tapping this arrow brings up an dialog, given the configuration of my system, the dialog looks as follows:

The values in your dialog may be different based on your system configuration.
If you choose the Edit All in Scope, Xcode will highlight all references and allow you to change the name of the selected variable. Changes will cascade through to each reference of the variable.

Using this tip you can quickly change all references to a variable in the current scope, watching the changes take place as you type.

Get Started Guide for iOS using Phonegap


  • 1. Requirements

    • Intel-based computer with Mac OS X Snow Leopard (10.6)
    Necessary for Installing on Device:
    • An Apple iOS device (iPhone, iPad, iPod Touch)
    • iOS developer certification
  • 2. Install SDK + PhoneGap

    Download and install Xcode from Apple Developer Portal (Membership required)

    Download the latest copy of PhoneGap and extract its contents. Navigate to the iOS directory and run the installer until completion.
  • 3. Setup New Project

    • Launch Xcode, then under the File menu select New and then New Project...
    • Select PhoneGap-based Application from the list of templates
    • Select the Next button, Fill in the "Product Name" & "Company Identifier" for your app
    • Choose a directory to store your app
    • You should see your project in Xcode 4 now. Press the Run button in the top left corner. Your build should succeed and launch in the simulator
    • You should see a error in your simulator informing you index.html was not found
    • To fix this, we need to copy the www directory into the project. Right click on the project in the left navigation window and click show in finder
    • In Finder, you should see the www directory beside your project
    • Next step is IMPORTANT! Drag the www folder into Xcode 4. You can't just drag the www folder into your app's folder. It needs to be dragged into Xcode 4!! In my case I would drag it and drop it on HiWorld shown below.
    • After you drag, you should see a prompt with a few options. Make sure to select Create folder references for any added folders. Click Finish
  • 4. Hello World

    Open the folder named www and type <h1>Hello World</h1> after the <body> tag in index.html. You can also add any associated Javascript and CSS files there as well.
  • 5A. Deploy to Simulator

    • Make sure to change the Active SDK in the top left menu to Simulator+version#.
    • Hit Run in your project window header.
  • 5B. Deploy to Device

    • Open [AppName]-Info.plist and change BundleIdentifier to the identifier provided by Apple. If you have a developer license, you can access and run the Assistant at here and register your App.
    • Make sure to change the Active SDK in the top left menu to Device+version#.
    • Hit Run in your project window header.
  • Thursday, November 3, 2011

    Read some interesting data from your iDevice with iOS 4.3



    Ever tried to read some interesting data like the Serial Number, IMEI, Phone Number… from your iPhone (or an other iDevice) ? Some data is easy to access, some other uses not supported API calls. I have written a simple App which can read the above mentioned data. You can download it here (click)

    Reading common data is very easy, this is the Code for the UniqueIdentifier, iDevice Name, System Name, System Version and Model:
    1. // Stores the complete Text Output 
    2. NSMutableString *data = [[NSMutableString alloc] init];  
    3. // Reading Legal Apple Data 
    4. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"phoneUniqueIdentifier", [[UIDevice currentDevice] uniqueIdentifier]]]; 
    5. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"name", [[UIDevice currentDevice] name]]]; 
    6. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"systemName", [[UIDevice currentDevice] systemName]]]; 
    7. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"systemVersion", [[UIDevice currentDevice] systemVersion]]]; 
    8. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"model", [[UIDevice currentDevice] model]]]; 
    9. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"localizedModel", [[UIDevice currentDevice] localizedModel]]]; 
    To access the IMEI and Serial Number you have to use the (semi)public IOKit framework. Erica Sadun has managed this and released the overwritten IOKit-Extension. I have included these files in my App and used it this way:
    1. #import "UIDevice-IOKitExtensions.h" 
    2.  
    3. // Reading not by the Appstore supported Data 
    4. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"IMEI", [[UIDevice currentDevice] imei]]]; 
    5. [data appendString:[NSString stringWithFormat: @"%@: %@nn",
      @"SerialNr", [[UIDevice currentDevice] serialnumber]]]; 

    Be aware that you cannot submit an App with this IOKit-Extension into the Appstore!

    Sunday, October 16, 2011

    iPhone: Locating the serial number, IMEI, ICCID, or MEID


    Getting the information using your iPhone

    If you have access to your iPhone, here are some quick ways to obtain your iPhone serial number, International Mobile Equipment Identity (IMEI), ICCID, or Mobile Equipment Identfier (MEID).

    iPhone About screen

    You can find your iPhone serial number, IMEI, ICCID, or MEID in the About screen on your iPhone. From the Home screen, tap Settings > General > About and scroll down.
    iPhone About screen
    If you are unable to access the About screen on your iPhone, please use one of the following options  to access the Serial Number and IMEI on iPhone models with SIM cards. If you have an iPhone 4 with Verizon, and are not able to access the information using the About screen, please see the section below to get the information from your computer.

    iPhone 3G, iPhone 3GS, iPhone 4 (GSM model), and iPhone 4S

    Please see this article for information on how to remove and re-insert the SIM tray. Once you have removed the SIM tray, the Serial Number and IMEI will be printed on the SIM tray as shown below.
    Serial number and IMEI number printed on an iPhone SIM tray.
    The SIM tray on an iPhone 4S activated on CDMA carrier displays both the MEID and IMEI number as 15 digits. The MEID number uses the first 14 digits, disregarding the last digit, and the IMEI number uses all 15 digits.

    Original iPhone

    Your original iPhone serial number and IMEI are engraved on the back metal case as shown:

    Getting the information from your computer

    If you are able to connect your iPhone to your computer

    If you have access to your computer and prefer to get the information using it, or if you are unable to use the iPhone itself, the following includes ways of accessing your iPhone serial number, International Mobile Equipment Identity (IMEI), ICCID, or Mobile Equipment Identfier (MEID).
    1. Connect your iPhone to your computer and open iTunes.
    2. Select the iPhone when it appears in the left-hand column.
    3. Click the Summary tab. Your iPhone's serial number and phone number will display as shown:
    4. If you click the words Phone Number in this tab, iTunes will also display the IMEI or MEID of your iPhone as shown below. If you click IMEI, iTunes will display the ICCID:

    Notes:
    • Some GSM carriers may not store the phone number on the SIM card, so the data may not be available in iTunes. If that is the case, you will want to access the information using one of the aforementioned ways. See HT2232 for more information.
    • You can choose Edit > Copy to put the serial number on the Clipboard. You can then paste the serial number into an email or webpage (for service or registration for example).

    If you have access to your computer, but not your iPhone

    Open iTunes and mouse over a device backup:
    1. Open Preferences in iTunes. (In Mac OS X, choose iTunes > Preferences. In Windows, choose Edit > Preferences.)
    2. Click the Devices tab.
    3. Position the mouse over a backup to display the phone number, serial number, and IMEI or MEID of the backed-up iPhone.
    About Screen tips
    • Hold down the Option key (Shift key in Windows) to reverse the scrolling.
    • Press the Space bar to pause the scrolling.
    • You can copy this information onto the Clipboard and and paste it into a document:
      • In Mac OS X, choose Edit > Copy (or Command-C).
      • In Windows, press Control-C.

    Getting the information from the iPhone packaging

    If you still have the device's original packaging, you can find your iPhone serial number and IMEI or MEID printed on the barcode label that's affixed to the packaging.

    Apple support and service


    Contacting Apple for support and service

    International Technical Support Numbers

    To expedite your call, please have your hardware serial number ready. Learn how to find your serial number.

    Country Phone Website
    Australia (61) 1-300-321-456* www.apple.com/au/support/
    Austria  Use Express Lane for fast, personalized service.
    (43) 0810 300 427*
    www.apple.com/at/support/
    Belgium Flemish: (32) 070 700 772
    French: (32) 070 700 773
    www.apple.com/benl/support/
    www.apple.com/befr/support/
    Brazil 0800-761-0880 www.apple.com/br/suporte/
    Brunei Dial 800-1111 first, then dial 800-708-5413 www.apple.com/asia/support/
    Bulgaria 00800 6002 7753 www.apple.com/bg/support/
    Canada


    Use Express Lane for fast, personalized service.
    English: 1-800-263-3394
    French: 1-800-263-3394
    www.apple.com/ca/support/
    www.apple.com/ca/fr/support/
    China (86) 4006-272273 www.apple.com.cn/support/
    Cyprus 800 92433 www.apple.com/cy/support/
    Czech Republic 800 700527 www.apple.com/cz/support/
    Denmark (45) 70 10 20 07 www.apple.com/dk/support/
    Estonia 8000 044333 www.apple.com/ee/support/
    Finland (358) 0800 96162 www.apple.com/fi/support/
    Fiji (61) 1-300-321-456* www.apple.com/asia/support/
    France Use Express Lane for fast, personalized service.
    Metropolitain: (33) 0805 540 003
    DOM-TOM: (33) 0825 77 00 35*
    www.apple.com/fr/support/
    Germany Use Express Lane for fast, personalized service.
    (49) 01805 009 433**
    www.apple.com/de/support/
    Greece 00800 44145417 www.apple.com/gr/support/
    Guam 1-800-865-0853 www.apple.com/asia/support/
    Hong Kong (852) 2112-0099* www.apple.com/hk/support/
    www.apple.com/hk/en/support/
    Hungary 06 80 983 435 www.apple.com/hu/support/
    India (91) 1800 4250 744 or (91) 80-4140-9000 www.apple.com/in/support/
    Indonesia 0018 03061 2009 www.apple.com/id/support/
    Ireland Use Express Lane for fast, personalized service.
    (353) 1850 946 191
    www.apple.com/ie/support/
    Italy Use Express Lane for fast, personalized service.
    (39) 199 120 800*
    www.apple.com/it/support/
    Japan Within Japan: 0120-27753-5
    Outside Japan: (81) 3-5334-2096
    www.apple.com/jp/support/
    Latvia 800 03251 www.apple.com/lv/support/
    Lithuania (8-800) 30772 www.apple.com/lt/support/
    Luxembourg (352) 800 24550 www.apple.com/lu/support/
    Macau Dial 0800-111 first, then dial 800-708-5413 www.apple.com/mo/support/
    Malaysia 1-800 803-638 www.apple.com/my/support/
    Malta 800 62072*** www.apple.com/mt/support/
    Mexico 001-866-676-5682
    iPhone: 001-800-692-7753
    www.apple.com/mx/support/
    Netherlands (31) 0900 7777 703 www.apple.com/nl/support/
    New Zealand 00800-7666-7666 www.apple.com/nz/support/
    Norway (47) 815 00 158 www.apple.com/no/support/
    Pakistan Dial 00800 01001 first, then dial 800 361 0479 www.apple.com/asia/support/
    Papua New Guinea (61) 1-300-321-456* www.apple.com/asia/support/
    Philippines 1-800-1441-0234 www.apple.com/ph/support/
    Poland 00800 4411875 (universal free phone) www.apple.com/pl/support/
    Portugal (00351) 0707 200 826 (national rate) www.apple.com/pt/support/
    Romania 0800 894847 www.apple.com/ro/support/
    Russia 495 5809557
    (PSTN local rate for Moscow, national rates for others)
    www.apple.com/ru/support/
    Singapore 800-186-1087 or (65) 6835-1812* www.apple.com/sg/support/
    Slovakia 0800 004640 www.apple.com/sk/support/
    Slovenia 0800 80321 www.apple.com/si/support/
    South Korea (82) 1544-2662* www.apple.com/kr/support/
    Spain Use Express Lane for fast, personalized service.
    (34) 902 151 992
    www.apple.com/es/support/
    Sweden (46) 0771 199 519 www.apple.com/se/support/
    Switzerland Use Express Lane for fast, personalized service.
    French: (41) 0848 000 132
    German: (41) 0848 000 132
    www.apple.com/chfr/support/
    www.apple.com/chde/support/
    Tonga (61) 1-300-321-456* www.apple.com/asia/support/
    Taiwan (886) 0800-095-988 www.apple.com/tw/support/
    Thailand 001800 4412904 www.apple.com/th/support/
    United Arab Emirates 8000 444 0407 www.apple.com/ae/support/
    United States


    Use Express Lane for fast, personalized service.
    1-800-275-2273
    iPhone: 1-800-MY-IPHONE (1-800-694-7466)
    Apple Rebates: 1-877-4-APL-PROMO (1-877-427-5776)
    Education Customers: 1-800-800-2775 (support), 1-800-780-5009 (sales)
    Enterprise: 1-866-752-7753
    www.apple.com/support/
    United Kingdom Use Express Lane for fast, personalized service.
    (44) 0844 209 0611****
    www.apple.com/uk/support/
    Vanuatu (61) 1-300-321-456* www.apple.com/asia/support/
    Vietnam Dial 1-201-0288, then dial 800-708-5413 www.apple.com/vn/support/

    Telephone numbers and hours of operation are subject to change.

    * Local and national telephone rates apply.
    ** 0,14 € pro Minute aus dem deutschen Festnetz. Maximal 0,42 € pro Minute aus dem Mobilfunknetz.
    *** Free phone technical support number is not accessible by mobile phone.
    **** £0.05p per minute when dialed from a landline within the UK. Call charges may vary when calling from a mobile phone.