Posts Tagged ‘sdk’

iPhone OS SDK – Checking Internet Reachability

March 16th, 2010 iPhone SDK Tips

Checking for an internet connection is becoming an important part of each iPhone app, as applications and websites evolve to more a more unified experience, getting info off the internet is very important.
Checking for an internet connection on iPhone can be a somewhat moderately difficult task. Luckily apple released a Reachability sample code, which then makes implementation very easy.
Well, in line with the recent theme of my site, I’m going to show you how I implemented it in my programming experiences lately.
I’m going to run you through I how do this in my apps, with a focus on keeping it simple.

Step 1: Add the reachability class from the Reachability Demo
Download them from the apple sample, or here. Then include them in your Xcode, by dragging the sources into xcode (Copying in is fine).

Step 2: Add the Extra Frameworks
Add the Foundation and SystemConfiguration inbuilt frameworks to your app.
Screen-shot-2010-03-16-at-10.37.42.CaUeN2xcbHVT.jpg
Step 3: Think first then code
iPhone-Empty-View-Implemented-by-Tapku.D6WKcx9GiT7b.jpg
The code is in the next step, but first its important to think where you are going to check for Internet Access. If you only need a particular site to work, change the initial ping to that one site. google.com, finance.yahoo.com etc.
econdly and more importantly, think about where you are going to implement the Reachability test. You will generally only want to do it once, in AppDelegate and then store the outcome as a boolean variable (YES/NO). If there ends up being multiple places in the application where you want to test for internet access, remember to do all the lookup business just once. This will speed up the app and also reduce app owners data transfer per session.
I am going to implement this such that a separate icon appears in the top right of my app MainView if there is no internet. Checkout the Three20 or Tapku empty pages, of how they implement the nice ‘empty’ screen which seems to be invented for iPhone by apple.

Step 4: The easy bit: The Code
I want to implement the reachability test in the appDelegate. First lets include the library in appDelegate.h (the header file).
#import “Reachability.h”

in the @interface of the header:
//Got Internets?
NSString *internetReachability;
BOOL gotInternet;

and in the bottom of the header, before end:

-(BOOL)checkInternet;

In the main of the appDelegate, we add a custom method to handle the work, it does most of the heavy lifting in the Reachability.m file. (Notice how the ping url is finance.yahoo.com. I recommend pointing this to the website you are interested in interacting with.
#pragma mark -
#pragma mark Checking Internet Connection

-(BOOL)checkInternet{
//Test for Internet Connection
NSLog(@“——–“);NSLog(@“Testing Internet Connectivity”);
Reachability *r = [Reachability reachabilityWithHostName:@“finance.yahoo.com];
NetworkStatus internetStatus = [r currentReachabilityStatus];
BOOL internet;
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
internet = NO;
} else {
internet = YES;
}
return internet;
}

This method is then able to be called where-ever, but I want to do it in didFinishLaunchingWithOptions. I use the following:
gotInternet = [self checkInternet]; //Test for Internet, calling the self method

if ( gotInternet == 0) {
//I have no internet
NSLog(@”hey, i have no internets”);
internetIcon.hidden = NO;

} else {
//I do have internet
NSLog(@”hey, i have internets: all systems go”);
}

4. And thats it!

Notice how it now stores the variable accessible class-wide. To check for internet now, just check to see the BOOL output of gotInternet. An extension to this is if there is no internet, show the Tapku empty window instead of something internet input related.

Heres the NSLog output, the second launch of the app I turned wifi on, on the mac.

Hope you found it useful, comments possible below.

iPhone OS Development – deSimpleChart

February 20th, 2010 iPhone SDK Tips

iPhone SDK Simple Charting Class

Throughout my current project I’ve been looking for a simple chart that I can place inside my iPhone Application. There is the best framework around core-plot, but I found is extremely complicated and a little unmanageable for experienced programmers. I then came across CKSparkLine which is a fantastic and simple way of plotting an array of data to a uiview.

In my work I have lead to a slightly adapted CKSparkLine which would be useful enough to help someone else who wants a simple chart plot in an iPhone App.

Introducing the sample code: deSimpleChart. A ’simple’ chart plot based  off CKSparkLine to help a programmer new to iPhone development out. Below a screenshot of the complete output of the application. Keeping the idea simple and easy to learn from.


deSimpleChart 0.01  
Build “Keeping charts simple”

Download at Github
->”git clone git://github.com/duivesteyn-enterprises/deSimpleChart.git”

iPhone iPad Development – Quick – TableView Properties

February 12th, 2010 iPhone SDK Tips

I am currently working on supporting multi orientations of the iPhone (iPad) Screen, where on rotation some objects will be resized and others will be moved. For example I am wanting to move a tableView object and I want to ensure when I rotate back to Portrait that the tableview ends up in the exact same place before I moved it.

I came up with some quick debug code you can use in your iPhone App Development (or iPad App development).

The idea is you get the H,W,X,Y co-ordinates and can then later perfectly replace the initial properties.

Run this in viewDidAppear (for example)

int height = tableView.frame.size.height;
int width = tableView.frame.size.width;
int x = tableView.frame.origin.x;
int y = tableView.frame.origin.y;

NSLog(@”TableView Height:%d”,height);
NSLog(@”TableViewWidth:%d”,width);
NSLog(@”TableViewX:%d”,x);
NSLog(@”TableViewY:%d”,y);


Then later in willRotateToInterfaceOrientation you can use the code:

tableView.frame = CGRectMake(x, y, w, h);  //where x,y are the frame origin coordinates (x,y) and w,h are the width and height of the frame