本文中将简单介绍几个笔者认为在开发中很有用的iOS开源类库及其简单使用方法
1: SBJson
SBJson, 又名Json Framework, 是一个非常流行的,开源的JSON解析类库。SBJSon的使用非常简单,为在网络中传输与解析格式化的数据提供了极大的便利。
SBJson的使用也很简单,在项目中将类库文件添加到项目中,然后加入几个依赖的FrameWork,目前比价稳定的有ARC3.1版本的和非ARC版本的3.0,大家可以根据自身需要进行下载。
下面就SBJson的使用进行简单介绍:
1. #import "SBJSon.h"
2.
3. //写json
4. SBJsonWriter* jsonWriter = [[[SBJsonWriter alloc] init] autorelease];
5. NSMutableArray* tempArray = [NSMutableArray array];
6. NSDictionary* tempDicA = [NSMutableDictionary dictionary];
7. "valueA" forKey @"keyA"];
8. [tempArray addObject: tempDicA];
9. NSDictionary* tempDicB = [NSMutableDictionary dictionary];
10. "valueB" forKey @"keyB"];
11. [tempArray addObject: tempDicB];
12. NSMutableDictionary* jsonDic = [NSMutableDictionary dictionary];
13. "array"];
14. NSString* jsonString = [jsonWriter stringWithObject: jsonDic];
15.
16. //解析json
17. NSDictionary* resultDic = [jsonString JSONValue];
18. "array"];
19. NSDictionary* dicA = [resultArray objectAtIndex: 0];
20. NSDictionary* dicB = [resultArray objectAtIndex: 1];
21. "%@, %@" [dicA objectForKey @"keyA", [dicB objectForKey: @"keyB"]);
2: ASIHTTPRequest
ASIHTTPRequest是一个非常流行的iOS平台网络通信类库,使用ASIHTTPRequest之后,大大简化了iOS平台的网络编程。其以方便的接口对同步、异步的网络传输进行了传输,将ASIHTTPRequest添加到自己的项目也非常方便,将类库中所有文件拷贝到一个文件夹中,然后将此文件夹添加到项目中,同时要添加如下图CFNetWork之下所示的类库,就可以使用ASIHTTPRequest了:
使用ASIHTTPRequest步骤非常简答,在一般应用开发中,网络连接基本上使用的都是异步方式,下面简单演示一下最简单的异步通讯方法
1. #import "ASIHTTPRequest.h"
2.
3. - (void) requestDataFromServer
4. {
5. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
6. "www.fakeurl.com"];
7. ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL: url];
8. [request setTag: 1024];
9. [request setTimeOutSeconds: 3];
10. [request setAllowCompressedResponse:YES];
11. [request setDelegate:self];
12. [request startAsynchronous];
13. [pool drain];
14. }
15.
16. - (void)requestFinished:(ASIHTTPRequest *)request
17. {
18. NSString* rawString = [request responseString];
19. if (request.tag == 1024) {
20. //处理网络返回结果
21. }
22. }
23.
24. - (void)requestFailed:(ASIHTTPRequest *)request
25. {
26. if (request.tag == 1024) {
27. //处理网络错误
28. }
29. }
注意上面的两个函数中,后面连个为ASIHTTPRequest的delegate函数,其声明类型不能改变,只要在生成ASIHTTPRequest时的deleage设成了self,那么最后返回结果,不管是成功调用还是网络失败,都会调用这两个函数中的对应的一个。
3: FMDataBase
FMDataBase是iOS平台中一个非常强大的数据库类库,其将sqlite面向过程的接口以面向对象的方法展现出来,提供了极高的可用性。
其使用很简单,将sqlite 库添加到项目中,然后将FMDataBase类库文件添加到项目中,下面是对笔者对FMDataBase进行的一个二次封装,处理的数据库很简单,只有一张表,两个列,存储的都是一些key-value对,读者可以根据自身需要对此类进行修改。
1. #import <Foundation/Foundation.h>
2. #import "FMDatabase.h"
1. @interface DBController : NSObject {
2. }
3.
4. @property (nonatomic, assign) FMDatabase *dataBase;
5.
6. +(BOOL)databaseExit;
7.
8. -(BOOL)initDatabase;
9.
10. -(void)closeDatabase;
11.
12. -(BOOL)deleteTable;
13.
14. -(BOOL)InsertTable:(NSString *)key_type value:(NSString *)key_value;
15.
16. -(BOOL)UpdataTable:(NSString *) valueStr key:(NSString *)keyStr;
17.
18. -(NSMutableDictionary *)querryTable;
19.
20. +(BOOL) deleteDataBase;
21.
22. @end
23.
24. @synthesize dataBase = _dataBase;
25.
26. - (id)init{
27.
28. if(self = [super init]){
29. _dataBase = [FMDatabase databaseWithPath: [DBController getPath]];
30. if (![_dataBase open]) {
31. "Create/Open dataBase %@ Failed!", [DBController getPath]);
32. }
33. }
34. return self;
35. }
36.
37. //数据库是否存在
38. +(BOOL)databaseExit
39. {
40. return [[NSFileManager defaultManager] fileExistsAtPath: [self getPath]];
41. }
42.
43.
44. //初始化数据库
45. -(BOOL)initDatabase{
46. if ([DBController databaseExit]) {
47. return [self createTable];
48. }
49. return NO;
50. }
51.
52.
53.
54. //创建数据库
55. -(BOOL)createTable
56. {
57. return [self.dataBase executeUpdate: @"create table if not exists personTable(id integer primary key autoincrement, key text,value text);"];
58. }
59.
60. //删除数据表
61. -(BOOL)deleteTable{
62. if ([DBController databaseExit]) {
63. return [self.dataBase executeUpdate: [NSString stringWithFormat:@"drop table %@;",_PERSONINFO]];
64. }
65. return NO;
66. }
67.
68.
69.
70. //关闭数据库
71. - (void) closeDatabase
72. {
73. [self.dataBase close];
74. }
75.
76. //插入数据
77. -(BOOL)InsertTable:(NSString *)key value:(NSString *)value
78. {
79. if ([DBController databaseExit]) {
80. BOOL result = NO;
81. [self.dataBase beginTransaction];
82. "INSERT INTO personTable (key,value) VALUES (?,?)", key, value];
83. [self.dataBase commit];
84. return result;
85. }
86. return NO;
87. }
88.
89. //更新数据
90. -(BOOL)UpdataTable:(NSString *) valueStr key:(NSString *)keyStr
91. {
92. if ([DBController databaseExit]) {
93. BOOL result = NO;
94. [self.dataBase beginTransaction];
95. "UPDATE personTable SET value=? WHERE key=?", valueStr, keyStr];
96. [self.dataBase commit];
97. return result;
98. }
99. return NO;
100. }
101.
102. //查询整个表
103. -(NSMutableDictionary *)querryTable
104. {
105. NSMutableDictionary* resultDic = [[NSMutableDictionary alloc] init];
106.
107. "select * from personTable"];
108. while ([rs next]) {
109. "value"] forKey: [rs stringForColumn: @"key"]];
110. }
111. return [resultDic autorelease];
112. }
113.
114. +(BOOL) deleteDataBase
115. {
116. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
117.
118. NSString *documentsDirectory = [paths objectAtIndex:0];
119.
120. //设置数据库得路径
121.
122. NSFileManager *fileManager = [NSFileManager defaultManager];
123.
124. BOOL find = [fileManager fileExistsAtPath:path];
125.
126. if (find) {
127.
128. [fileManager removeItemAtPath: path error: nil];
129.
130. }
131. return find;
132. }
133.
134.
135. - (void)dealloc {
136. [_dataBase close];
137. _dataBase = nil;
138. [super dealloc];
139. }
140.
141. + (NSString*) getPath {
142. // 打开的数据库
143. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
144.
145. NSString *documentsDirectory = [paths objectAtIndex:0];
146.
147. return [documentsDirectory stringByAppendingPathComponent:_DBNAME];//设置数据库得路径
148. }
149.
150.
151. @end
4: MPProgressHUD
MPProgressHUD是一个非常好用的进度指示器类库,其提供了苹果官方sdk没有提供的progress indicator接口,且提供多种样式,使用方法简便。
首先将类库文件添加到项目中。
使用实例代码如下:
1. #import <UIKit/UIKit.h>
2. #import "MBProgressHUD.h"
1. #import <libkern/OSAtomic.h>
2. @interface SampleViewController : UITableViewController <MBProgressHUDDelegate>
3. @property (nonatomic, retain) NSCondition* condition;
4. @property (nonatomic, retain) MBProgressHUD* hud;
5. @end
6.
7. static volatile NSInteger WAITING_RESPONSE_FOR_SERVERRESPONSE = 0;
8.
9. - (void) popOutMBProgressHUD;
10. - (void) selectorForMPProgressHUD;
11. - (void) notifyMPProgressHUDToDisappear;
12.
13. @implementation SampleViewController
14. @synthesize hud = _hud;
15. @synthesize condition = _condition;
16.
17. - (id) initWithCoder:(NSCoder *)aDecoder
18. {
19. self = [super initWithCoder: aDecoder];
20. if (self != nil) {
21. _hud = nil;
22. _condition = [[NSCondition alloc] init];
23. }
24. return self;
25. }
26.
27. - (void) dealloc
28. {
29. [_hud release];
30. [_condition release];
31. }
32.
33. - (void) popOutMBProgressHUD
34. {
35. MBProgressHUD* tempHud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
36. self.hud = tempHud;
37. [self.navigationController.view addSubview: tempHud];
38.
39. self.hud.dimBackground = YES;
40. self.hud.delegate = self;
41. "正在处理";
42. [self.hud showWhileExecuting:@selector(selectorForMPProgressHUD) onTarget:self withObject: nil animated:YES];
43. [tempHud release];
44. }
45.
46. - (void) selectorForMPProgressHUD
47. {
48. OSAtomicCompareAndSwapInt(0,
49. 1,
50. &WAITING_RESPONSE_FOR_SERVERRESPONSE);
51. [self performSelectorInBackground: @selector(tempSelector) withObject: nil];
52. [self.condition lock];
53. while (OSAtomicCompareAndSwapInt(1,
54. 1,
55. &WAITING_RESPONSE_FOR_SERVERRESPONSE)) {
56. NSDate* timeOutDate = [NSDate dateWithTimeIntervalSinceNow: 5.0f];
57. [self.condition waitUntilDate: timeOutDate];
58. }
59. [self.condition unlock];
60. }
61.
62. - (void) notifyMPProgressHUDToDisappear
63. {
64. //通知进度显示hud消失
65. [self.condition lock];
66. OSAtomicCompareAndSwapInt(1,
67. 0,
68. &WAITING_RESPONSE_FOR_SERVERRESPONSE);
69. [self.condition signal];
70. [self.condition unlock];
71. }
[cpp]
view plain
copy
1. - (void)hudWasHidden:(MBProgressHUD *)hud
2. {
3. // Remove HUD from screen when the HUD was hidded
4. [self.hud removeFromSuperview];
5. self.hud = nil;
6. }
7.
8. - (void) tempSelector
9. {
10. //模拟真实的耗时操作
11. [self notifyMPProgressHUDToDisappear];
12. }
13.
14. @end