1

I am creating a "no internet connection". This is for opening an app without internet connection. So first I added this code to my bridge.h

import "Reachability.h"

Then add following method to reachability class. First declare in .h file :

+ (BOOL)checkIntenetRechable;  

and implement in .m file

+ (BOOL)checkIntenetRechable  
{  
    BOOL isInternetAvailable;  
    Reachability *internetReach = [Reachability reachabilityForInternetConnection];  
    [internetReach startNotifier];  

    NetworkStatus netStatus = [internetReach currentReachabilityStatus];  
    BOOL connectionRequired = [internetReach connectionRequired];  
    NSString *statusString = @"";  

    switch (netStatus)  
    {  
        case NotReachable:  
        {  
            statusString = @"Access Not Available";  
            isInternetAvailable = FALSE;  
            break;  
        }  
        case ReachableViaWWAN:  
        {  
            statusString = @"Reachable WWAN";  
            isInternetAvailable = TRUE;  
            break;  
        }  
        case ReachableViaWiFi:  
        {  
            statusString = @"Reachable WiFi";  
            isInternetAvailable = TRUE;  
            break;  
        }  
    }  

    if(connectionRequired)  
    {  
        statusString = [NSString stringWithFormat: @"%@, Connection Required", statusString];  
        isInternetAvailable = FALSE;  
    }  
    return isInternetAvailable;  
}  

Then wherever I want to check internet connection I put the following snippet there and alert user about it.

if Reachability.checkIntenetRechable() == false {  
    let alertView = UIAlertCole: "APP_NAME", message: "Please check your internet connection.", preferredStyle: UIAlertControllerStyle.Alert)  
    //alertView.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (action: UIAlertAction ) in }))  
    alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction ) in  
       // Put some code for okay button  
    }))  
    self.presentViewController(alertView, animated: true, completion: nil)  
}  

AND after awhile I got This error...what could be the problem??

D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
Baby mouse
  • 25
  • 3
  • Shouldn't this be tagged "objective-c" rather than "c++"? – Jesper Juhl Jul 23 '16 at 08:42
  • change UIAlertView = "app name", message: ... to UIAlertView("app name", message: ...) – Witterquick Jul 24 '16 at 06:29
  • Thanks for the suggestion @JesperJuhl. – Baby mouse Jul 25 '16 at 07:13
  • I'll try that one @Roee84 – Baby mouse Jul 25 '16 at 07:13
  • let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) – Saurabh Jain Jul 25 '16 at 07:17
  • 1
    @Roee84, don't leave answers as comments. Post them below as answers so they can be accepted. – Jim Jul 25 '16 at 07:20
  • 1
    @SaurabhJain, don't leave answers as comments. Post them below as answers so they can be accepted. – Jim Jul 25 '16 at 07:20
  • While this can be useful, preflight checks (especially network connectivity) are actively discouraged by apple. – Antzi Jul 25 '16 at 08:00
  • So what would be the best solution on this @Antzi – Baby mouse Jul 25 '16 at 08:08
  • This is an UX issue and it would be hard to tell without looking at your app. Trying to make the real request once you need it and then displaying an error could be the correct way. Try to think about what would be the smoothest for your users – Antzi Jul 25 '16 at 08:10

1 Answers1

0
    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
     alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil)) 
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
Saurabh Jain
  • 1,688
  • 14
  • 28
  • I got this [error](https://drive.google.com/file/d/0B0Je1_NFqcfXWHUwdGVXSkxZR1E/view?usp=sharing)..what could be the error?(https://drive.google.com/file/d/0B0Je1_NFqcfXWHUwdGVXSkxZR1E/view?usp=sharing) – Baby mouse Jul 25 '16 at 07:42
  • @Babymouse you need to present view controller on view controller only. u can do something like `self.window!.rootviewcontroller!.presentViewControoler(alert)` as you are writing this code in app delegate class – D4ttatraya Jul 25 '16 at 08:10
  • Thank you so much @SaurabhJain – Baby mouse Jul 25 '16 at 09:01
  • Thank you so much @DashAndRest – Baby mouse Jul 25 '16 at 09:01
  • @SaurabhJain is this applicable for my app whenever I open my app with no internet connection??Because what I'm experiencing now is that my app keeps on loading whenever I have no internet connection. – Baby mouse Jul 25 '16 at 09:06
  • Hi @SaurabhJain...I tested the app without internet connection (I opened the app without internet connection) and the activity indicator keeps on loading....I don't know what happened...Can you tell me how to fix this? – Baby mouse Jul 27 '16 at 02:27
  • @Babymouse It's simple.. the activity indicator show that it just finding the internet connection.. you should check the internet is available or not.http://stackoverflow.com/questions/30743408/check-for-internet-connection-in-swift-2-ios-9 – Saurabh Jain Jul 27 '16 at 04:47
  • Yeah, I know it @SaurabhJain....But why is that when I open the app without internet, it keeps on loading instead of the alert "no internet connection"? – Baby mouse Jul 27 '16 at 08:32
  • @Babymouse Can you give your code where you activity indicator show? It may be you don't stop the animation of activity indicator. – Saurabh Jain Jul 27 '16 at 10:39