RequestManager.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // RequestManager.m
  3. // AJPushDemo
  4. //
  5. // Created by kean_qi on 2021/3/17.
  6. //
  7. #import "RequestManager.h"
  8. @implementation RequestManager
  9. + (instancetype)sharedInstance {
  10. static id _instance;
  11. static dispatch_once_t onceToken;
  12. dispatch_once(&onceToken, ^{
  13. _instance = [[self alloc] init];
  14. });
  15. return _instance;
  16. }
  17. - (void)GETRequestWithUrl:(NSString *)urlString paramaters:(NSMutableDictionary *)paramaters successBlock:(SuccessBlock)success FailBlock:(failBlock)fail{
  18. NSMutableString *strM = [[NSMutableString alloc] init];
  19. [paramaters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  20. // 服务器接收参数的 key 值.
  21. NSString *paramaterKey = key;
  22. // 参数内容
  23. NSString *paramaterValue = obj;
  24. // appendFormat :可变字符串直接拼接的方法!
  25. [strM appendFormat:@"%@=%@&",paramaterKey,paramaterValue];
  26. }];
  27. urlString = [NSString stringWithFormat:@"%@?%@",urlString,strM];
  28. // 截取字符串的方法!
  29. urlString = [urlString substringToIndex:urlString.length - 1];
  30. NSLog(@"urlString:%@",urlString);
  31. NSURL *url = [NSURL URLWithString:urlString];
  32. NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
  33. // 2. 发送网络请求.
  34. // completionHandler: 说明网络请求完成!
  35. [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  36. NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
  37. // 网络请求成功:
  38. if (data && !error) {
  39. // 查看 data 是否是 JSON 数据.
  40. // JSON 解析.
  41. id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
  42. // 如果 obj 能够解析,说明就是 JSON
  43. if (!obj) {
  44. obj = data;
  45. }
  46. // 成功回调
  47. dispatch_async(dispatch_get_main_queue(), ^{
  48. if (success) {
  49. success(obj,response);
  50. }
  51. });
  52. } else {
  53. // 失败回调
  54. if (fail) {
  55. fail(error);
  56. }
  57. }
  58. }] resume];
  59. }
  60. //POST请求方法
  61. - (void)POSTRequestWithUrl:(NSString *)urlString paramaters:(NSMutableDictionary *)paramaters successBlock:(SuccessBlock)success FailBlock:(failBlock)fail {
  62. // NSMutableString *strM = [[NSMutableString alloc] init];
  63. // [paramaters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  64. // // 服务器接收参数的 key 值.
  65. // NSString *paramaterKey = key;
  66. // // 参数内容
  67. // NSString *paramaterValue = obj;
  68. // // appendFormat :可变字符串直接拼接的方法!
  69. // [strM appendFormat:@"%@=%@&",paramaterKey,paramaterValue];
  70. //
  71. // }];
  72. // NSString *body = [strM substringToIndex:strM.length - 1];
  73. // NSData *bodyData = [body dataUsingEncoding:NSUTF8StringEncoding];
  74. NSLog(@"%@",urlString);
  75. NSLog(@"%@",paramaters);
  76. NSURL *url = [NSURL URLWithString:urlString];
  77. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
  78. // 1.设置请求方法:
  79. request.HTTPMethod = @"POST";
  80. [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
  81. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  82. // 2.设置请求体
  83. // request.HTTPBody = bodyData;
  84. request.HTTPBody = [NSJSONSerialization dataWithJSONObject:paramaters options:NSJSONWritingPrettyPrinted error:nil];
  85. // 2. 发送网络请求.
  86. // completionHandler: 说明网络请求完成!
  87. [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  88. // 网络请求成功:
  89. if (data && !error) {
  90. // 查看 data 是否是 JSON 数据.
  91. // JSON 解析.
  92. id obj = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
  93. NSLog(@"%@",obj);
  94. // 如果 obj 能够解析,说明就是 JSON
  95. if (!obj) {
  96. obj = data;
  97. }
  98. // 成功回调
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. if (success) {
  101. success(obj,response);
  102. }
  103. });
  104. } else {
  105. // 失败回调
  106. if (fail) {
  107. fail(error);
  108. }
  109. }
  110. }] resume];
  111. }
  112. @end