[iOS]一些第三方框架的使用

SDWebImage

功能:加载网络图片,并自动缓存到内存和硬盘

导入头文件:UIImageView+WebCache.h

使用:
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder

GDataXML

功能:解析XML文件,适合解析大文件

配置:

  1. 导入libxml2库
  2. 设置libxml2的头文件搜索路径(为了能找到libxml2库的所有头文件)
    在Head Search Path中加入/usr/include/libxml2
    Build Settings –> Search Paths –> Head Search Path
  3. 设置链接参数(自动链接libxml2库)
    在Other Linker Flags中加入-lxml2
    Build Settings –> Linking –> Other Linker Flags
  4. 由于GDataXML是非ARC的,因此得设置编译参数-fno-objc-arc
    Build Phases –> Compile Sources

导入头文件:GDataXMLNode.h

使用:

- (NSArray *)DOMparseXMLWithData:(NSData *)data
{
    // 1.加载文档
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];

    // 2.获得根元素
    GDataXMLElement *root = doc.rootElement;

    // 3.获得所有video元素
    NSArray *elements = [root elementsForName:@"video"];

    // 4.将GDataXMLElement对象转成HMVideo模型
    NSMutableArray *videos = [NSMutableArray array];
    for (GDataXMLElement *ele in elements) {
        HMVideo *video = [[HMVideo alloc] init];
        video.ID = [ele attributeForName:@"id"].stringValue.intValue;
        video.length = [ele attributeForName:@"length"].stringValue.intValue;
        video.name = [ele attributeForName:@"name"].stringValue;
        video.image = [ele attributeForName:@"image"].stringValue;
        video.url = [ele attributeForName:@"url"].stringValue;
        [videos addObject:video];
    }
    return videos;
}

SSZipArchive

功能:可以压缩以及解压缩文件

配置:
导入libz.dylib动态库即可

导入头文件:SSZipArchive.h

使用:

  • + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination;
  • + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination
    overwrite:(BOOL)overwrite password:(NSString *)password error:(NSError **)error;
  • + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(id)delegate;
  • + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite
    password:(NSString *)password error:(NSError **)error delegate:(id)delegate;

  • + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)filenames;
  • + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;

Reachability

功能:用来检测网络状态(蜂窝数据流量/WiFi/没有网络)

配置:
1. 导入SystemConfiguration.framework
2. 由于Reachability.m是非ARC,所以要在Compile Sources时加上-fno-objc-arc

导入头文件:Reachability.h

使用:
网络状态发生改变时,系统会传出kReachabilityChangedNotification通知。响应此通知,并在函数里面查看网络状态。
e.g.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange)
    name:kReachabilityChangedNotification object:nil];
    self.conn = [Reachability reachabilityForInternetConnection];
    [self.conn startNotifier];
}

- (void)dealloc
{
    [self.conn stopNotifier];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)networkStateChange
{
    [self checkNetworkState];
}

- (void)checkNetworkState
{
    // 1.检测wifi状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];

    // 2.检测手机是否能上网络(WIFI\3G\2.5G)
    Reachability *conn = [Reachability reachabilityForInternetConnection];

    // 3.判断网络状态
    if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi
        NSLog(@"有wifi");
    } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网
        NSLog(@"使用手机自带网络进行上网");
    } else { // 没有网络
        NSLog(@"没有网络");
    }
// 用WIFI
// [wifi currentReachabilityStatus] != NotReachable
// [conn currentReachabilityStatus] != NotReachable

// 没有用WIFI, 只用了手机网络
// [wifi currentReachabilityStatus] == NotReachable
// [conn currentReachabilityStatus] != NotReachable

// 没有网络
// [wifi currentReachabilityStatus] == NotReachable
// [conn currentReachabilityStatus] == NotReachable
}

ASI

功能:HTTP终结者,可以完成普通的GET/POST请求,文件下载、文件上传并且显示进度,功能强大

配置:
1. 导入libz.1.2.5.dylib库
2. 导入MobileCoreServices.framework框架
3. 导入SystemConfiguration.framework框架
4. 由于ASI是非ARC的,因此以ASI开头的8个文件以及Reachability.h得设置编译参数-fno-objc-arc
Build Phases –> Compile Sources

导入头文件
ASIHTTPRequest.h
ASIDownloadCache.h
ASIFormDataRequest.h

使用:
使用见http://blog.csdn.net/u013412764/article/details/48829423#t33

AFN

功能:可以完成常见的网络功能,没有ASI强大。但由于AFN是ARC的,所以配置起来比ASI简单。ASI基于CFNetwork框架开发,而AFN基于NSURL,所以AFN的效率要低一点。
[iOS]一些第三方框架的使用_第1张图片

使用
使用见http://blog.csdn.net/u013412764/article/details/48829423#t47

你可能感兴趣的:(ios)