这里主要把我遇到的小问题再写三个出来。
1)iOS获取当前日期的方法
有时候我们需要获取系统当前的日期来做一些操作,用下面这个方法:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
可是这个时间可能是不准确的,这个系统时间是可以被用户在手机上更改的,所以以前捕鱼达人就遇见过这个问题,使用这个系统时间来计时,被用户改了以后,时间就一直无限了。
为了避免这个问题,我们推荐使用网络时间,这个时间是无法被用户改变的
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate networkDate]];
[dateFormatter release];
2)iOS判断当前网络连接状况
//检测网络连接状况
+ (BOOL)isExistenceNetwork
{
BOOL isExistenceNetwork = NO;
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
switch ([reachability currentReachabilityStatus])
{
case NotReachable:
isExistenceNetwork = NO;
//NSLog(@"没有网络");
break;
case ReachableViaWWAN:
isExistenceNetwork = YES;
//NSLog(@"当前网络为3G");
break;
case ReachableViaWiFi:
isExistenceNetwork = YES;
//NSLog(@"当前网络为Wifi");
break;
}
return isExistenceNetwork;
}
// 检测可用的网络环境是否为wifi
+ (BOOL)IsEnableWIFI
{
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 检测可用的网络环境是否为3G
+ (BOOL)IsEnable3G
{
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
3)将一个BOOL值插入到NSMutableDictionary中一般情况下,基本数据类型(int,BOOL等)是不允许被当做value插入到字典中的
可以用这个方法插入:
首先将BOOL包装到NSNumber中:
NSNumber *boolNumber = [NSNumber numberWithBool:YES];
取得bool值:
BOOL b = [boolNumber boolValue];
或通过这种方式:
NSValue *boolValue = [NSValue value:pointerToBool withObjcType:@encode(BOOL *)];
BOOL * b = [boolValue pointerValue];
4) iOS使用webView加载本地的网页
如果你想使用webView加载本地的网页,首先的一步你需要把这个网页下载下来,包含它的html,样式(css),以及一些可能存在的图片资源,然后把这些文件拷贝到你的项目里,(一定要引入项目,像你使用图片资源的引入)
然后:
UIWebView * webView;
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourhtmlName" ofType:@"html"];// 你的本地html名
NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:html baseURL:baseURL];