2015-10-19 46 views
0

在iOS 9之后,我用来检查我的应用程序是否与我的服务器有下一部分代码连接,如果没有响应,我要求用户启动wifi等。在iOS 9中检查Internet连接失败?

警告显示对话框:

#pragma mark - SHOW ALERTVIEW FOR IOS 7 or less AND IOS 8 
-(void) alertNoInternet:(NSString*)alertTitle withMessage:(NSString *)alertMessage{ 

    NSString *alert3gButtonText = @"Mobile Data"; 
    NSString *alertWifiButtonText = @"WIFI"; 
    NSString *alertCancelButtonText = @"Cancel"; 


    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(NSFoundationVersionNumber_iOS_8_0)) { 
     NSLog(@"SQA: iOS 8 dialog process"); 
     /*id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController; 
     if([rootViewController isKindOfClass:[UINavigationController class]]) 
     { 
     rootViewController = [((UINavigationController *)rootViewController).viewControllers objectAtIndex:0]; 
     }*/ 

     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle 
                       message:alertMessage 
                      preferredStyle:UIAlertControllerStyleActionSheet]; 
     //We add buttons to the alert controller by creating UIAlertActions: 
     UIAlertAction *action3g = [UIAlertAction actionWithTitle:alert3gButtonText 
                  style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) 
            { 
             NSURL *urlCellular = [NSURL URLWithString:@"prefs:root=General&path=USAGE/CELLULAR_USAGE"]; 

             if([[UIApplication sharedApplication] canOpenURL:urlCellular]) { 
              [[UIApplication sharedApplication] openURL:urlCellular]; 
             } 


             [alertController dismissViewControllerAnimated:YES completion:nil]; 
             //home button press programmatically 
             UIApplication *app = [UIApplication sharedApplication]; 
             [app performSelector:@selector(suspend)]; 

             //wait 2 seconds while app is going background 
             [NSThread sleepForTimeInterval:2.0]; 

             //exit app when app is in background 
             exit(0); 
            }]; 

     //We add buttons to the alert controller by creating UIAlertActions: 
     UIAlertAction *actionWifi = [UIAlertAction actionWithTitle:alertWifiButtonText 
                  style:UIAlertActionStyleDefault 
                  handler:^(UIAlertAction * action) 
            { 
             NSURL *urlWifi = [NSURL URLWithString:@"prefs:root=WIFI"]; 

             if ([[UIApplication sharedApplication] canOpenURL:urlWifi]) { 
              [[UIApplication sharedApplication] openURL:urlWifi]; 
             } 

             [alertController dismissViewControllerAnimated:YES completion:nil]; 
             //home button press programmatically 
             UIApplication *app = [UIApplication sharedApplication]; 
             [app performSelector:@selector(suspend)]; 

             //wait 2 seconds while app is going background 
             [NSThread sleepForTimeInterval:2.0]; 

             //exit app when app is in background 
             exit(0); 
            }]; 


     //We add buttons to the alert controller by creating UIAlertActions: 
     UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:alertCancelButtonText 
                   style:UIAlertActionStyleCancel 
                  handler:^(UIAlertAction * action) 
             { 
              [alertController dismissViewControllerAnimated:YES completion:nil]; 
              //home button press programmatically 
              UIApplication *app = [UIApplication sharedApplication]; 
              [app performSelector:@selector(suspend)]; 

              //wait 2 seconds while app is going background 
              [NSThread sleepForTimeInterval:2.0]; 

              //exit app when app is in background 
              exit(0); 
             }]; 


     //[alertController addAction:action3g]; 
     [alertController addAction:actionWifi]; 
     [alertController addAction:actionCancel]; 
     [self presentViewController:alertController animated:YES completion:nil]; 
    } 

    if (SYSTEM_VERSION_LESS_THAN(NSFoundationVersionNumber_iOS_7_0)) { 
     NSLog(@"SQA: iOS 7 or less dialog process"); 

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle 
                  message:alertMessage 
                  delegate:nil 
                cancelButtonTitle:alertCancelButtonText 
                otherButtonTitles:alertWifiButtonText, nil]; 
     alertView.tag = TAG_ALERT_NOINTERNET; 
     [alertView show]; 
    } 
} 

这是源代码检查连接:

- (void)checkInternet:(connection)block 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

    NSURL *url = [NSURL URLWithString:@"http://www.tempdevserver.com/"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    request.HTTPMethod = @"HEAD"; 
    request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData; 
    request.timeoutInterval = 10.0; 

    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler: 
    ^(NSURLResponse *response, NSData *data, NSError *connectionError) 
    { 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
     block([(NSHTTPURLResponse *)response statusCode] == 200); 
    }]; 
} 

现在,iOS上的9,总是显示对话框,决不让我从那里经过。 改变了什么?

回答

2

这里是完美的解决方案,可能对您有所帮助。

**in your Appdelegate.m File** 

//Check Internet Plugin 
#include<unistd.h> 
#include<netdb.h> 
///// 

#pragma mark Check Internet connection 
-(BOOL)checkInternetConnection { 

    char *hostname; 
    struct hostent *hostinfo; 
    hostname = "google.com"; 
    hostinfo = gethostbyname (hostname); 
    if (hostinfo == NULL) 
    { 
     NSLog(@"-> no connection!\n"); 
     return NO; 
    } 
    else{ 
     NSLog(@"-> connection established!\n"); 
     return YES; 
    } 
} 

试试上面的代码,它做工精细,完美

+0

谢谢,这是一个解决方案。我不知道是否有另一种选择。谢谢Mehul。 – MAOL

+0

@Victor_J_Martin,我的荣幸。我们随时准备为您提供帮助。 – Mehul

+0

@Victor_J_Martin,你是从事iOS应用程序开发人员工作多长时间的? – Mehul