iOS随笔(二)

这里主要把我遇到的小问题再写三个出来。

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值