【问题标题】:Internet reachability not working properly in iPhone互联网可访问性在 iPhone 中无法正常工作
【发布时间】:2016-02-11 17:34:10
【问题描述】:

我正在开发一个持续跟踪互联网连接并上传一些数据的应用程序,该应用程序具有以下功能:当互联网不可用时,它会离线保存数据(照片),而在互联网可用时它会上传数据。我的应用程序运行良好,但有时它不能检查互联网,当我关闭设备 wifi 并再次打开时,它正在工作,所以有人可以告诉我这里有什么问题让我卡住了吗?我的可达性代码如下:

代码

- (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;
    }
}

【问题讨论】:

    标签: ios objective-c iphone reachability


    【解决方案1】:

    试试这个代码

    将以下代码放入您的 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;
            }
        }
    }
    

    希望对你有用

    【讨论】:

    • 你好 nikunj 非常感谢你的快速回答,我刚刚注意到当我关闭并再次打开我的 iPhone 的 wifi 时,然后上传数据工作正常,你能告诉我是什么问题吗?我必须把你的代码放在哪里亲爱的?请帮助我,我从 1 周开始就坚持这一点。我希望你能帮助我......:(
    • 您的代码已被弃用,此代码无法正常工作。如果我的代码正常工作,请投票并接受我的回答@jigarjims7
    • 好的兄弟让我先试试这个。如果问题仍然存在,希望你能提供帮助。:)
    • 你好,同样的问题仍然存在..请帮助我,问题是我第一次打开 wifi 时,上传不工作,但是当我关闭并再次打开 wifi 上传开始..并且工作正常。
    • 我在 appdelegate 中使用了可达性,并在特定时间使用了一个计时器来检查互联网。我再次想解释一下我的情况是,当我在离线模式下上传 y 媒体时,它保存在设备中,然后我正在从后台杀死应用程序并打开wifi,然后重新打开应用程序,所以现在互联网即将到来,但在这种情况下它无法正常工作..
    【解决方案2】:

    您的代码的问题是您可能没有观察到任何连接变化。

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

    您可以使用 Apple 提供的 Reachabilityhere,而不是手动检查连接。

    您可以导入该类,然后在 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");
        }
    }
    

    编辑:
    您定义以下这 2 个场景来测试您的代码。

    1. 它正在工作,以防我杀死应用程序,重新打开应用程序,然后我打开 wifi
    2. 当我第一次打开 wifi 然后重新打开应用程序时,它无法正常工作

    在场景 1 中,您的观察者知道连接发生了变化,因为应用程序仍然存在。但是,在场景 2 中,您的应用没有任何有关连接更改的信息,因为在您打开应用后没有任何更改。

    如果您想在场景 2 中实现您想要的(将媒体上传到服务器),那么可达性更改不是您需要的工具。

    【讨论】:

    • 我在 appdelegate 中使用了可达性,并在特定时间使用了一个计时器来检查互联网。我再次想解释一下我的情况是,当我在离线模式下上传 y 媒体时,它保存在设备中,然后我正在从后台杀死应用程序并打开 wifi,然后重新打开应用程序,所以现在互联网即将到来,但在这种情况下它无法正常工作
    • 你的场景是这样的。 1. 应用离线-> 保存到设备 2. 你杀死应用 3. 你打开连接 4. 你重新打开应用。由于在这四个阶段根本没有运行连接通知,因此您的代码肯定不会工作。尝试跳过第 2 步并在不终止应用的情况下打开连接。
    • 那么如果我不想跳过第 2 步该怎么办?正如我已经知道通过跳过第 2 步我的代码运行良好,这就是为什么我针对特定案例发布此问题。它正在运行watsapp..!
    • 它可以工作,以防我杀死应用程序,重新打开应用程序然后打开wifi,但是当我第一次打开wifi然后重新打开应用程序时,它不起作用……!!!有什么想法吗?
    • 这值得另一个问题。我想到的一种策略是在本地数据库中为您的媒体实施自定义标志。所以类似于isMediaSentToServer 并在didFinishLaunchingWithOptions 函数期间继续将媒体发送到服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    相关资源
    最近更新 更多