#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible]; return YES; } @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h" #import "LFRequestData.h" @interface RootViewController ()<NSURLConnectionDataDelegate,LFRequestDataDelegate>//导入代理 { NSMutableData *newsData;//存放数据 } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; LFRequestData *requestData = [[LFRequestData alloc] initWithURLString:@"http://c.m.163.com/nc/article/list/T1370583240249/0-20.html"]; //确定代理 requestData.delegate = self; } #pragma mark -- LFRequestDataDelegate -- - (void)revieceData:(NSMutableData *)data{ // NSLog(@"接受到数据了:%@",data); //解析 NSDictionary *news = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",news); } @end
#import <Foundation/Foundation.h> @protocol LFRequestDataDelegate ; //协议的申明 @interface LFRequestData : NSObject @property (nonatomic , weak) id<LFRequestDataDelegate> delegate;//设置代理 @property (nonatomic , strong) NSMutableData *newsData;//存放数据 /** * 初始化的方法 * * @param URLString 网址 */ - (LFRequestData*)initWithURLString:(NSString*)URLString; @end /** * 代理传值 */ @protocol LFRequestDataDelegate <NSObject> - (void)revieceData:(NSMutableData*)data; @end
#import "LFRequestData.h" @interface LFRequestData ()<NSURLConnectionDataDelegate>//导入代理 { NSString *URLStr; } @end @implementation LFRequestData - (LFRequestData*)initWithURLString:(NSString*)URLString{ //如果URLString则返回空 if (URLString.length == 0) { return nil; } URLStr = URLString; return [self init]; } -(instancetype)init{ self = [super init]; if (self) { //1.创建URL NSURL *url = [[NSURL alloc] initWithString:URLStr]; //2.通过URL创建网络请求 NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; //3.创建链接 NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; NSLog(@"connection:%@",connection); } return self; } /** * 服务器响应的时候自动执行 */ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSHTTPURLResponse *HTTPResopnse = (NSHTTPURLResponse *)response; NSLog(@"code:%ld",(long)HTTPResopnse.statusCode); //如果状态码是200,则响应服务器了 if (HTTPResopnse.statusCode == 200) { //初始化newsData self.newsData = [[NSMutableData alloc] init]; } } /** * 如果有新的数据到来,此方法自动执行 */ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ //追加新数据 [self.newsData appendData:data]; } /** * 请求完成后,自动执行的方法 */ - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ // NSLog(@"%@",self.newsData); //如果能响应代理的方法,则执行代理的方法 if ([_delegate respondsToSelector:@selector(revieceData:)]) { [self.delegate revieceData:self.newsData]; } } /** * 请求失败时自动执行 */ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"error:%@",[error localizedFailureReason]); } @end