Close app when internet is not available

后端 未结 4 1010
北荒
北荒 2020-12-16 23:19

I want to close my app when an Internet connection is not available.

I check that, but how can I create an alert, and then close my app?

4条回答
  •  隐瞒了意图╮
    2020-12-16 23:54

    Instead of closing it, consider explaining the situation to the user by the means of a popup.

    First of all, download Reachability from Apple.

    Add the classes Reachability.h,.m,delegates to your project. Then in your .m class import Reachability

    #import "Reachability.h"
    

    And in viewWillAppear or when you should display the alert:

    //Connection check
        Reachability *reach = [Reachability reachabilityForInternetConnection]; 
        NetworkStatus netStatus = [reach currentReachabilityStatus];    
        if (netStatus == NotReachable)
        {   
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Explain the situation to the user" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
            [alert show];
            [alert release];       
    
        }    
        else {
        //other actions.
        }
    

    As others said before me.

提交回复
热议问题