LYRequest 0.2

LYRequest 0.2

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2015年1月

Leon(雷勇) 维护。



  • 作者:
  • Leon(雷勇)

LYNetwork

基本请求网络库(LYNetwork是一个完全开源的基于GET和POST请求的网络库)

安装

 platform :ios, '7.0'
 pod 'LYRequest', '~> 0.1'
pod install

使用

  • GET
  NSURL *url = [NSURL URLWithString:@"http://114.215.101.94:83/service/ads.php?type=1&flag=1"];
  LYRequest *request = [LYRequest shareInstance];
  [request requestWithURL:url
                   params:nil
                  method:@"get"
                 useCache:NO
             finishBlock:^(NSData *data) {
                  id obj = [NSJSONSerialization JSONObjectWithData:data
                                                           options:NSJSONReadingMutableContainers
                                                             error:nil];
                  NSLog(@"%@", obj);
             } errorBlock:^(NSURLConnection *connection, NSError *error) {
                  NSLog(@"error");
              }];
  • POST
  NSURL *url = [NSURL URLWithString:@"http://114.215.101.94:83/service/ads.php"];
  NSDictionary *dict = @{@"type":@"1", @"flag":@"1"};
  LYRequest *request = [LYRequest shareInstance];
  [request requestWithURL:url
                   params:dict
                   method:@"post"
                 useCache:NO
              finishBlock:^(NSData *data) {
                  id obj = [NSJSONSerialization JSONObjectWithData:data
                                                           options:NSJSONReadingMutableContainers
                                                             error:nil];
                  NSLog(@"%@", obj);
              } errorBlock:^(NSURLConnection *connection, NSError *error) {
                  NSLog(@"error");
              }];
  • 下载
  LYRequest *request = [LYRequest shareInstance];
  NSURL *url = [NSURL URLWithString:@"https://d.alipayobjects.com/sec/edit/beta/wkaliedit.dmg"];
  [request downloadWithURL:url progressBlock:^(float progress) {
      self.label.text = [NSString stringWithFormat:@"%d%%",(int)progress];
  } finishBlock:^(NSData *data) {
      NSString *filePath = [[self getDocumentPath] stringByAppendingPathComponent:[url lastPathComponent]];
      [data writeToFile:filePath atomically:YES];
  } errorBlock:^(NSURLConnection *connection, NSError *error) {
      NSLog(@"error");
  }];
  • 上传
    LYRequest *request = [LYRequest shareInstance];
    NSURL *url = [NSURL URLWithString:@"http://115.29.249.23:8081/Receive.ashx?operation=fqsp"]; // your fileupload address
    NSString *path = [[NSBundle mainBundle] pathForResource:@"IMG_1710.JPG" ofType:nil];
    NSDictionary *params = @{@"approvalid":@"9",
                             @"approvalname":@"ok",
                             @"categoryid":@"2",
                             @"contents":@"Try",
                             @"title":@"Leon",
                             @"userid":@"260",
                             @"username":@"admin",
                             @"workname":@"WorkApproval"};
    [request uploadWithURL:url filename:@"IMG_1710.JPG" params:params filePath:path progress:^(float progress) {
        self.label.text = [NSString stringWithFormat:@"%d%%",(int)progress];
    } finish:^(NSData *data) {
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"result =%@", result);
    } error:^(NSURLConnection *connection, NSError *error) {
        NSLog(@"error");
    }];