iOS 4 & iOS 5 tutorial: UIAlertView with link to WIFI or Settings app using Reachability and XCode 4.2

Hi!

This tutorial is about how to make your apps UX just a little bit better. Have you ever noticed the message in google maps stating that if you activate Wifi you would get a better positioning? Or that when you want to send a text message and your phone is on flight mode, that you get the message to de-activate flight mode in order to send a text?

Well, it is actually really simple. These are just links that are opened. So, in this tutorial I will show you how to do this using e.g. Reachability.

What you will learn

You will learn how to create a UIAlertView when no WIFI network is connected. The UIAlertView lets the user open the wifi settings directly from the app. You will also learn how to open other settings directly from your application.

What I expect from you

I expect you know how to use XCode and that you have some basic knowledge of Objective-C and development for iOS. A pro is if you have used UIAlertViews before.

What is required

Not more than a Mac with XCode 4.2+ installed

Good to know

I will be writing this for iOS 5 and be using ARC (Automatic Reference Counting), so you will not see any autoreleases, releases, deallocs ect.

So, lets start

First-off create a project called “AlertViewTest”. Make it a “Single View Application”. For the ease of this tutorial I chose only to support iPhone, so I recommend you do the same. However, this should work in a universal or iPad application as well.

So, now you will have your basic project. Nothing fancy, and nothing fancy needed.
Continue to next page …

Reachability

The next thing you need to do is to download a sample code from Apple. This sample code is called “Reachability” and is really usefull for your UX. You can download the code from http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html. But in case you do not have an paid subscription at Apple, you can also download it here: https://github.com/pokeb/asi-http-request/tree/master/External/Reachability

Download the file and unzip it (if downloaded from apple). Open the directory and drag the files “Reachability.m” and “Reachability.h” into XCode, just beneath the “AlertViewTest” group. Make sure you check the checkbox for “Copy items into destination group’s folder (if needed)”, see Pic.1.

Now your groups and files list should look like this (Pic.2)

Now, if you build your project you will notice a lot of build errors. This is due to several reasons; first-off you do not have the correct frameworks (yet) and second, the sample code is not written for the ARC standard. When using ARC, you are not allowed to allocate and release any objects. However, we can tell XCode not to expect ARC for these files. Lets start with that.

Left click on your project under “Project navigator”. The right side of xcode will change to the following (Pic.3)

Now, click “AlertViewTest” under targets, and then click “Build Phases”. You will see a new list of settings. Expand “Compile sources (4 items)” and double click on “Reachability.m”. Now a small window will popup with a textbox. In that text box write: “-fno-objc-arc” (without “) and press ENTER.

Since we are on that page anyways, expand “Link Binary With Libraries (3 items)”. Now click the + in the bottom left of that box. An new window will apprear. In this window, search for “SystemConfiguration.framework” and add it (Pic.4)

The contents of that page should now look like Pic.5.

Now you are all set; if you build your project now it should succeed. Lets start with some awesome coding.

Checking for WIFI connection

So, its time to write some code. Lets start with adding a function in our viewcontroller. Left click on “ViewController.h. Now add the following code beneath #import <UIKit/UIKit.h>:

and add the following code between @interface … and @end.

Now left click on “ViewController.m” and add the following code just above @end.

So, what does it do? Well, I am not going into every detail.. but basically what it does is that it askes the class Reachablitiy if there is a WIFI connection available. If there isn’t it will show an UIAlertView with a localized message. The message will offcourse be in english until you add translations.

But, if you run you application now, nothing will happen. What you also need to add is the following code in your “- (void) viewDidLoad” method:

Your code for this page should now look like:

Now, run your application and you will see the following (Pic.6):

Nice! Wouldn’t you say? No… not really since the buttons don’t do anything.. yet.

Now we need to implement the delegate function. Left click on your “ViewController.h” and add the following code directly after “@interface ViewController : UIViewController”

Your ViewController.h file should now look like:

Now, left click on ViewController.m again and add the following code just above @end:

So what does this do then? Well, this is a delegate function from UIAlertView. When the UIAlertView did dismiss (someone pressed a button) we know which button has been pressed. Since “Cancel” has index 0 (first button) and our first real button we added as the show settings button, we just need to check if the buttonIndex is 1. If so, we tell the application to open an URL. However, the URL is a URLScheme. This means that when launcing this URL it will do something else then open safari.

