1

我创建了一个 iPhone 应用程序,我想在其中检查互联网连接。在我编写的应用程序委托方法的 didFinishLaunchingWithOptions 方法中

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" title:firstTabTitleGlobal bundle:nil];
    viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" title:secondTabTitleGlobal bundle:nil];

    newNavController = [[UINavigationController alloc] initWithRootViewController:viewController1];

    userNavController = [[UINavigationController alloc] initWithRootViewController:viewController2];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:newNavController,userNavController,nil]

    Reachability *r = [Reachability reachabilityWithHostName:globalHostName];

    NetworkStatus internetStatus = [r currentReachabilityStatus];

    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {
        [self showAlert:globalNetAlertTitle msg:globalNetAlertMsg];
        [activityIndicator stopAnimating];
    }
    else
    {
        [activityIndicator stopAnimating];
        self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
    }
}

我的代码没问题,因为当没有互联网连接时会显示警报。但问题是当没有内部人员时会显示 default.png。当我再次运行应用程序时,应用程序将从显示的 default.png 运行。什么也没有发生。提前致谢。

4

3 回答 3

2

application:didFinishLaunchingWithOptions:仅在启动应用程序时运行。

如果您希望您的应用程序在后续应用程序激活时检查可用性,请尝试将您的代码放入applicationDidBecomeActive:

于 2012-06-24T07:06:58.150 回答
1

更好的是使用 NSNotifications,动态地告诉你是否有连接。您可以使用名为“reachabilty”的苹果类来做到这一点。一旦你将文件包含在你的项目中,你就可以使用这样的东西;

//in viewDidOnload    
[[NSNotificationCenter defaultCenter] addObserver:self 
                                          selector:@selector(handleNetworkChange:) 
                                          name:kReachabilityChangedNotification object:nil];
reachability = [[Reachability reachabilityForInternetConnection] retain];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];

if (status == NotReachable) {
    //Do something offline
} else {
    //Do sometihng on line
}

- (void)handleNetworkChange:(NSNotification *)notice{
 NetworkStatus status = [reachability currentReachabilityStatus];
 if (status == NotReachable) {
  //Show offline image
 } else {
  //Hide offline image
 }

}

(这是来自Reachability 网络更改事件未触发的更正代码)

然后,一旦发生任何网络更改,您就可以更新您的图像。但是,不要忘记将自己从 dealloc 中的接收通知中删除;

[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];

如果您需要有关如何实现此功能的更多信息,我很乐意为您提供帮助!

乔纳森

于 2012-06-24T16:04:01.943 回答
0

这在 Swift 5 中效果很好。返回一个布尔值。它也适用于移动数据。

public class Reachability {

        class func isConnectedToNetwork() -> Bool {

            var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
            zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
            zeroAddress.sin_family = sa_family_t(AF_INET)

            let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
                $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                    SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
                }
            }

            var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
            if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
                return false
            }

            /* Only Working for WIFI
             let isReachable = flags == .reachable
             let needsConnection = flags == .connectionRequired

             return isReachable && !needsConnection
             */

            // Working for Cellular and WIFI
            let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
            let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
            let ret = (isReachable && !needsConnection)

            return ret

        }
    }

希望这可以帮助。

于 2019-08-01T19:09:25.870 回答