Version 1.2 January 19
Copyright © 2009 Servin Corporation. http://servin.com
The iPhone OS, like Mac OS X, provides both Apple specific networking functions, and Unix-type networking functions.
There are Apple specific networking functions that are prefixed with SCNetwork. For example, you can use SCNetworkReachabilityGetFlags() to return status flags as to how you can reach the network.
Another example is the Unix function call gethostbyname(). You can issue this same call on the iPhone to convert a host name into an IP address.
In this Servin Mini-Course, you will learn how to use both Apple-specific and Unix-type function calls to get network information.
If Xcode is not already running, start it up:
At this point, you should see the Xcode menu at the top of your desktop.
With Xcode running, create a new Xcode project:
At this point, you should see Xcode open a new window that shows a number of files.
Go ahead and build the default application:
In this exercise, you will edit NetStatusAppDelegate.h and NetStatusAppDelegate.m to add a UITextView as a subview to the UIWindow.
NetStatusAppDelegate.h NetStatusAppDelegate.m
#import <UIKit/UIKit.h>
@interface NetStatusAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITextView *textView;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITextView *textView;
@end
#import <NetStatusAppDelegate.h>
@implementation NetStatusAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Add text
self.textView.text = @"Output will go here...";
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
As a review, in this exercise you added code to the NetStatusAppDelegate class to create a UITextView, setting it so that it cannot be edited. You also added text to the UITextView, and then added the UITextView to the subview of the window.
In this exercise, you will add the SystemConfiguration.framework to your project. This framework is needed, for it holds the SCNetwork functions, along with other functions related to system configuration.
- CoreGraphics.framework - Foundation.framework - UIKit.framework
- CoreGraphics.framework - Foundation.framework - SystemConfiguration.framework - UIKit.framework
As a review, in this exercise you added the SystemConfiguration.framework to your project. This is needed, because the SCNetwork functions you will use later are in this framework.
In this exercise, you will add the two additional header files needed for this project:
- SystemConfiguration/SystemConfiguration.h - netinet/in.h
- main.m - NetStatus_Prefix.pch
#ifdef __OBJC__ #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import <SystemConfiguration/SystemConfiguration.h> #import <netinet/in.h> #endif
As a review, in this exercise you added the additional header files needed for this project.
In this exercise, you will use the SCNetworkReachabilityGetFlags() function to determine:
- Is the network reachable - Is the network a cell phone network
NetStatusAppDelegate.h NetStatusAppDelegate.m
Boolean SCNetworkReachabilityGetFlags( SCNetworkReachabilityRef target, SCNetworkReachabilityFlags *flags);
SCNetworkReachabilityRef SCNetworkReachabilityCreateWithAddress( CFAllocatorRef allocator, const struct sockaddr *address)
#import <NetStatusAppDelegate.h>
@implementation NetStatusAppDelegate
@synthesize window;
@synthesize textView;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Part 1 - Create Internet socket addr of zero
struct sockaddr_in zeroAddr;
bzero(&zeroAddr, sizeof(zeroAddr));
zeroAddr.sin_len = sizeof(zeroAddr);
zeroAddr.sin_family = AF_INET;
// Part 2- Create target in format need by SCNetwork
SCNetworkReachabilityRef target =
SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &zeroAddr);
// Part 3 - Get the flags
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
// Part 4 - Create output
NSString *sNetworkReachable;
if (flags & kSCNetworkFlagsReachable)
sNetworkReachable = @"YES";
else
sNetworkReachable = @"NO";
NSString *sCellNetwork;
if (flags & kSCNetworkReachabilityFlagsIsWWAN)
sCellNetwork = @"YES";
else
sCellNetwork = @"NO";
NSString *s = [[NSString alloc]
initWithFormat:
@"Network Reachable: %@\n"
@"Cell Network: %@\n",
sNetworkReachable,
sCellNetwork];
// Add text
self.textView.text = s;
[sCellNetwork release];
[sNetworkReachable release];
[s release];
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
Network Reachable: YES Cell Network: NO
As a review, in this exercise you used the SCNetworkReachabilityGetFlags() function to get status related to network reachability.
In this exercise, you will use the Unix C library function gethostbyname(3) to display the IP address of a remote host.
endhostent, gethostbyaddr, gethostbyname - get network host entry
#include <netdb.h> struct hostent * gethostbyname(const char *name);
struct hostent *remoteHostEnt = gethostbyname("servin.com");
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host addr type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses from the name server */
}
struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
#include <arpa/inet.h> char * inet_ntoa(struct in_addr in);
char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
main.m NetStatus_Prefix.pch
#ifdef __OBJC__ #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import <SystemConfiguration/SystemConfiguration.h> #import <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #endif
NetStatusAppDelegate.h NetStatusAppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create instance of UITextView
self.textView = [[UITextView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Part 1 - Create Internet socket addr of zero
struct sockaddr_in zeroAddr;
bzero(&zeroAddr, sizeof(zeroAddr));
zeroAddr.sin_len = sizeof(zeroAddr);
zeroAddr.sin_family = AF_INET;
// Part 2- Create target in format need by SCNetwork
SCNetworkReachabilityRef target =
SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &zeroAddr);
// Part 3 - Get the flags
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
// Part 4 - Create output
NSString *sNetworkReachable;
if (flags & kSCNetworkFlagsReachable)
sNetworkReachable = @"YES";
else
sNetworkReachable = @"NO";
NSString *sCellNetwork;
if (flags & kSCNetworkReachabilityFlagsIsWWAN)
sCellNetwork = @"YES";
else
sCellNetwork = @"NO";
// Get host entry info for given host
struct hostent *remoteHostEnt = gethostbyname("servin.com");
// Get address info from host entry
struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
// Convert numeric addr to ASCII string
char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
NSString *s = [[NSString alloc]
initWithFormat:
@"Network Reachable: %@\n"
@"Cell Network: %@\n"
@"Remote IP: %s\n",
sNetworkReachable,
sCellNetwork,
sRemoteInAddr];
// Add text
self.textView.text = s;
[sCellNetwork release];
[sNetworkReachable release];
[s release];
// Make non-editable
self.textView.editable = NO;
// Add as subview to window
[window addSubview:self.textView];
// Decrement our usage count
[self.textView release];
[window makeKeyAndVisible];
}
Network Reachable: YES Cell Network: NO Remote IP: 216.75.35.182
As a review, in this exercise you used the Unix man pages to display information about the gethostbyname(3) C-language library call, and you used the function to resolve a name to an IP address.
Feel free to contact the author for any of the following:
Updated 2009 Jan 19
Content viewable on all web browsers, including smart mobile phone devices.
Copyright © 1995-2009 Servin Corporation. All rights reserved.