Require Wifi

Discussion in 'iOS Programming' started by newtoiphonesdk, Mar 15, 2011.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I want to make one of the Views of my app require a Wifi Connection. What do I need to do to make this work? Like, just an alert pops up when no wifi network is connected, and prompts you to turn it on before it will load the page?
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    There is a function which checks for wifi or wan connections, u can go like
    ifWifiIsAvaible (don't know exact name), and it should be already connected
    {
    //Do stuff
    } else {
    UIAlertView *alert here. with telling u to to turn wifi on.
    Do this so ur app won't crash when trying to load data from internet without inet connection.
    }
     
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Ok, I have done this, and it works great, thank you. I do have a new issue though. This is how I have the Webview set up:
    Code:
    - (void)awakeFromNib
    {
    	Reachability *reachability = [Reachability reachabilityWithHostName:@"google.com"];
    	NetworkStatus status = [reachability currentReachabilityStatus];
    	
    	if (status == ReachableViaWiFi) {
    		[sermons loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.316apps.com/lewisville.php"]]];
    	}
    	
    	else {
    		UIAlertView *cancelled = [[UIAlertView alloc] initWithTitle:@"Wifi Required" message:@"To listen to the sermons, you must be connected to a Wifi Network." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    		[cancelled show];
    		[cancelled release];
    	}
    
    	timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
    	
    }
    It works great when wifi is not connected, but once I turn on wifi, and hit reload, it doesn't do anything. What do I need to change to allow it to try the webpage again without having to completely close out the app and restart?
     
  4. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #4
    You should really think about what you're trying to achieve here. You're basically sending out a request to an external site to make sure you'll have connectivity for your own site... What if google.com is down but the user has a good functionning wifi ? Your code would then fail to load the view.

    This is probably not what you want. Let the OS gracefully handle the lack of connection and inform the user that the page was not reached. UIWebView already does all of this for you, no need to "force check" connectivity.

    For your code not working when wifi is up, have you tried removing all your checks and just running this line in awakeFromNib :

    Code:
    - (void)awakeFromNib
    {
        [sermons loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.316apps.com/lewisville.php"]]];
        timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
    }
    
    What happens if you use this code and manually make sure Wifi is up ?
     
  5. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #5
    I understand what you mean about Google, I had that in there cause that's what tutorials I had read said to do. I just know that the website I want to show will not be approved by Apple because of being too data intensive. To get it in there, I know it needs to be running on wifi. I can get it to display an alertview for this when wifi is down, but I just dont know how to attempt to reload it once wifi is turned back on, without killing the background app all the way.
     
  6. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #6
    Why would Apple deny you from loading a webpage on 3G ? You can open Safari and type in that URL and no block in place will prevent it.

    Again, let's go back to your actual problem which is not being able to display your view when the network is up, have you tried removing the check ?
     
  7. newtoiphonesdk, Mar 16, 2011
    Last edited: Mar 16, 2011

    newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #7
    The reason they will not allow it is because it is too data intensive. Specifically, the mp3s that are being listened to download more than 5 mb of data in a 5 minute period of time. I know it doesn't make sense, but it is what Apple has told me on several occasions when they reject the app. My actual problem is not what you described. My problem is reloading the webpage after wifi is turned on. How do I do that?

    Update:
    Here is the exact wording for why they rejected it:
    cannot be posted to the App Store because it is transferring excessive volumes of data over the cellular network, and is not permitted under section 9.3 of the App Store Review Guidelines <https://developer.apple.com/appstore/resources/approval/guidelines.html>.
     
  8. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #8
    Ok, then the check will need to be there in the final version. However, let's come back to your current problem. You say when you reload the app with wifi on, it's not displaying anything.

    First we need to see whether your check is correct or not. Again, have you tried removing the check and see if the page loads ? If it does, we're in good shape, your loadRequest is working. If it doesn't, then we'll need to fix that.

    In other words, are you sure your code is ever trying to execute the loadRequest ?
     
  9. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #9
    The only time it attempts to load the page is when it frat starts in the awakefromnib command.
     
  10. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #10
    And it works if Wifi is enbaled at that time ? If it does, where else are you calling loadRequest if Wifi wasn't enabled during the call to awakeFromNib ?
     
  11. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #11
    Really don't know what else to say since I have sent you every bit of code I have for the webview and reachability.
     
  12. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #12
    If that is the case, your problem is easily figured out : You only check for connectivity once. You need to periodically check if connectivity is there through Wifi and then execute loadRequest when it is.

    It would seem you have started doing that :

    Code:
    [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
    Where is the code for tick and what does it do ?
     
  13. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #13
    Sorry it took me a while to get back. In an area with no Internet for a few days. The code for tick is simply to add the activity indicator to show that a page is loading so people know something is going on.
     
  14. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #14
    Reload webview if no view currently loaded?

    I have an app that will not load the webview unless wifi is on. I would now like this to try and load the webview again after the user turns on wifi. I was thinking I could link an ibaction to the tab bar item that would check if a URL is currently loaded. If so, it does nothing, if not, it will try to reload the original nsurl request. This way the user could exit app, turn on wifi, and when they come back, it would once more try to load the webview
     
  15. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #15
    Well, use the viewWillAppear delegate function of ur viewController.
    This should take care of that exactly, put a check in there, to see if ur wifi is avaible, if this is true, load the page, if it isn't, stop there.
    This will be called every time u will check the view, even if it's a nav controller. Don't use the viewDidLoad, because with the multitasking it will only load once, unless u kill app and reboot :)
     
  16. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #16
    Thanks. That works, though the only thing I don't like about it is it will reload every time someone navigates away from the tab, and then comes back to it. So far, I have just set up an IBOutlet for a UIButton and an IBAction attached to it. on viewDidLoad if wifi is active, it hides the button, and loads the page, if no wifi, it hides the page and shows the button, which gives an alert to turn on wifi, and then hit the reload button I built.
     
  17. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #17
    Exactly,
    or u could work with booleans, and do it whenever u please, programming is a matter of experimenting x)
     

Share This Page