如何使用didFinishLaunchingWithOptions iOS检查internet连接并停止应用程序

如何使用didFinishLaunchingWithOptions iOS检查internet连接并停止应用程序,ios,objective-c,appdelegate,Ios,Objective C,Appdelegate,我仍在学习iOS,只是我完成了我的第一个应用程序,但我想在应用程序运行时添加一些功能,这里我使用的是appdelegate中的did finish launching with option方法,我想更改此代码,首先检查用户是否有internet,如果没有显示uialertView,如果没有互联网,我需要一个功能,可以像停止应用程序 警告(此应用程序需要internet,而您现在没有internet,请稍后再试),应用程序将退出。 或者在某些情况下,web服务可能无法工作 如果可能,请解释我应该

我仍在学习iOS,只是我完成了我的第一个应用程序,但我想在应用程序运行时添加一些功能,这里我使用的是appdelegate中的did finish launching with option方法,我想更改此代码,首先检查用户是否有internet,如果没有显示uialertView,如果没有互联网,我需要一个功能,可以像停止应用程序

警告(此应用程序需要internet,而您现在没有internet,请稍后再试),应用程序将退出。 或者在某些情况下,web服务可能无法工作

如果可能,请解释我应该将if语句放在哪里,以及如何退出应用程序

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
       [[UINavigationBar appearance] setBarTintColor:[UIColor lightGrayColor]];




        NSFileManager *fileManger=[NSFileManager defaultManager];
        NSError *error;
        NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

        NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

        NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"istudentDatabase.plist"];




       // NSLog(@"plist path %@",destinationPath);

        //if Plist not exists will copy new one
        if ([fileManger fileExistsAtPath:destinationPath]){


           NSLog(@"Settings File exists ");



        }else{
            // Copy New Plist 
            NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"istudentDatabase.plist"];

          [fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];





        }



        settingsClass * settings =[[settingsClass alloc]init];


        NSNumber * userid = [settings loadPlist:[NSString stringWithFormat:@"userid"]];

        if ([userid intValue] == 0)
        {
            //NSLog(@"You Dont Have USerid ");
            // Send a synchronous request
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://fahads-macbook-pro.local/ios/newuser.php"]];
            NSURLResponse * response = nil;
            NSError * error = nil;
            NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
                                                  returningResponse:&response
                                                              error:&error];

            if (error == nil)
            {
    NSDictionary * mydata = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

                [settings saveNewUserId:[mydata[@"userid"] intValue]];

        NSLog(@"%@",mydata[@"userid"]);

            }else{
                NSLog(@"Error please Check Your Connections ");

            }


        }else{


            NSLog(@"You Have Userid : %@",userid);

        }


        NSMutableDictionary * itemsPlist = [[NSMutableDictionary alloc]initWithContentsOfFile:destinationPath];
        NSLog(@"Items : %@",itemsPlist);


        return YES;
    }
此外,如果无法退出应用程序,我有视图控制器,在该视图控制器上有一个按钮,我想用if语句从appdelegate中隐藏该按钮,例如,如果没有连接,则隐藏开始按钮并显示一些无连接的提示。


感谢advance

您无法阻止应用程序启动/退出应用程序。这是不可能的。那么我能为这个案子做些什么呢?我的应用程序正在使用web服务如果web服务不可用或internet连接出现问题,我如何解决此问题?@Geeker先生可能会给出一个很好的视图,显示“请连接到internet”此外,如果用户在午餐后连接,您还可以添加一些pull-to-refresh组件,这样他就可以将应用拉到refresh,并将应用作为意图使用午餐。使用退出(0)将从应用商店拒绝您的应用。只要弹出一个对话框,告诉用户没有互联网,如果每次应用程序出现在前台时都检查连接/尝试建立连接,则无需添加下拉刷新,因为如果用户禁用了连接,他们需要从应用程序中切换以重新打开。然后,如果他们返回到您的应用程序,您将有机会在appBecameActive中再次执行网络活动。请同时执行这两项操作。可达性的通知很有用,因为它们告诉您何时重试,因为设备的网络连接正在工作。当设备的网络连接正在工作,但Internet上其他地方的某个错误中断了连接时,用户请求的重试(如拉动刷新)非常有用。可达性无法告诉您服务器本身或网络的其他部分。