iphone网络编程总结

一:确认网络环境3G/WIFI

1. 添加源文件和framework
    
    开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审(我们的)查的。
    Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。要在应用程序程序中使用Reachability,首先要完成如下两部:
    
    1.1. 添加源文件:
    在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。如下图:

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

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

// 是否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];
        }
    }

4. 链接状态的实时通知
    网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:
    
    Reachability 1.5版本
    // My.AppDelegate.h
    #import "Reachability.h"

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

@property NetworkStatus remoteHostStatus;

@end

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

@implementation MyAppDelegate
    @synthesize remoteHostStatus;

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

// 通知网络状态
    - (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)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];
    }

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

// MyAppDelegate.h
    @class Reachability;

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

@end

// 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];
        // ...
    }

二:使用NSConnection下载数据
    
    1.创建NSConnection对象,设置委托对象
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];
    [NSURLConnection connectionWithRequest:request delegate:self];
    
    2. NSURLConnection 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;

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];        //可能多次收到数据,把新的数据添加在现有数据最后
    }

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

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

三:使用NSXMLParser解析xml文件

1. 设置委托对象,开始解析
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];   //或者也可以使用initWithContentsOfURL直接下载文件,但是有一个原因不这么做:
    // 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];

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;

static NSString *feedURLString = @"http://www.yifeiyang.net/test/test.xml";

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

- (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"]);
        }
    }

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

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        // 取得元素的text
    }

NSError *parseError = nil;

一:确认网络环境3G/WIFI

1. 添加源文件和framework

开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审(我们的)查的。

Apple 的例程 Reachability 中介绍了取得/检测网络状态的方法。要在应用程序程序中使用Reachability,首先要完成如下两部:

1.1. 添加源文件:

在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。如下图:

1.2.添加framework:

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

2. 网络状态

Reachability.h中定义了三种网络状态:

typedef enum {

NotReachable = 0,            //无连接

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

ReachableViaWWAN            //使用WiFi网络

} NetworkStatus;

因此可以这样检查网络状态:

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);

}

// 是否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];

}

}

4. 链接状态的实时通知

网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:

Reachability 1.5版本

// My.AppDelegate.h

#import "Reachability.h"

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

NetworkStatus remoteHostStatus;

}

@property NetworkStatus remoteHostStatus;

@end

// My.AppDelegate.m

#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize remoteHostStatus;

// 更新网络状态

- (void)updateStatus {

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

}

// 通知网络状态

- (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)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];

}

- (void)dealloc {

// 删除通知对象

[[NSNotificationCenter defaultCenter] removeObserver:self];

[window release];

[super dealloc];

}

Reachability 2.0版本

// MyAppDelegate.h

@class Reachability;

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {

Reachability  *hostReach;

}

@end

// 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];

// ...

}

二:使用NSConnection下载数据

1.创建NSConnection对象,设置委托对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];

[NSURLConnection connectionWithRequest:request delegate:self];

2. NSURLConnection 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;

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];        //可能多次收到数据,把新的数据添加在现有数据最后

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

// 错误处理

}

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

}

三:使用NSXMLParser解析xml文件

1. 设置委托对象,开始解析

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];   //或者也可以使用initWithContentsOfURL直接下载文件,但是有一个原因不这么做:

// 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];

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;

static NSString *feedURLString = @"http://www.yifeiyang.net/test/test.xml";

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];

}

- (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"]);

}

}

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

qualifiedName:(NSString *)qName

{

// 元素终了句柄

if (qName) {

elementName = qName;

}

}

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

{

// 取得元素的text

}

NSError *parseError = nil;

