【问题标题】:Check internet connection in iOS app only once仅在 iOS 应用程序中检查一次互联网连接
【发布时间】:2015-08-19 08:01:54
【问题描述】:

为了检查我的 iOS 应用中的互联网连接,我做了以下操作:

导入 SystemConfiguration.framework

在我的项目中添加来自 https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html 的 Reachability.h 和 Reachability.m

然后我在 ViewController.m 中添加了这个:

#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (![self connected])
    {
        // not connected
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else
    {        
    }
}

我在应用中打开的每个视图(控制器)都会出现警报。那个神经。它应该只出现在一个视图(需要互联网连接)或只出现一次(启动应用程序或互联网连接中断时)。 (或任何其他想法?)

有没有办法做到这一点?

【问题讨论】:

  • 为什么?您可以随时断开连接。检查您何时真正需要使用连接(和/或处理连接错误)。
  • 我不太明白你的意思。 “检查您何时真正需要使用连接”:我该怎么做?
  • 您可能正在继承您的 ViewController 类?然后摆脱警报,只将其添加到您想要的视图控制器中。
  • [alert show];添加断点,看看是谁调用的。

标签: ios connection


【解决方案1】:

我通过为集成了我的主页的 ViewController 创建一个自己的 HomeViewController.h/HomeViewController.m 文件解决了这个问题。在那里我添加了 alert-if-no-internet-connection 代码。

【讨论】:

    【解决方案2】:

    而不是UIAlertView,您应该放置一个布尔值,如果您有互联网连接,则为真,如果您没有,则为假,当您想要发出请求时,检查布尔值并显示警报或做您想做的事.

    如果您只想在第一次打开 ViewController 时执行一次,您可以这样做

    - (BOOL)connected
    {
        Reachability *reachability = [Reachability reachabilityForInternetConnection];
        NetworkStatus networkStatus = [reachability currentReachabilityStatus];
        return !(networkStatus == NotReachable);
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        if (![self connected])
        {
            if(hasPresentedAlert == false){
    
                // not connected
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No internet connection!" message:@"Check internet connection!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
                 [alert show];
                 hasPresentedAlert = true;
             }
        } else
        {        
        }
    }
    

    并在接口中声明布尔值

    @interface ViewController (){
         BOOL hasPresentedAlert;
    }
    

    【讨论】:

    • 感谢您的回答。我的实际代码和你的“想法”到底有什么不同?
    • 不同之处在于它只会显示一次,当您推送另一个 ViewController 时,您不需要实现 Reachability 方法,因为它会显示来自 ViewController 的警报,该警报将在您的堆栈中。这就是“想法”
    • 谢谢,我试试。在哪里添加“@interface ViewController (){ BOOL hasPresentedAlert; }” ViewController.h 或 ViewController.m?
    • 在 .h 或 .m 中您看到此行的位置 @interface ViewController 添加此行 ** BOOL hasPresentedAlert;**。我建议您阅读或查看一些教程以更好地理解。
    【解决方案3】:

    在应用委托类中,您应该编写 (BOOL) 连接函数,在视图控制器类中,您只需从应用委托访问此函数。

    -(void) viewDidLoad:(BOOL)动画{

    [超级 viewDidLoad:animated];

    AppDelegate * 应用程序=(应用程序委托 *)[[UIApplication sharedApplication]delegate];

    if ([app connected]) {

    // 未连接

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"没有网络连接!"消息:@“检查互联网连接!” delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];

    [警报显示];

    }

    }

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      相关资源
      最近更新 更多