Posts tagged xcode

Get to the point: Detect Internet Connection

We will be using Apple’s reachability class to detect the state of the Internet connection. Reachability is freely available from Apple’s website: http://developer.apple.com/iphone/library/samplecode/Reachability/

1- Create a View-based Application and name it InternetConnectionState

2- Download Apple’s Reachability App source code and copy  reachability.h & reachability.m files to your application’s Classes folder.

3- Add these files to your XCode project. Right click the Classes folder and select  Add->Existing Files (see Figure 1). Select the reachability.h & reachability.m files and add them to your XCode project. You should see these file under the Classes folder (see Figure 2).

Figure 1

Figure 2

4- Add the “SystemConfiguration” framework. Right click the Frameworks folder and select Add->Existing Frameworks. Select & Add the SystemConfiguration.framework (see Figure 3).

Figure 3

5- Open the InternetConnectionStateAppDelegate.h and add the following code below the #import <UIKit/UIkit.h> line:

@class Reachability;

Add the following lines inside @interface:

Reachability* hostReach;

Reachability* internetReach;

Reachability* wifiReach;

Finally, add these lines above the @end line:

- (void) updateInterfaceWithReachability: (Reachability*) curReach;

- (void) statusView: (NSString*) statusString;

6- Open the InternetConnectionStateAppDelegate.m and add this line below the last #import:

#import "Reachability.h"

7- Add the following lines of code to the applicationDidFinishLaunching function:

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

hostReach = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];

[hostReach startNotifer];

[self updateInterfaceWithReachability: hostReach];

internetReach = [[Reachability reachabilityForInternetConnection] retain];

[internetReach startNotifer];

[self updateInterfaceWithReachability: internetReach];

wifiReach = [[Reachability reachabilityForLocalWiFi] retain];

[wifiReach startNotifer];

[self updateInterfaceWithReachability: wifiReach];

8- Copy these functions below the applicationDidFinishLaunching function:

- (void) networkStatusChanged: (Reachability*) curReach

{

NetworkStatus netStatus = [curReach currentReachabilityStatus];

BOOL connectionRequired= [curReach connectionRequired];

NSString* statusString= @"";

switch (netStatus)

{

case NotReachable:

{

statusString = @"Access Not Available";

connectionRequired= NO;

[self statusView:statusString];

break;

}

case ReachableViaWWAN:

{

statusString = @"Reachable WWAN";

[self statusView:statusString];

break;

}

case ReachableViaWiFi:

{

statusString= @"Reachable WiFi";

[self statusView:statusString];

break;

}

}

if(connectionRequired)

{

statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];

}

}

- (void) statusView: (NSString *) statusString{

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 50)];

titleLabel.font = [UIFont systemFontOfSize:15];

titleLabel.textColor = [UIColor blackColor];

titleLabel.backgroundColor = [UIColor whiteColor];

titleLabel.opaque = YES;

titleLabel.textAlignment = UITextAlignmentCenter;

titleLabel.text = statusString;

[viewController.view addSubview: titleLabel];

}

- (void) updateInterfaceWithReachability: (Reachability*) curReach

{

if(curReach == hostReach)

{

BOOL connectionRequired= [curReach connectionRequired];

NSString* baseLabel=  @"";

if(connectionRequired)

{

baseLabel=  @"Cellular data network is available.\n  Internet traffic will be routed through it after a connection is established.";

}

else

{

baseLabel=  @"Cellular data network is active.\n  Internet traffic will be routed through it.";

}

}

if(curReach == internetReach)

{

[self networkStatusChanged: curReach];

}

if(curReach == wifiReach)

{

[self networkStatusChanged: curReach];

}

}

- (void) reachabilityChanged: (NSNotification* )note

{

Reachability* curReach = [note object];

NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

[self updateInterfaceWithReachability: curReach];

}

9- Save & Build your application. If there were no errors you should get something similar to Figure 4.

Figure 4

Click here to download the source code.

Get to the point: Tab Bar Application – Adding an UITableView to a Navigation Controller

Get to the point: Tab Bar Application – Adding an UITableView to a Navigation Controller

1- Open the previous tutorial sources (Tab Bar Application – Navigation Controller Embedded).

2- Edit the NavigationContentsViewController.h file. Change the source code so it looks like this:

#import &lt;UIKit/UIKit.h&gt;

@interface NavigationContentsViewController : UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt; {

NSMutableArray *itemsList;

UITableView *myTableView;

}

@property(nonatomic,retain)NSMutableArray *itemsList;

@property(nonatomic,retain)UITableView *myTableView;

@end

Notice the <UITableViewDelegate, UITableViewDataSource> protocols we’ve added.

3- Edit the NavigationContnensViewController.m file and add the following code after the @implementation line:

@synthesize itemsList;
@synthesize myTableView;

4- Add the TableView functions to the NavigationContnensViewController.m file.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [itemsList count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

}

