1.使用[UIApplication sharedApplication] openURL方式打开一个网页或其他程序时,ios未响应可能原因是要打开的url地址中包含空格或其他特殊字符,需要先对url字符串进行编码,然后再使用即可。
NSString * urlString = [NSString stringWithFormat:@"%@%@",prefixURL,rearString];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
2.获取ios及设备信息
NSLog([[UIDevice currentDevice] name]); // Name of the phone as named by user
NSLog([[UIDevice currentDevice] uniqueIdentifier]); // A GUID like string
NSLog([[UIDevice currentDevice] systemName]); // "iPhone OS"
NSLog([[UIDevice currentDevice] systemVersion]); // "2.2.1"
NSLog([[UIDevice currentDevice] model]); // "iPhone" on both devices
NSLog([[UIDevice currentDevice] localizedModel]); // "iPhone" on both devices
float version = [[[UIDevice currentDevice] systemVersion] floatValue]; //获取版本号
//获取MAC地址
NSLog([[UIDevice currentDevice] macaddress]);
//获取当前使用的语言
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
3.ios定时器
NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(定时调用的方法名称)
userInfo:nil
repeats:YES];
4.ios解析json
使用开源json包,项目地址:
http://stig.github.com/json-framework/
NSData * responseData = [respones responseData];
NSString * strResponser = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
SBJsonParser * parser = [[SBJsonParser alloc]init];
NSMutableDictionary *dicMessageInfo = [parser objectWithString:strResponser]; // 解析成json解析对象
[parser release];
//发送者
NSString * sender = [dicMessageInfo objectForKey:@"sender"];
5.使用自定义的方法处理服务器响应
使用自定义的方法处理服务器响应时需要指定代理为当前对象,即:[request setDelegate:self];并通过[request setDidFinishSelector:@selector(didRequestDownloadDataDone:)];指定didRequestDownloadDataDone为响应函数,didRequestDownloadDataDone方法将以服务器的响应对象为参数,即- (void)didRequestDownloadDataDone:(ASIHTTPRequest *)respones,通过获取respones的状态进行判断请求是否成功([respones responseStatusCode] < 400)并进一步处理响应结果;
6.json嵌套对象解析:
//要上传的字符串
NSString *dataStr=[[NSString alloc] initWithString:@"{\"cross\":{\"1\":\"true\",\"2\":\"false\",\"3\":\"true\"}}"];
//获取响应返回字符串
NSData * responseData = [respones responseData];
NSString * strResponser = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//嵌套解析
SBJsonParser * parser = [[SBJsonParser alloc]init];
NSMutableDictionary *dicMessageInfo = [parser objectWithString:strResponser]; // 解析成json解析对象
NSMutableDictionary * cross = [dicMessageInfo objectForKey:@"cross"];
NSString *cross1= [cross objectForKey:@"1"];
//解析json到各个字符串
//发送者
[parser release];
NSLog(@"cross1: %@",cross1);
7.拼接json字符串
通过使用SBJson中的SBJsonWriter类的方法- (NSString*)stringWithObject:(id)value可以将一个对象中的值格式化为json字符串,符合key/value格式的数据封装到NSDictionary后可以使用该方法进行格式化,其他数据通过拼接字符串的方式格式化。
在拼接过程中可以使用类NSMutableString的方法:
- (void)appendString:(NSString *)aString;、
- (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
动态添加字符串。
拼接的字符串可通过json在线验证的方式验证其格式是否正确,网址为:
http://jsonlint.com/
-(NSString *) getJsonString
{
NSMutableString *json = [NSMutableString stringWithCapacity:128];
NSString *jsonString=nil;
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
[json appendString:@"{\"data\":{"];
[json appendFormat:@"\"%@\":\"%d\",",@"reset",reset];
if(missionStatus!=NULL)
{
jsonString=[writer stringWithObject:status];
if(jsonString!=NULL)
{
[json appendString:@"\"status\":"];
[json appendString:jsonString];
}
}
[json appendString:@"}}"];
return json;
}