There are a numerous type of URLSchemes, and you can even create your own (more on that some other time).

So, if you run your application now, you will open the wifi settings page in the settings app. However, the simulator does not support this so it will just open the front page of the settings app.

What else can you open?

Well, basicly anything. There is a great site listing many URLSchemes, and on this page of it you can find any page you can open: http://handleopenurl.com/scheme/apple-settings-app

So, I hope you enjoyed the tutorial. If so, please Facebook like it(!!) and also, Please Flattr it(!).

You can download the source here: Download source ZIP file here

Best regards,
Paul Peelen

Posted in Apple, Development, Free stuff, iOS, iPhone, Mac, Objective-c, Tutorials, XCode Tagged with: , , , , , , , , , ,
20 comments on “iOS 4 & iOS 5 tutorial: UIAlertView with link to WIFI or Settings app using Reachability and XCode 4.2
  1. Hi Paul,

    Great tutorial. Thanks for mentioning us!

  2. Paul Peelen says:

    Your welcome!
    Great services deserve great acknowledgment!

  3. phil mahoney says:

    Nice implementation, is there any way to make this Asynchronous, so that if the connection drops the alert will pop up?

  4. Paul Peelen says:

    I think there is, however I have not yet done that. Have a look on whether it is possible using the Reachability class.

  5. Patrick Aguillon says:

    Nice article !

    Anyway, I think you cannot open settings from your application on iOS4. Can you ? I tried to open url on iPad1 4.2 and it failed. Maybe you should add a short note about that 😉

    Works fine on iOS5 🙂

  6. Paul Peelen says:

    Could be, I haven’t tested in on iOS4. Thanks!

  7. Jm says:

    I’m currently developing a simple app when users click on “Update Database”, it will check if the user is connected to their VPN. If not, it will redirect users to the settings page and ask them to connect to VPN first.

    Any ideas how? Please help.

  8. badre says:

    awesome. easy easy… more of you!

  9. Paul Peelen says:

    Sorry… not at the top of my head. Check what the Reachability class can do for you, it might support it.

  10. Iosvn says:

    that’s really helpful but i can’t access page 2…
    please share it for me! thanks you

  11. abdul nasir b.a says:

    thanks for giving light in my brain, i really enjoyed this tutorial… thanks a lot…

  12. abdul nasir b.a says:

    but…… while come to the coding am struggling. everything is fine but it’s not going to setting page. i test this in iPhone 4s (iOS 5.1) but it’s not working. NSURL *url = [NSURL URLWithString:@”prefs:root=General&path=Network”];
    [[UIApplication sharedApplication] openURL:url]; this is the code i written in didDismissWithButtonIndex

  13. Paul Peelen says:

    I’ll have a look. Most likely some WP problem

  14. jue_jiang says:

    So after going to the wifi setting view, how to go back to the former view, as I do not find any back button on the wifi setting view.

  15. Robert says:

    Also having problems to make it work in iOS 5.1

  16. JaySea says:

    no more available in iOS5.1
    because of Apple disabled it…

  17. FYI You have htmlentities showing in your sourcecode for the delegate declaration

  18. Matteo says:

    Just checked now in xcode 5, it works perfectly.

    this code will make the alert pop up only when you try to open a view which need a connection. how about to make a “general” code which can work all over the application (i.e. also if i am on a table view) so get an alert in every moment and in every app’s page if ther’s no connection?

    excuse me for my english

    Thanks a lot!

  19. Philbert Pascal says:

    Hello Paul,
    I am new in the Xcode environment. I électonicien and I just wrote my first application. I finished the interface.
    By cons I have two functions to write and my Xcode limits that are felt. I have to be able to view a WiFi network list and choose.
    And the second function and send a value between 1 and 60 on the same network WiFi. Can you help me. Thank you in advance.
    Pascal
    Not That Email: pascal.philbert1@free.fr
    Ps: I can send you my application under Xcode6.

  20. Paul Peelen says:

    @Philbert Sorry for my late reply. I guess by now you have figures out your problem, or moved on. Otherwise, please let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

 

Categories