int main(int argc, const char * argv[]) {
@autoreleasepool {
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
#import <Foundation/Foundation.h> @interface BNRLogger : NSObject<NSURLSessionDataDelegate> @property(nonatomic) NSDate *lastTime; -(NSString *) lastTimeString; -(void)updateLastTime: (NSTimer *) t; @end
#import "BNRLogger.h"
@implementation BNRLogger
-(NSString *)lastTimeString
{
static NSDateFormatter *dateFormatter=nil;
if(!dateFormatter)
{
dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"created dateFormatter");
}
return [dateFormatter stringFromDate:self.lastTime];
}
-(void)updateLastTime:(NSTimer *)t
{
NSDate *now=[NSDate date];
[self setLastTime:now];
NSLog(@"Just set time to %@",self.lastTimeString);
}
@end
#import <Foundation/Foundation.h>
#import "BNRLogger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
BNRLogger *logger=[[BNRLogger alloc]init];
__unused NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:logger selector:@selector(updateLastTime:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
#import <Foundation/Foundation.h>
#import "BNRLogger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
BNRLogger *logger=[[BNRLogger alloc]init];
//URL是一张图片的下载链接
NSURL *url = [NSURL URLWithString:@"http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimg.xiazaizhijia.com%2Fuploads%2F2016%2F0914%2F20160914112151862.jpg&thumburl=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D2349180720%2C2436282788%26fm%3D11%26gp%3D0.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
__unused NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:logger delegateQueue:[NSOperationQueue mainQueue]];
__unused NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:logger selector:@selector(updateLastTime:) userInfo:nil repeats:YES];
//4.根据会话对象创建一个Task(发送请求)
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
//5.执行任务
[dataTask resume];
[[NSRunLoop currentRunLoop]run];
}
return 0;
}
#import <Foundation/Foundation.h> @interface BNRLogger : NSObject<NSURLSessionDataDelegate> @property (nonatomic, strong) NSMutableData *responseData; @property(nonatomic) NSDate *lastTime; -(NSString *) lastTimeString; -(void)updateLastTime: (NSTimer *) t; @end
#import "BNRLogger.h"
@implementation BNRLogger
-(NSMutableData *)responseData
{
if (_responseData == nil) {
_responseData = [NSMutableData data];
}
return _responseData;
}
-(NSString *)lastTimeString
{
static NSDateFormatter *dateFormatter=nil;
if(!dateFormatter)
{
dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"created dateFormatter");
}
return [dateFormatter stringFromDate:self.lastTime];
}
-(void)updateLastTime:(NSTimer *)t
{
NSDate *now=[NSDate date];
[self setLastTime:now];
NSLog(@"Just set time to %@",self.lastTimeString);
}
//1.接收到服务器响应的时候调用该方法
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
//在该方法中可以得到响应头信息,即response
NSLog(@"didReceiveResponse--%@",[NSThread currentThread]);
NSLog(@"响应");
//注意:需要使用completionHandler回调告诉系统应该如何处理服务器返回的数据
//默认是取消的
/*
NSURLSessionResponseCancel = 0, 默认的处理方式,取消
NSURLSessionResponseAllow = 1, 接收服务器返回的数据
NSURLSessionResponseBecomeDownload = 2,变成一个下载请求
NSURLSessionResponseBecomeStream 变成一个流
*/
completionHandler(NSURLSessionResponseAllow);
}
//2.接收到服务器返回数据的时候会调用该方法,如果数据较大那么该方法可能会调用多次
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData--%@",[NSThread currentThread]);
NSLog(@"返回");
//拼接服务器返回的数据
[self.responseData appendData:data];
}
//3.当请求完成(成功|失败)的时候会调用该方法,如果请求失败,则error有值
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"didCompleteWithError--%@",[NSThread currentThread]);
NSLog(@"完成");
if(error == nil)
{
//解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil];
NSLog(@"%@",dict);
}
}
@end
//在”辅助对象”方法应用程序中的main.m中加入这行代码 [[NSNotificationCenter defaultCenter]addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];
//在”辅助对象”方法应用程序中的BNRLogger.m中加入这行代码
-(void)zoneChange:(NSNotification *)note
{
NSLog(@"The system time zone has changed!");
}
[[NSNotificationCenter defaultCenter]addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserverForName:NSSystemTimeZoneDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note){
NSLog(@"The system time zone has changed!");
}];
-(void)zoneChange:(NSNotification *)note
{
NSLog(@"The system time zone has changed!");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有