Archive of articles classified as' "iPhone SDK Tips"

Back home

iPhone OS SDK – Checking Internet Reachability

16/03/2010

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.

View Comments

iPhone SDK – Implementing the Tapku Graph In your application

7/03/2010

One of the simpler and nicer available graph controllers your iPhone app is from the Tapku library. The tapku library is created by the team at http://tapku.com/ at hosted at http://github.com/devinross/tapkulibrary.

The graph view controller I find as a good mix between a simple SparkLine (http://key-solutions.ca/cksparkline.html) and the full-on core-plot.

Implementing the whole Tapku library is overkill to just use the chart and in fact can be a little difficult for beginners too.

To implement the graph follow the following steps:

Screenshot2010-03-08at10.05.25.OmxZv34TWnLc.jpg

1.Copy the following files out of the Tapku Library and copy into your project:

GraphController.h & GraphController.m
TKGraphController.h & TKGraphController.m
TKGraphView.h & TKGraphView.m
TKGlobal.h & TKGlobal.m
UIImageAdditions.h & UIImageAdditions.m
UIViewAdditions.h & UIImageAdditions.m
TapkuLibrary.bundle (for the graphics)

2. Add the framework QuartzCore

#import <QuartzCore/QuartzCore.h>


3. In GraphController.h, remove the line to import the full Tapku Library and import TKGraphController.h and TKGraphView.h.

Alter GraphController.h to:

//#import <TapkuLibrary/TapkuLibrary.h>
#import “TKGraphController.h”
#import “TKGraphView.h”


4. Now to call the graph, we display it as a modal view controller. Use the following code in your project to push the view onto the screen:

In your header file add:
#import
“GraphController.h”

In the main add

UIViewController *vc = [[GraphController alloc] init];

[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[
self presentModalViewController:vc animated:YES];
[vc
release];
return;


And thats it!

I have a sample xcode project available too.

View Comments

iPhoneOS Addressbook Create New Client & Get Data

5/03/2010


I just finished a snippet of code for an upcoming 1 day iphonedev project which is very simple. The snippet needs to show the user an add address book contact box, let the user add the contact info and then Dismiss the add contact window to get on with the rest of the app, and of course saving the data into memory.
The code is attached below, and be sure to be watching the NSLog.




View Comments

iPhoneOS iPad Development Resources

4/03/2010

Code

General Cocoa Programming Resources


When in Trouble


My Favorite Libraries

  • Core Plot – Graphing Library
  • Three20 – The Famous Library with all the Facebook’y goodness
  • Tapku Library – An awesome set of tools – Nice Plot, Calendars, Loading HUD, EmptyView
  • Flurry – Awesome App Analytics
  • CKSparkLine – Basic Line Graph in a View
  • AppiRater – Add App Rating Reminder
  • Open Flow – Open Implementation of cover flow
  • InAppSettings – Include app settings inside the app easily
  • RegExKit – Objective-C Goodness


Address Book Integration


Email Integration


Useful Code


UI

UITableView Pretty Section Titles


Design


Sales

Application Sales Metrics

View Comments

iPhone/iPad Development – Screen Rotation Resizing

3/03/2010

There comes a time in many apps where the screen will be rotated and the view will resize to the new screen layout. This will be even more important when it comes to the iPad which recommends that the app is usable on all different orientations.

On rotation, I call a method that then goes and tediously shuffles the frames of all my onscreen items for landscape, and then it needs to be done again when going back to portrait. (There should be a class out there somewhere which automates all this).

The important part is getting the X,Y,Width,Height data of all the frames of the objects and I have lately adapted code to generate the following:

if ([[self.view subviews] count] &gt; 0) { <br />
NSLog(@"Enumerating Subview Details\n%@",[self.view subviews]); <br />
}

<p>What is does is print (to NSLog) a list of all the subviews in the main view. It makes getting the frame X,Y,W,H data much faster. Hope it helps!</p>

View Comments

iPhone UI – About Page Trends & Roundup

2/03/2010

iPhone about pages have become an art in theirselves. Below is a roundup of all the ones I’ve found interesting…More added as they come.

View Comments

Some Simple iPhone UI Elements

1/03/2010

I’ve been a little busy lately and have some ideas queued up but I havent had the chance to post much. Today something quick.

When developing my first app, I had some simple questions I could only answer by looking at what apps already do. Questions like “How is a single text field and keyboard on a page displayed to the user? How is a date best added/altered in a view? How can I enter in a full sized textbox for the user to enter a note? Insipration Source: Things from Cultured Code. They use some basic iPhone UI elements, but its just done so well.

View Comments

iPhone OS Development – deSimpleChart

20/02/2010

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”

View Comments

iPhone SDK FirstRun.png – Displaying Special ‘one off’ info in an app

19/02/2010

In making MicroCRM, I wanted to display some temp information to the user when a table list was empty. I wanted to make it as clear as possible a ‘lead’, ‘job’ or ‘invoice’ needs to be added first.

This is not a new concept in iPhone/Mac apps, but I want to share my implementation.

1. Firstly, I made the picture I wanted to display in my app.  Then it was added to my Xcode project. Below is the actual artwork the app uses.

2. The following code is placed in  (void)viewWillAppear: for the ViewController I wanted to place the image. (I have commented it well to help the reader).

//de.009 - FirstRun Image if ArrayCount is 0

[firstRunImage removeFromSuperview];

int InvoiceCount = [appDelegate.Array count];    //get number of Invoices<br />
NSString *arraycount = [NSString stringWithFormat:@"%d", InvoiceCount];<br />
NSLog (@"Leads Page Loaded: %@ Entries",arraycount);    //Log code for number of items in array


if (InvoiceCount==0) {       //If there are no invoices<br />
NSLog (@"Adding First Run Image");     //Log code<br />
firstRunImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img-FirstRun.png"]];     //Allocate UIImageView to hold the image, and define the image<br />
firstRunImage.frame = CGRectMake(0,0,320,100);      //Define Frame Size<br />
[self.view addSubview:firstRunImage];    //Add ImageView to current view<br />
} else {<br />
[firstRunImage removeFromSuperview];    //Otherwise remove it<br />
[firstRunImage release], firstRunImage=nil;  //release memory<br />
}

See it in action

And thats it. I have the same code for all three sections of my app. To get the app, goto http://microcrmapp.com and follow the app store link.

Other implementations of a similar idea are found in many different apps. Apple’s own included apps, the facebook app and so on. There is a particularly common UI element that seems to be an imitation of apple’s own included ‘no data/history/updating’ page (see below). This is becoming more and more common place and it does seem to be a nice way to show the user ‘theres nothing!’.

I have even found a way to implement this, its using the amazing TapkuLibrary. I’ll let you work it all out for yourself now and it may even become a new post in the future

View Comments

iPhone Development – inAppSettings – Allowing Settings from Inside your app to be the same as the Settings Outside your App

19/02/2010

My latest app MicroCRM implements inAppSettings to easily manage the app settings. InAppSettings is all about not duplicating the existing settings view in the iPhone SDK (using Settings.bundle). It allows for a simple call inside your app to open up the settings page as a modal view.

InAppSettings lives over at bitbucket, made by InScopeApps. I chose it over InAppSettingsKit, because it was just that little bit easier to use. As far as I see, they both do almost exactly the same thing. There is a head to head VS here.

The project even comes with an elaborate settings.bundle with all sorts of example settings in it. I just modified this (cutting out heaps).

Installation

  • Dragged the InAppSettings folder into my Xcode project (copying files)
  • Added #import “InAppSettings.h” to my appDelegate
  • Hooked up a button in my app to the below IBAction presentSettings (in Interface Builder)
  • Then modified settings.bundle to contain the settings my app was going to use

Example Code

- (IBAction)presentSettings {
InAppSettingsModalViewController *settings = [[InAppSettingsModalViewController alloc] init];
[self presentModalViewController:settings animated:YES];
[settings release];
}


View Comments