// Set up the cell...

NSString *cellValue = [itemsList objectAtIndex:indexPath.row];

cell.textLabel.text = cellValue;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *selectedCellItem = [NSString stringWithFormat:@"%d", indexPath.row];

TableViewController *fvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]];

fvController.selectedCellItem = selectedCellItem;

[self.navigationController pushViewController:fvController animated:YES];

[fvController release];

fvController = nil;

}

5- Add these lines of code below the previous added code.

-(void)loadView{

myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];

myTableView.delegate = self;

myTableView.dataSource = self;

myTableView.autoresizesSubviews = YES;

itemsList = [[NSMutableArray alloc] init];

[itemsList addObject:@"Item1"];

[itemsList addObject:@"Item2"];

self.navigationItem.title = @"Test List";

self.view = myTableView;

}

6- Remember to release the itemsList array. Do so by adding [itemsList release]; to the dealloc function.

7- We will need a View Controller so go ahead and create it (File->New File ->Cocoa Touch Class->UIViewController subclass) and name it TableViewController (Figure 1). Make sure the “With XIB for user interface” option is marked.

8- Open the TableViewController.h file and change the source code so it looks like this:

#import &lt;UIKit/UIKit.h&gt;

@interface TableViewController : UIViewController {

NSString *selectedCellItem;

}

@property (nonatomic, retain) NSString *selectedCellItem;

@end

9- Now open the TableViewController.m file and add this line of code below the @implementation:

@synthesize selectedCellItem;</pre>
10- Uncomment the viewDidLoad function and add these lines of code:
[sourcecode language="objc"]self.navigationItem.title = @"Selected Item";

CGRect frame = CGRectMake(0, 0, 320, 180);

self.view = [[UIView alloc] initWithFrame:frame];

self.view.backgroundColor = [UIColor whiteColor];

frame = CGRectMake(100, 20, 100, 50);

UILabel *label = [[UILabel alloc] initWithFrame:frame];

label.text = selectedCellItem;

[self.view addSubview:label];

[label release];

[super viewDidLoad];

11- Go back to the NavigationContentsViewController.m file and add this code below the #import “NavigationContentsViewController.h”:

#import "TableViewController.h"

12- That’s it, if you have followed all the previous steps, your project should compile & run without errors.

Download the sources of this tutorial by clicking here.

Get to the point – Code Snippets: NSString to NSInteger & vice versa

NSString to NSInteger code snippet:

NSString *strTest = [NSString stringWithFormat:@"%d", 10];

NSInteger to NSString code snippet:

NSInteger iTest = [strTest integerValue];

Get to the point – iPhone Development: Tab Bar Application

Get to the point, iPhone development tutorials for those programmers who want concise step by step tutorials, but do not have the time to watch a 30 min video tutorial.

Get to the point – iPhone Development: Tab Bar Application (Adding new views)

1- Open Xcode and create a new “Tab Bar Application” (see Figure 1). Name it tabbartest (see Figure 2).


Figure 1


Figure 2

2- Xcode will create the necessary files for a basic Tab Bar Application (see Figure 3).


Figure 3

3- By double clicking the MainWindow.xib file Interface Builder will show up (see Figure 4). If the Inspector and Library windows don’t appear, open them (Tools->Library, Tools->Inspector).


Figure 4

4- Go back to Xcode and create a new file (File->New File). From the User Interface menu list select “View XIB” (see Figure 5). Name this view as ThirdView.xib (see Figure 6).


Figure 5


Figure 6

5- From Xcode double click the ThirdView.xib file. Interface Builder will show up again (see Figure 7).


Figure 7

6- Click on the File’s Owner icon. Go to the Inspector window and click on the “View Identity” icon. From the Class list select FirstViewController (see Figure 8).


Figure 8

7- Click on the “View Connection” menu option. Click on the view outlet, then drag & release it on the view canvas (see Figure 9). If you do this correctly the “View Connection” window should look like Figure 10.


Figure 9


Figure 10

8- Go back to Xcode and open the MainWindow.xib file. Add a new Tab Bar Item (see Figure 11) by selecting it from the Library. Drag the Tab Bar Item to the Tab Bar Controller (see Figure 12).


Figure 11


Figure 12

9- Click on the newly added Tab Bar Item and select the “View Controller Attributes” from the Inspector Window (see Figure 13). Set the NIB Name parameter to ThirdView (see Figure 14). Once the NIB Name is set, your MainWindow file should show the text “Loaded From ThirdView” (see Figure 15).


Figure 13


Figure 14


Figure 15

10- Save all your Xcode & Interface Builder files. Click “Build and Run” from Xcode. The Tab Bar Application will show up on the iPhone Simulator with a third Tab Bar Item (see Figure 16).


Figure 16

Tab Bar Application – Source Code

Get Adobe Flash playerPlugin by wpburn.com wordpress themes