所以 - 我有相同的AFNetworking代碼,在模擬器上運行時,返回的結果如預期的那樣,並且給我返回所有正確的JSON。設備上的AFNetworking 401,在模擬器上工作
不久,當我在設備上運行它時,我得到了一個401.現在,我明白這意味着'未授權',但它究竟如何在模擬器上絕對游泳,然後在設備上給我一個401?
該代碼如下,並且只是更改爲刪除我們的API的地址,該地址必須保持私密。
phoneNumber = phoneNumberField.text;
[[NSUserDefaults standardUserDefaults] setObject:phoneNumber forKey:@"phone_number"];
NSString *UDIDstring = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSLog(@"UDID: %@",UDIDstring);
NSString *jsonRequest = [NSString stringWithFormat:@"{\"userId\":\"%@\",\"userType\":\"%@\",\"deviceId\":\"%@\",\"deviceType\":\"%@\"}",phoneNumberField.text,@"Number",UDIDstring,@"iPhone"];
NSLog(@"json: %@",jsonRequest);
NSDictionary *stuff = [[NSDictionary alloc] initWithObjectsAndKeys:
phoneNumberField.text, @"userId",
@"Number", @"userType",
UDIDstring,@"deviceId",
@"iPhone",@"deviceType",
nil];
NSLog(@"stuff: %@",stuff);
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:stuff options:0 error:&error];
NSString *urlString = @"(hidden for privacy reasons)"
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
NSString *userpass = [@"vclient:VastV1" base64EncodedString];
NSLog(@"userpass: %@",userpass);
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"Basic: %@",userpass] forHTTPHeaderField:@"Authorization"];
//[request setValue:@"*/*" forHTTPHeaderField:@"Accept"];
[request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
op.securityPolicy = securityPolicy;
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON responseObject: %@ ",responseObject);
[infoLabel setText:@"You have been sent an activation code to the number you used. Please enter it above."];
[signupLabel setText:@"your code:"];
[mainButton setTitle:@"Activate" forState:UIControlStateNormal];
firstSignup=true;
[phoneNumberField setText:@""];
[phoneNumberField setPlaceholder:@"Enter activation code."];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
[op start];
你使用的是HTTPS?它是一個自簽名證書嗎? – 2015-04-01 20:39:03