0

Okay. My app depends on having an active internet connection. When there isn't one, you may as well not be using it. Originally I was just going to present the user an alertView with options "retry connection" and "close app", but after a little research I found that Apple frown on apps closing themselves. So instead I decided to present an alertView with options "retry connection" and "wait", where the wait would take you to a relatively dull viewController, which would check for an internet connection periodically (once every 15 seconds or so). The idea of that is to bore the user into exiting the app for me with the home button, or to resume gameplay on the off chance the internet connection is re-established.

However, as you can imagine the internet access could go at any given time (when you're on any given view controller). This means that using segues is out of the question, because I would have to do a stupid amount of them, all going from every viewController to this one wait viewController.

Got any ideas? Is there a [self gotoViewControllerWithTitle:@"wait"]; command I don't know about?

I was thinking I could make all of my internet related stuff happen in one view controller I guess and just pass values to the other VCs if it came to it, but that seems very limiting/infantile.

Thank you in advanced and any help is appreciated :).

Greg Cawthorne
  • 396
  • 5
  • 21

3 Answers3

2

Have your app delegate detect no internet, and present your Modal "No Internet" view controller. Then when internet returns "dismissModalViewController" and hey presto, your previous view controller is underneath.

Daniel Broad
  • 2,512
  • 18
  • 14
  • Okay. This seems like an effective answer. How would I go about getting my app delegate to carry out the check and presentation of the view controller? I have implemented a method that checks the connection in each viewController individually. Would I be using an NSTimer to periodically check the connection in the app delegate? and how would I get it to present the view controller? I have only worked with 'performSegueWithIdentifier:' before and that requires a segue to be present. Is there a 'presentViewControllerWithIdentifier:'? – Greg Cawthorne Sep 01 '12 at 15:21
  • 1
    Look up "Reachability" to check for internet, you just alloc/init a view controller then [self.rootViewController presentViewController:xxx]. – Daniel Broad Sep 01 '12 at 16:57
  • Thanks. I have imported Reachable and followed the chosen anser on this thread: http://stackoverflow.com/questions/3790957/reachability-guide-for-ios-4 Everything looks good, apart from a yellow triangle saying "incomplete implementation". Then when I press run I get ten red errors coming from the Reachable.m file, saying things like "ARC forbids explicit message send of release', 'NSAutoReleasePool is unavailable in automatic counting reference mode' and 'Cast of C pointer of type 'void *': to objective-c pointer type 'Reachability*' etc. Any ideas? Maybe the Reachable files are out of date? – Greg Cawthorne Sep 01 '12 at 22:55
1

You could subclass 'UIViewController' to create your own base 'UIViewController' to have a generic method that checks the connection and on disconnect calls a modal view controller saying that the user has lost connection. And then maybe even pop to the root view controller. Then subclass that class and then every UIView Controller will now have the functionality you need. OOP FTW

On a side note, having a program that will only function with a working internet connection is a really bad idea. It's hard to keep the users hooked to an app when they'll be forced to quit the app when they lose connection.

brianSan
  • 545
  • 6
  • 17
  • Thanks. I am relatively new to objective-C and OOP. How would I go about creating subclasses etc? Your solution sounds effective, but unfortunately I would be at a loss on how to implement it. I will try dorada's solution first, since that seems the easier option, but I would still appreciate a more detailed explanation of your solution if you could be so kind, since that method of coding would be very useful for me to master. – Greg Cawthorne Sep 01 '12 at 15:17
  • On a second note, your comment about it being a bad idea to have an app that has no functionality without an internet connection, seems very valid. I may in fact work on implementing an offline mode with limited features. However the original question still stands, as it is a skill that could be applied to many different scenarios. – Greg Cawthorne Sep 01 '12 at 15:24
  • Hey, sorry for the long response time... But to subclass, all you have to do is ⌘+N and create a new Objective-C class. Then under `Subclass of:` type in the class you want to inherit from. It can be any of the Foundation/UIKit classes or it can be from your own user-defined classes. On one final note, being relatively new to obj-c and oop, I would suggest trying to undergo small projects and funtionality before trying to make a full blown app. Odds are, it won't be a very effective app and your reputation as an app developer will suffer greatly from that first app. Learn your stuff first. – brianSan Sep 03 '12 at 16:08
0

Make your "no connection view controller" a singleton object. Then you can access this same instance using a class methods (e.g : [MyNoConnectionViewController shareInstance]).

Samir
  • 627
  • 4
  • 9
  • 1
    I wouldn't use a singleton here. It's bad for code reuse and it creates an unnecessary dependency. – Rengers Sep 01 '12 at 14:53