2015-04-01 59 views
-1

所以 - 我有相同的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]; 
+0

你使用的是HTTPS?它是一個自簽名證書嗎? – 2015-04-01 20:39:03

回答

0

你可以試試這個,我做這一切的時候

#import "AFNetworking.h"

NSDictionary *params = @{ @"key1": txtBox1.text, @"key2", txtbox2.text}; 
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

AFJSONRequestSerializer *serializerRequest = [AFJSONRequestSerializer serializer]; 
AFJSONResponseSerializer *serializerResponse = [AFJSONResponseSerializer serializer]; 

[serializerRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]]; 
serializerResponse.acceptableContentTypes = [NSSet setWithObject:@"application/json"]; 

manager.requestSerializer = serializerRequest; 
manager.responseSerializer = serializerResponse; 

[manager.securityPolicy setAllowInvalidCertificates:YES]; 

[manager POST:@"url" 
    parameters:params 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     // DO WHAT YOU WANT IF SUCCESSED 
     } 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     // DO WHAT YOU WANT IF FAILED 
     }]; 
1

的一個區別是,你都報告不同的設備ID。也許您正在使用的API會跟蹤已授權的設備ID。而你的Mac已被授權,但iOS設備沒有。

您可以嘗試在Mac上記錄設備ID,然後更改您的代碼以將該ID用作硬編碼字符串,而不是使用iPhone返回的內容。

0

您是否使用取決於時間的認證方案?我已經看到模擬器上的漂移,導致它到401.

相關問題