異步請求:網絡
-(BOOL)getOnlyKey1 { NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; __block bool isTrue = false; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"]; NSURL *url = [NSURL URLWithString:urlstr]; NSDictionary *dic = @{@"imei":myUUIDStr,@"av":AppVersion}; [manager POST:urlstr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) { MyLog(@"%@", operation.responseString); NSRange range = [operation.responseString rangeOfString:@"\"msg\":\"0\""]; if (range.location != NSNotFound) { isTrue = true; } if (!isTrue) { SHOWALERT(@"錯誤", @"您需要聯繫開發者"); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { MyLog(@"返回失敗結果:%@", error.localizedFailureReason); SHOWALERT(@"錯誤", @"請求開發者server失敗"); isTrue = true; }]; return isTrue; }
-(BOOL)getOnlyKey2 { NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; BOOL isTrue = false; NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"]; NSURL *url = [NSURL URLWithString:urlstr]; NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:url]; urlrequest.HTTPMethod = @"POST"; NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, AppVersion]; NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; urlrequest.HTTPBody = body; AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlrequest]; requestOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; [requestOperation start]; [requestOperation waitUntilFinished]; MyLog(@"%@",requestOperation.responseString); NSRange range = [requestOperation.responseString rangeOfString:@"\"msg\":\"0\""]; if (range.location != NSNotFound) { isTrue = true; } if (!isTrue) { SHOWALERT(@"錯誤", @"您需要聯繫開發者"); } return isTrue; }
-(BOOL)getOnlyKey { NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; //應用版本 NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary]; NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"]; NSString *urlString = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, versionNum]; //將nstring轉換成nsdata NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; //MyLog(@"body data %@", body); [request setHTTPBody:body]; NSURLResponse *response = nil; NSError *error = nil; //第二,三個參數是指針的指針,所有要用取址符,這種方法是同步方法。同步操做沒有完畢。後面的代碼不會運行。 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; // MyLog(@"返回結果是:%@", str); if (error == nil) { //接受到數據,表示工做正常 NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; MyLog(@"%@",str); NSRange range = [str rangeOfString:@"\"msg\":\"0\""]; if (range.location != NSNotFound) { return true; }else{ return false; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出錯鳥" message:@"您需要聯繫項目開發者" delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil]; [alert show]; } } if(error != nil || response == nil) { return false; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"錯誤" message:@"登錄失敗。網絡不穩定" delegate:nil cancelButtonTitle:@"肯定" otherButtonTitles:nil]; [alert show]; } return false; }