【代码笔记】iOS-网络嗅探

本文介绍了一个iOS应用如何通过Reachability类监测网络连接状态,并根据不同网络类型显示提示信息的方法。包括了AppDelegate类的实现细节,以及如何在应用启动时初始化网络监测功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,效果图。

二,工程图。

三,代码。

AppDelegate.h

复制代码
#import <UIKit/UIKit.h>
#import "Reachability.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
     Reachability *reachability;
     BOOL WarningViaWWAN;
}

@property (strong, nonatomic) UIWindow *window;


- (void)ReachabilitySniff:(Reachability*) curReach;
- (void)ReachabilitySniffNotification:(NSNotification* )notification;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

@end
复制代码

 

AppDelegate.m

复制代码
#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    RootViewController *rootVC=[[RootViewController alloc]init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:rootVC];
    self.window.rootViewController=nav;
    
    
    //启动网络嗅探功能
    WarningViaWWAN = TRUE;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReachabilitySniffNotification:) name:kReachabilityChangedNotification object:nil];
    
    if (!reachability) {
        reachability = [Reachability reachabilityForInternetConnection];
    }
    [reachability startNotifier];
    
    [self performSelector:@selector(ReachabilitySniff:) withObject:reachability afterDelay:20];

    
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
#pragma mark -网络嗅探
- (void)ReachabilitySniffNotification:(NSNotification* )notification
{
    Reachability* curReach = [notification object];
    [self performSelector:@selector(ReachabilitySniff:) withObject:curReach afterDelay:2];
}

- (void)ReachabilitySniff:(Reachability*) curReach
{
    NSLog(@"ReachabilitySniffNewWorkStatus");
    if (!curReach) {
        return;
    }
    NetworkStatus status = [curReach currentReachabilityStatus];
    switch (status) {
        case ReachableViaWiFi:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您正在使用WiFi网络" message:Nil delegate:self cancelButtonTitle:Nil otherButtonTitles:@"本次不再提醒",@"知道了", nil];
            [alert show];
            break;
        }
        case ReachableViaWWAN:
        {
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您正在使用移动网络,运营商会收取流量费,建议使用WiFi网络" message:Nil delegate:self cancelButtonTitle:Nil otherButtonTitles:@"本次不再提醒",@"知道了", nil];
                [alert show];
             break;
        }
        case NotReachable:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"没有网络" message:Nil delegate:self cancelButtonTitle:Nil otherButtonTitles:@"本次不再提醒",@"知道了", nil];
            [alert show];
            break;
        }
    }
}
复制代码

 

转载于:https://www.cnblogs.com/yang-guang-girl/p/5602484.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值