Posts tagged xcode
Get to the point: Detect Internet Connection
Feb 21st
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
Get to the point – Code Snippets: NSString to NSInteger & vice versa
Feb 7th
NSString to NSInteger code snippet:
NSString *strTest = [NSString stringWithFormat:@"%d", 10];
NSInteger to NSString code snippet:
NSInteger iTest = [strTest integerValue];





















