2

I am using Storyboard with a Navigation Controller and prepareforsegue. I have two UITableViews. If you click on a row in the first table you get to the second table. The second table picks its data from a plist depending which row you clicked in the first table. This works fine as long there is an internet connection. If there is no internet connection it crashes.

Now I want to check if there is an internet connection before the second table loads. And if there is no internet connection I want to display an UIAlertView.

I would like to do this with NSURLConnection but I don't know where to implement the code. Will I put this in the .m of the first table at prepareforsegue or and the .m of the second table?

Thanks.

halloway4b
  • 573
  • 1
  • 11
  • 23

1 Answers1

3

You should use Reachability Code Written By Apple.

Please Go Through This Link For Downloading the Reachability Files.

For using That Code You need to Import SystemConfiguration Framework.

Follow As Target->BuildPhase->LinkBinaryWithLibraries->Click"+"->Choose SystemConfiguration.

Then Import the #import "Reachability.h" Header in ViewController.

Then Just Write Few Lines Code Just before navigating To anotherView

Reachability *reach = [Reachability reachabilityForInternetConnection]; 
NetworkStatus netStatus = [reach currentReachabilityStatus];    
if (netStatus == NotReachable) {        
    NSLog(@"No internet connection!");
    UIAlertView *information = [[UIAlertView alloc] initWithTitle:@"Server Connection is not available" message:nil  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [information show];
    [information release];


} 
else {        
//Write your logic here Like As navigating to anotherview       
}
Kamar Shad
  • 6,089
  • 1
  • 29
  • 56
  • Looks good and I am trying it but I am using ARC with Xcode 4.3. It seems the Reachability from Apple is not compatible with Xcode 4.3. I already disabled ARC for the Reachability.m but I still got an error: Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1 – halloway4b Feb 28 '12 at 10:35
  • @halloway4b Read this Out Carefully http://stackoverflow.com/questions/7560239/xcode-command-developer-usr-bin-clang-failed-with-exit-code-1 – Kamar Shad Feb 28 '12 at 10:40
  • Well...I think I did not get it but thanks for the link. I just went through your shown example and got this. Please help. clang: error: no such file or directory: 'add' – halloway4b Feb 28 '12 at 10:54
  • I opened a brand new project with ARC as SingeView App and went through the steps you said and the same error appears. So it seems nothing wrong with all my code added. – halloway4b Feb 28 '12 at 11:06
  • 1
    it can't be because i have followed same and working fine .Just do one things Delete Reachability(.h and .m both) files. This Time Drop Both Files Directly into your Xcode Project Directly and Don't Forget To select "Copy Items Into destination Group's Folder" check Box. – Kamar Shad Feb 28 '12 at 11:36
  • @halloway4b Sorry two more Things Need To do if create this Newproject with ARC, (1)you will have to disable ARC for Reachability Flag As Target->Compile Sorces->Click on Reachability.m and Set "-fno-objc-arc" Flag .(2)Remove [information release] line From ViewController. – Kamar Shad Feb 28 '12 at 11:54
  • OK. Create a new project. Single View App. Name it InternetTest. Just for iPhone.Use Storyboard and ARC. Next I drop Reachability.h and Reachability.m direct into my project. Check "Copy Items Into destination Group's Folder". Add SystemConfiguration.Framework. Go to ViewController.h and #import "Reachability.h". Paste code from above to ViewController.m under ViewDidLoad. Go to Compile Sources and disable ARC for Reachability.m. Run and Compile and I got the error. – halloway4b Feb 28 '12 at 12:06
  • The same error like I mentioned above. Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/u‌​sr/bin/clang failed with exit code 1 . clang: error: no such file or directory: 'add – halloway4b Feb 28 '12 at 12:19
  • Here you find this App I created. http://dl.dropbox.com/u/9358444/TestInternet.zip – halloway4b Feb 28 '12 at 12:27
  • @halloway4b wait it's done , You did one mistake under the BuildPhase->Target->Compile Sorces->you need to write "-fno-objc-arc" instead of this "add-fno-objc-arc".Now It's Working – Kamar Shad Feb 28 '12 at 12:35
  • @halloway4b you welcome and if u really Like my Help then Accept my Answer. – Kamar Shad Feb 28 '12 at 12:42