您的位置:68399皇家赌场 > 域名注册 > IOS剖断互连网情况,ios互联网蒙受

IOS剖断互连网情况,ios互联网蒙受

发布时间:2019-08-10 15:06编辑:域名注册浏览(100)

    Reachability

    Apple 的 例程 Reachability 中牵线了获得/检验互联网状态的方法。

    在你的先后中采用

    1、Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中; 2、然后将 SystemConfiguration.framework 增多进工程。

    Reachability 中定义了3种网络状态。

    typedefenum {

    NotReachable = 0,//无连接

    ReachableViaWiFi,//使用3G/GPRS网络

    ReachableViaWWAN//使用WiFi网络

     

    } NetworkStatus;

     

    简单决断项目接二连三:

    // 是否wifi

    (BOOL) IsEnableWIFI {

    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);

    }

    // 是还是不是互联网连接

    (BOOL) IsEnableConnection {

    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);

    }

     

    // 是还是不是互联网连接

    (BOOL) IsEnable3G {

    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN);

    }

    作者下载的是vertion2.2 开垦Web等互联网使用程...

            @interface MyAppDelegate : NSObject <UIApplicationDelegate> {
                Reachability  *hostReach;
            }

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName

    IOS剖断网络意况,ios互联网意况

    自家下载的是vertion2.2

     

     

    付出Web等网络应用程序的时候,须求分明互联网意况,连接境况等音讯。若无拍卖它们,是不会经过Apple的稽审的。

        // 文告互联网状态
        - (void)reachabilityChanged:(NSNotification *)note {
            [self updateStatus];
            if (self.remoteHostStatus == NotReachable) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil)
                             message:NSLocalizedString (@"NotReachable", nil)
                            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];
                [alert release];
            }
        }

    }

        - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
            // 错误管理
        }

    switch ([r currentReachabilityStatus]) {

        @interface MyAppDelegate : NSObject <UIApplicationDelegate> {
            NetworkStatus remoteHostStatus;
        }

    if (parseError && error) {

        // My.AppDelegate.m
        #import "MyAppDelegate.h"

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)

        3.  接纳比方
        - (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
        {
            NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
            [parser setDelegate:self];
            [parser setShouldProcessNamespaces:NO];
            [parser setShouldReportNamespacePrefixes:NO];
            [parser setShouldResolveExternalEntities:NO];
            [parser parse];
            NSError *parseError = [parser parserError];
            if (parseError && error) {
                *error = parseError;
            }
            [parser release];
        }

    // ...

    三:使用NSXMLParser解析xml文件

    qualifiedName:(NSString *)qName

        
        
        1.2.添加framework:
        将SystemConfiguration.framework 加多进工程。如下图:
        
        
        2. 网络状态
        
        Reachability.h中定义了二种网络状态:
        typedef enum {
            NotReachable = 0,            //无连接
            ReachableViaWiFi,            //使用3G/GPRS网络
            ReachableViaWWAN            //使用WiFi网络
        } NetworkStatus;
        
        由此能够这么检查网络状态:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

        @implementation MyAppDelegate
        @synthesize remoteHostStatus;

    [[NSNotificationCenter defaultCenter] removeObserver:self];

        - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
            // disconnect
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;   
            NSString *returnString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
            NSLog(returnString);
            [self urlLoaded:[self urlString] data:self.receivedData];
            firstTimeDownloaded = YES;
        }

    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);

        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
                                            qualifiedName:(NSString*)qName attributes:(NSDictionary *)attributeDict{
            // 元素初始句柄
            if (qName) {
                elementName = qName;
            }
            if ([elementName isEqualToString:@"user"]) {
                // 输出属性值
                NSLog(@"Name is %@ , Age is %@", [attributeDict objectForKey:@"name"], [attributeDict objectForKey:@"age"]);
            }
        }

    3.  运用举个例子

        2. 常用的嘱托方法
        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
                                    namespaceURI:(NSString *)namespaceURI
                                    qualifiedName:(NSString *)qName 
                                    attributes:(NSDictionary *)attributeDict;
        - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
                                    namespaceURI:(NSString *)namespaceURI 
                                    qualifiedName:(NSString *)qName;
        - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
        - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;

    [self.navigationItem setLeftBarButtonItem:nil animated:NO];

        4. 链接状态的实时通报
        互连网连接状态的实时检讨,通知在互联网利用中也是拾叁分须要的。接续状态爆发变化时,要求立即地文告用户:
        
        Reachability 1.5版本
        // My.AppDelegate.h
        #import "Reachability.h"

    if ([elementName isEqualToString:@"user"]) {

        Reachability *r = [Reachability reachabilityWithHostName:@“www.apple.com”];
        switch ([r currentReachabilityStatus]) {
                case NotReachable:
                        // 未有网络连接
                        break;
                case ReachableViaWWAN:
                        // 使用3G网络
                        break;
                case ReachableViaWiFi:
                        // 使用WiFi网络
                        break;
        }
        
        3.反省当前互联网遇到
        程序运转时,倘诺想检验可用的网络情状,可以像这么
        // 是否wifi
         (BOOL) IsEnableWIFI {
            return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
        }

    - (void)reachabilityChanged:(NSNotification *)note {

        // 程序运转器,运行互连网监视
        - (void)applicationDidFinishLaunching:(UIApplication *)application {
        
            // 设置网络检查实验的站点
            [[Reachability sharedReachability] setHostName:@"www.apple.com"];
            [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
            // 设置网络状态变化时的文告函数
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
                                                     name:@"kNetworkReachabilityChangedNotification" object:nil];
            [self updateStatus];
        }

    [parser parse];

        // 是否3G
         (BOOL) IsEnable3G {
            return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
        }
        例子:
        - (void)viewWillAppear:(BOOL)animated {    
        if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) && 
                ([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {
                self.navigationItem.hidesBackButton = YES;
                [self.navigationItem setLeftBarButtonItem:nil animated:NO];
            }
        }

    delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

        3. 兑现委托方法
        - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
            // store data
            [self.receivedData setLength:0];            //平常在此地先清空中接力受多少的缓存
        }
        
        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
               /* appends the new data to the received data */
            [self.receivedData appendData:data];        //大概多次摄取数量,把新的数额增加在现有数据最后
        }

    1.2.添加framework:

        - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
        {
            // 获得成分的text
        }

    self.navigationItem.hidesBackButton = YES;

        // 更新网络状态
        - (void)updateStatus {
            self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
        }

    self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];

        1. 加多源文件和framework
        
        开荒Web等互联网应用程序的时候,须求认同网络情状,连接情形等音讯。若无管理它们,是不会透过Apple的审(我们的)查的。
        Apple 的 例程 Reachability 中介绍了收获/检查测量试验网络状态的艺术。要在应用程序程序中使用Reachability,首先要水到渠成如下两部:
        
        1.1. 增多源文件:
        在你的程序中动用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到您的工程中。如下图:

    将SystemConfiguration.framework 增添进工程。如下图:

        - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
                                            qualifiedName:(NSString *)qName
        {
            // 成分终了句柄
            if (qName) {
                   elementName = qName;
            }
        }

    @end

        - (void)dealloc {
            // 删除通告对象
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [window release];
            [super dealloc];
        } 
        
        Reachability 2.0版本     

    互联网连接状态的实时检查,文告在网络选取中也是十二分须要的。接续状态发生变化时,须要立刻地公告用户:

        static NSString *feedURLString = @"";

    firstTimeDownloaded = YES;

        @end

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

        @property NetworkStatus remoteHostStatus;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""

        @end

    // MyAppDelegate.m

        1. 安装委托对象,初始深入分析
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];   //或许也能够应用initWithContentsOfU宝马7系L直接下载文件,可是有两个缘故不那样做:
        // It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
        // because it gives less control over the network, particularly in responding to connection errors.
        [parser setDelegate:self];
        [parser parse];

    进而得以如此检查互连网状态:

    二:使用NSConnection下载数据
        
        1.创设NSConnection对象,设置委托对象
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];
        [NSURLConnection connectionWithRequest:request delegate:self];
        
        2. NSUPAJEROLConnection delegate委托方法
            - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;  
            - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;  
            - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;  
            - (void)connectionDidFinishLoading:(NSURLConnection *)connection;  

    }

        // MyAppDelegate.h
        @class Reachability;

    qualifiedName:(NSString *)qName

    小说来源:

    {

        NSError *parseError = nil;
        [self parseXMLFileAtURL:[NSURL URLWithString:feedURLString] parseError:&parseError];

    [alert release];

        // MyAppDelegate.m
        - (void)reachabilityChanged:(NSNotification *)note {
            Reachability* curReach = [note object];
            NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
            NetworkStatus status = [curReach currentReachabilityStatus];
        
            if (status == NotReachable) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
                                  message:@"NotReachable"
                                  delegate:nil
                                  cancelButtonTitle:@"YES" otherButtonTitles:nil];
                                  [alert show];
                                  [alert release];
            }
        }
                                  
        - (void)applicationDidFinishLaunching:(UIApplication *)application {
            // ...
                      
            // 监测互联网状态
            [[NSNotificationCenter defaultCenter] addObserver:self
                                  selector:@selector(reachabilityChanged:)
                                  name: kReachabilityChangedNotification
                                  object: nil];
            hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
            hostReach startNotifer];
            // ...
        }

    namespaceURI:(NSString *)namespaceURI

    一:确认互联网景况3G/WIFI

    [parser parse];

    Apple 的 例程 Reachability 中介绍了获取/检验网络状态的法子。要在应用程序程序中使用Reachability,首先要水到渠成如下两部:

    // 更新互联网状态

    3.反省当前网络意况

    1. 兑现委托方法

    }

    }

    // MyAppDelegate.h

    [parser release];

    1. 安装委托对象,开头分析

    // 是否3G

    @interface MyAppDelegate : NSObject {

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;

    程序启动时,要是想检查测验可用的网络境遇,能够像那样

    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);

    {

    if (status == NotReachable) {

    // 未有网络连接

    case NotReachable:

    #import "MyAppDelegate.h"

    // 元素开始句柄

    @interface MyAppDelegate : NSObject {

    Reachability  *hostReach;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil)

    [self updateStatus];

    @class Reachability;

    hostReach startNotifer];

    NetworkStatus status = [curReach currentReachabilityStatus];

    Reachability* curReach = [note object];

    } NetworkStatus;

    name: kReachabilityChangedNotification

    // 使用3G网络

    一:确认互联网遭遇3G/WIFI

    1. 链接状态的实时通报
    1. 增加源文件和framework

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];   //或然也足以使用initWithContentsOfU大切诺基L直接下载文件,不过有贰个缘故不那样做:

    qualifiedName:(NSString*)qName attributes:(NSDictionary *)attributeDict{

    }

    本文由68399皇家赌场发布于域名注册,转载请注明出处:IOS剖断互连网情况,ios互联网蒙受

    关键词: OC技术短文 网络 开发 ios iPhone

上一篇:AppCan 2.0 正式发布,推活动应用云服务

下一篇:没有了