iOS-iphone网络编程总结的更多相关文章

  1. 聊聊iOS中网络编程长连接的那些事

    1.长连接在iOS开发中的应用 常见的短连接应用场景:一般的App的网络请求都是基于Http1.0进行的,使用的是NSURLConnection.NSURLSession或者是AFNetworking ...

  2. IOS开发——网络编程总汇

    关于IOS的网络编程,大家都会想到C实现的底层BSD ,CFNetwork和NSURL之类的库,虽然如今非常多第三方库非常方便,可是作为一名开发人员,也须要了解底层代码. 以下的思维导图是关于眼下开发 ...

  3. 【iOS】网络编程:上传图片到服务器

    在网络编程中,如果需要上传图片,那么他的方法将会和普通的上传数据不同,下面将讲解如何上传图片. 环境信息: Mac OS X 10.9.5 Xcode 5.1.1 IOS 7.1 正文: - (NSU ...

  4. ios开发 网络编程浅析(一)

    iphone包含了很多框架和库,从底层的套接字到不同层次的封装,可以方便地给程序添加网络功能. (1)BSD套接字.最底层的套接字,这是Unix网络开发常用的API.如果从其他系统移植程序,而程序用的 ...

  5. iOS:网络编程中三个数据解析协议HTTP、XML、JSON的详细介绍

    网络编程:HTTP协议.XML数据协议.JSON数据协议: HTTP: 1.HTTP传输数据有四种方式:Get方式.Post方式.同步请求方式.异步请求方式. 说明:同步和异步请求方式在创建链接对象和 ...

  6. iOS开发——网络编程Swift篇&Alamofire详解

    Alamofire详解 预览图 Swift Alamofire 简介 Alamofire是 Swift 语言的 HTTP 网络开发工具包,相当于Swift实现AFNetworking版本. 当然,AF ...

  7. iOS开发——网络编程Swift篇&(八)SwiftyJSON详解

    SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...

  8. iOS开发——网络编程OC篇&(一)XMPP简单介绍与准备

    XMPP简单介绍与准备 一.即时通讯简单介绍 1.简单说明 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双方都看到交谈 ...

  9. iOS开发——网络编程OC篇&Socket编程

    Socket编程 一.网络各个协议:TCP/IP.SOCKET.HTTP等 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被称作 ...

  10. iOS开发——网络编程OC篇&使用WebView构建HyBird应用

    使用WebView构建HyBird应用 HyBird是一种本地技术与Web相结合,能过实现跨平台的移动应用开发,最常用的一个框架:PhoneGap 一:首先,写好html代码 <!DOCTYPE ...

随机推荐

  1. mysql中删除重复数据

    //首先我们需要知道我们重复的都有哪些数据, //第一步:进行对数据表进行分组,group by. //第二步:进行后通过having进行限制筛选,条数大于等于2的 //第三步:进行多表删除. //案 ...

  2. vue jqury如何获取元素中的属性

    1.点击事件获取 点击事件通过传值得方式 <el-button type="danger" round @click="delHander($event)" ...

  3. js 定义数组转json

    var msgData = {} msgData["url"] = openUrl msgData["courseName"] = val.name JSON. ...

  4. MyBatis的关联查询

    关联映射的一对多 //查询经理角色 以及 该角色下对应的员工集合 public SmbmsRole getRoleAndUser(Integer id); <resultMap id=" ...

  5. 使用 ServerSocket 建立聊天服务器-2

    1. 从serverListener中可以看出,每一个客户端创建新的请求之后,都会把它分配给一个独立的chatsocket ,但是每一个ChatSocket都是相互独立的,他们之间并不能沟通,所以要新 ...

  6. exam8.29

    咕了好几篇后... 我终于开始重新写了 T1: 不会,没思路,暴搜还可能会(一开始我以为暴搜时间复杂度为$\Theta (mn ^ k)$) 于是码出了暴搜... 跑一遍$(4,4,5)$,然后... ...

  7. vue 组件的通信方式(完整版)

    几种通信方式无外乎以下几种: Prop(常用) $emit (组件封装用的较多) .sync语法糖 (较少) $attrs & $listeners (组件封装用的较多) provide &a ...

  8. package.json 与 package-lock.json 的区别

    根据官方文档,这个package-lock.json 是在 `npm install`时候生成一份文件,用以记录当前状态下实际安装的各个npm package的具体来源和版本号. 它有什么用呢?因为n ...

  9. jsp页面中的EL表达式不被解析org.apache.jasper.JasperException: Unable to convert string [${item.createtime}]

    https://m.imooc.com/qadetail/277572 web.xml的版本是不是2.3, 如果是2.3,在jsp页面开头添加<%@ page isELIgnored=" ...

  10. 在docker容器中编译hadoop 3.1.0

    在docker容器中编译hadoop 3.1.0 优点:docker安装好之后可以一键部署编译环境,不用担心各种库不兼容等问题,编译失败率低. Hadoop 3.1.0 的源代码目录下有一个 `sta ...