car in 場站查車鎖車 ios
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

299 行
12KB

  1. //
  2. // LockCarViewController.m
  3. // goodpk
  4. //
  5. // Created by 歐特儀 on 2020/3/20.
  6. // Copyright © 2020 Altob. All rights reserved.
  7. //
  8. #import "LockCarViewController.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. #import "LockModel.h"
  11. #import "CocoaSecurity.h"
  12. #import "Util.h"
  13. #import <CommonCrypto/CommonDigest.h>
  14. #define APIURL @"https://cloudservice.altob.com.tw/LockCarSerivce/api/JumpApi"
  15. #define CARSECURITY @"/CARSECURITY?"
  16. @interface LockCarViewController ()
  17. @property (weak, nonatomic) IBOutlet UIImageView *Image;
  18. @property (weak, nonatomic) IBOutlet UIButton *LockBtn;
  19. @property (weak, nonatomic) IBOutlet UIButton *nLockBtn;
  20. @end
  21. @implementation LockCarViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. //NSLog(@"傳值:%@", self.status);
  26. //NSLog(@"傳值:%@", self.acct);
  27. //NSLog(@"傳值:%@", self.pwd);
  28. if([self.status containsString:@"unlocked"]){
  29. self.nLockBtn.userInteractionEnabled = NO;
  30. self.nLockBtn.alpha = 0.4;
  31. }else{
  32. self.LockBtn.userInteractionEnabled = NO;
  33. self.LockBtn.alpha = 0.4;
  34. self.Image.image = [UIImage imageNamed:@"Lock_on"];
  35. }
  36. [self.LockBtn setTag:0];
  37. [self.nLockBtn setTag:1];
  38. [self.LockBtn addTarget:self
  39. action:@selector(handleButtonClicked:)
  40. forControlEvents:UIControlEventTouchUpInside
  41. ];
  42. [self.nLockBtn addTarget:self
  43. action:@selector(handleButtonClicked:)
  44. forControlEvents:UIControlEventTouchUpInside
  45. ];
  46. }
  47. - (void) handleButtonClicked:(id)sender {
  48. switch ([sender tag]) {
  49. case 0:
  50. //API 鎖車
  51. [self Lock_api:self.acct :self.pwd];
  52. break;
  53. case 1:
  54. //API 解鎖
  55. [self nLock_api:self.acct :self.pwd];
  56. break;
  57. }
  58. }
  59. //鎖車
  60. -(void) Lock_api:(NSString *)acct :(NSString *)pwd {
  61. CocoaSecurityResult *Nacct = [CocoaSecurity aesEncrypt:acct key:[Util sha256HashFor:@"Altob"]];
  62. NSString *status = @"1";
  63. //NSLog(@"加密字串:%@", [NSString stringWithFormat:@"%@%@%@%@%@", acct, @"i", pwd, @"iii", status]);
  64. NSString *key = [self md5: [NSString stringWithFormat:@"%@%@%@%@%@", acct, @"i", pwd, @"iii", status]];
  65. NSString *carUrl = [NSString stringWithFormat:@"%@%@acct=%@&pwd=%@&status=%@&key=%@&station=%@", APIURL, CARSECURITY, Nacct.base64, pwd, status, key, self.station];
  66. //第一步,创建URL
  67. NSURL *url = [NSURL URLWithString:carUrl];
  68. //NSDictionary *jsonBodyDict = @{};
  69. //NSLog(@"URL: %@", url);
  70. //第二步,创建请求
  71. NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
  72. //设置请求方式为POST,默认为GET
  73. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  74. [request setValue:@"Bearer 9a406a82bd551e6ef8e845f42f788af4" forHTTPHeaderField:@"Authorization"];
  75. [request setHTTPMethod:@"GET"];
  76. //设置参数
  77. //NSData *jsonBodyData = [NSJSONSerialization dataWithJSONObject:jsonBodyDict options:kNilOptions error:nil];
  78. //[request setHTTPBody:jsonBodyData];
  79. //新作法
  80. NSURLSession *sesson = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
  81. // 2.创建 NSURLSessionDataTask
  82. NSURLSessionDataTask *dataTask = [sesson dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  83. if (error) {
  84. // error
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. [self showAlert: @"注意" : @"您所使用的連線加密(SSL)異常,基於安全考量不提供相關功能。請確認您的連線環境及App下載來源安全。" : 0];
  87. });
  88. }else {
  89. // 获得数据后,返回到主线程更新 UI
  90. dispatch_async(dispatch_get_main_queue(), ^{
  91. NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  92. LockModel *LK= [[LockModel alloc] initWithString:responseString error:nil];
  93. if([LK.result_code isEqual:@"OK"]){
  94. self.LockBtn.userInteractionEnabled = NO;
  95. self.LockBtn.alpha = 0.4;
  96. self.Image.image = [UIImage imageNamed:@"Lock_on"];
  97. self.nLockBtn.userInteractionEnabled = YES;
  98. self.nLockBtn.alpha = 1;
  99. }else{
  100. [self showAlert:@"錯誤" :@"鎖車失敗,請稍後再試" :nil];
  101. }
  102. });
  103. }
  104. }];
  105. // 3.执行 Task
  106. [dataTask resume];
  107. /*
  108. //第三步,连接服务器
  109. NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  110. NSString *jsonString = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
  111. //NSLog(@"CAR LOGIN:%@", jsonString);
  112. LockModel *LK= [[LockModel alloc] initWithString:jsonString error:nil];
  113. if([LK.result_code isEqual:@"OK"]){
  114. self.LockBtn.userInteractionEnabled = NO;
  115. self.LockBtn.alpha = 0.4;
  116. self.Image.image = [UIImage imageNamed:@"Lock_on"];
  117. self.nLockBtn.userInteractionEnabled = YES;
  118. self.nLockBtn.alpha = 1;
  119. }else{
  120. [self showAlert:@"錯誤" :@"鎖車失敗,請稍後再試" :nil];
  121. }
  122. */
  123. }
  124. //解鎖
  125. -(void) nLock_api:(NSString *)acct :(NSString *)pwd {
  126. CocoaSecurityResult *Nacct = [CocoaSecurity aesEncrypt:acct key:[Util sha256HashFor:@"Altob"]];
  127. NSString *status = @"0";
  128. //NSLog(@"加密字串:%@", [NSString stringWithFormat:@"%@%@%@%@%@", acct, @"i", pwd, @"iii", status]);
  129. NSString *key = [self md5: [NSString stringWithFormat:@"%@%@%@%@%@", acct, @"i", pwd, @"iii", status]];
  130. NSString *carUrl = [NSString stringWithFormat:@"%@%@acct=%@&pwd=%@&status=%@&key=%@&station=%@", APIURL, CARSECURITY, Nacct.base64, pwd, status, key, self.station];
  131. //第一步,创建URL
  132. NSURL *url = [NSURL URLWithString:carUrl];
  133. //NSDictionary *jsonBodyDict = @{};
  134. //NSLog(@"URL: %@", url);
  135. //第二步,创建请求
  136. NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
  137. //设置请求方式为POST,默认为GET
  138. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  139. [request setValue:@"Bearer 9a406a82bd551e6ef8e845f42f788af4" forHTTPHeaderField:@"Authorization"];
  140. [request setHTTPMethod:@"GET"];
  141. //设置参数
  142. //NSData *jsonBodyData = [NSJSONSerialization dataWithJSONObject:jsonBodyDict options:kNilOptions error:nil];
  143. //[request setHTTPBody:jsonBodyData];
  144. //新作法
  145. NSURLSession *sesson = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
  146. // 2.创建 NSURLSessionDataTask
  147. NSURLSessionDataTask *dataTask = [sesson dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  148. if (error) {
  149. // error
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. [self showAlert: @"注意" : @"您所使用的連線加密(SSL)異常,基於安全考量不提供相關功能。請確認您的連線環境及App下載來源安全。" : 0];
  152. });
  153. }else {
  154. // 获得数据后,返回到主线程更新 UI
  155. dispatch_async(dispatch_get_main_queue(), ^{
  156. NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  157. LockModel *LK= [[LockModel alloc] initWithString:responseString error:nil];
  158. if([LK.result_code isEqual:@"OK"]){
  159. self.nLockBtn.userInteractionEnabled = NO;
  160. self.nLockBtn.alpha = 0.4;
  161. self.Image.image = [UIImage imageNamed:@"Lock_off"];
  162. self.LockBtn.userInteractionEnabled = YES;
  163. self.LockBtn.alpha = 1;
  164. }else{
  165. [self showAlert:@"錯誤" :@"解鎖失敗,請稍後再試" :nil];
  166. }
  167. });
  168. }
  169. }];
  170. // 3.执行 Task
  171. [dataTask resume];
  172. /*
  173. //第三步,连接服务器
  174. NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  175. NSString *jsonString = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
  176. //NSLog(@"CAR LOGIN:%@", jsonString);
  177. LockModel *LK= [[LockModel alloc] initWithString:jsonString error:nil];
  178. if([LK.result_code isEqual:@"OK"]){
  179. self.nLockBtn.userInteractionEnabled = NO;
  180. self.nLockBtn.alpha = 0.4;
  181. self.Image.image = [UIImage imageNamed:@"Lock_off"];
  182. self.LockBtn.userInteractionEnabled = YES;
  183. self.LockBtn.alpha = 1;
  184. }else{
  185. [self showAlert:@"錯誤" :@"解鎖失敗,請稍後再試" :nil];
  186. }
  187. */
  188. }
  189. - (NSString *) md5:(NSString *) input
  190. {
  191. const char *cStr = [input UTF8String];
  192. unsigned char digest[16];
  193. CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
  194. NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  195. for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
  196. [output appendFormat:@"%02x", digest[i]];
  197. return output;
  198. }
  199. -(void)showAlert:(NSString *)title :(NSString *)text: (NSInteger *)type{
  200. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
  201. message:text
  202. preferredStyle:UIAlertControllerStyleAlert ];
  203. UIAlertAction *sinupAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
  204. }];
  205. [alertController addAction:sinupAction];
  206. [self presentViewController:alertController animated:YES completion:nil];
  207. }
  208. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler//通过调用block,来告诉NSURLSession要不要收到这个证书
  209. {
  210. // Get remote certificate
  211. SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
  212. SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);
  213. // Set SSL policies for domain name check
  214. NSMutableArray *policies = [NSMutableArray array];
  215. [policies addObject:(__bridge_transfer id)SecPolicyCreateSSL(true, (__bridge CFStringRef)challenge.protectionSpace.host)];
  216. SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef)policies);
  217. // Evaluate server certificate
  218. SecTrustResultType result;
  219. SecTrustEvaluate(serverTrust, &result);
  220. BOOL certificateIsValid = (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
  221. // Get local and remote cert data
  222. NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate));
  223. NSString *hash = [[self doSha256:remoteCertificateData] base64EncodedStringWithOptions:0];
  224. // The pinnning check
  225. if ([hash isEqualToString:@"FHloZIw4i6+30lmxrUujLieHlIDpxHySL1niMxvgmpU="] && certificateIsValid) {
  226. NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
  227. completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
  228. } else {
  229. completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, NULL);
  230. }
  231. }
  232. - (NSData *)doSha256:(NSData *)dataIn {
  233. NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
  234. CC_SHA256(dataIn.bytes, dataIn.length, macOut.mutableBytes);
  235. return macOut;
  236. }
  237. @end