首页 文章

Internet可访问性在iPhone中无法正常工作

提问于
浏览
1

我正在开发一个不断跟踪互联网连接并上传一些数据的应用程序,应用程序具有以下功能:当互联网不可用时,它会保存数据(照片)离线以及上传数据的互联网可用性 . 我的应用程序工作正常,但有时它没有chechking互联网,当我关闭我的设备wifi并再次打开,它正在工作,所以任何人都可以请告诉我这里有什么问题困扰我?我的可达性代码如下:

code

- (void)reachabilityCheck
{
    /* Internet checking  */
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    Reachability *reach = [Reachability reachabilityForInternetConnection];
    reach.reachableOnWWAN = YES;
    [reach startNotifier];
    NetworkStatus internetStatus = [reach currentReachabilityStatus];
    if (internetStatus != NotReachable) {
        //Develop By Payal
        if(self.internetConnection == 0)
        {
            NSLog(@"Connection active");
            self.internetConnection = 1;
        }
        //Develop By Payal Done
    }
    else {
        NSLog(@"Connection inactive");
        self.internetConnection = 0;
    }
}

2 回答

  • 0

    试试这个代码

    将波纹管代码放在 viewDidLoad 方法中,它会立即调用通知

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(checkNetworkStatus:)
                                                     name:kReachabilityChangedNotification object:nil];
    
    
    
    //Net Connection Chack
    - (void)checkNetworkStatus:(NSNotification *)notice
    {
        // called after network status changes
    
        NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
        switch (internetStatus)
        {
            case NotReachable:
            {
                NSLog(@"The internet is down.");
                UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Error" message:@"No Internet Connection\nPlease Check The Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alrt show];
    
                break;
            }
            case ReachableViaWiFi:
            {
                NSLog(@"The internet is working via WIFI");
                break;
            }
            case ReachableViaWWAN:
            {
                NSLog(@"The internet is working via WWAN!");
                break;
            }
        }
    }
    

    我希望这对你有用

  • 0

    您的代码的问题是您可能没有观察到任何连接更改 .

    我不确定你使用了什么触发器,以便在关闭和连接wifi时你的代码正常工作 .

    您可以使用Apple here提供的 Reachability 类,而不是使用计时器手动检查连接 .

    您可以导入该类,然后在viewDidLoad中注册任何连接更改(如果需要全局观察者,则注册appDelegate didFinishLaunchingWithOptions方法) .

    //AppDelegate.h
    @property (strong, nonatomic) Reachability *reachability;
    
    //AppDelegate.m
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.reachability = [Reachability reachabilityForInternetConnection];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityDidChange:) name:kReachabilityChangedNotification object:nil];
        [self.reachability startNotifier];
    }
    
    #pragma mark - Reachability notification
    - (void)reachabilityDidChange:(NSNotification *)notification {
        Reachability *reachability = (Reachability *)[notification object];
    
        if ([reachability isReachable]) {
            NSLog(@"There is connection");
        } else {
            NSLog(@"Unreachable");
        }
    }
    

    编辑:
    您定义以下两个方案来测试您的代码 .

    • 它正在工作以防我杀死应用程序,重新打开应用程序,然后我打开wifi

    • 当我第一次打开wifi然后重新打开应用程序时,它无法正常工作

    在方案1中,您的观察者知道连接更改,因为应用程序仍处于活动状态 . 但是,在方案2中,您的应用程序没有关于连接更改的任何信息,因为在您打开应用程序后没有任何更改 .

    如果要在方案2中实现所需(将介质上载到服务器),则可达性更改不是您需要的工具 .

相关问题