validate self signed certificate wit AFNetworking - ios

I want to connect my iOS App with AFNetwork to my webserver with a self-signed certificate. I found a solution on github (https://github.com/AFNetworking/AFNetworking/pull/694) I tried it out and the certificate pinning seems to work but i got an other error:
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed.
(NSURLErrorDomain error -1012.)" UserInfo=0x7bc2090 {NSErrorFailingURLKey=(my domain)}
Does anybody know whether this error has to do with the AFNetworking Framework and the self signed certificate or not?
Solved:
I found the solution for the error. I had to set the SSLPinningMode to AFSSLPinningModeCertificate now it works.
AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"%#",error);
}];
operation.SSLPinningMode = AFSSLPinningModeCertificate;
[operation start];

Try this work around.
AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"%#",error);
}];
operation.securityPolicy.allowInvalidCertificates = YES;
[operation start];

Related

Convert AFJsonRequestOperation setUploadProgressBlock from AFNetworking 1.6 to AFNetworking 3.1

Is that possible to rewrite following code used AFNetworking v 1.6:
__block AFJSONRequestOperation *operation =
[AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self uploadFinished:JSON completionBlock:completion failureBlock:failure];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
[self uploadFailedWithError:error completionBlock:completion failureBlock:failure];
[UIApplication hideProgressLoading];
}];
[operation
setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
[UIApplication updateProgress:totalBytesWritten * 1.0/totalBytesExpectedToWrite];
}];
[operation start];
to the AFNetworking 3.1 version?

AFJSONRequestOperation create "oauth_problem=signature_invalid"

I am developing app which require to use OAuth1.0 for call API.
I am able to Authenticate with OAuth1 and call GET method API.
But when I try to call POST method with passing JSON object. It give me "oauth_problem=signature_invalid"
Code for request :
NSMutableURLRequest *request = [self.twitterClient requestWithMethod:#"POST" path:apiURL parameters:jsonObj];
AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Success: %#", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Error: %#", error);
}];
[jsonOperation start];
I am struggling with this. Any help will be appreciated. Thanks in advance.
I finally solve problem.
In my case problem was with signature_method
I set..
self.twitterClient.signatureMethod = AFPlainTextSignatureMethod;
before call request. And it works.

Json Accelerator and AFNetworking

When the operation is running, I can not enter data in JSON model CREATED BY ACCELERATOR.
Can you tell me what I am doing wrong?
{
[super viewDidLoad];
NSLog(#"you are in a tableViewController");
self.title = #"NavigationOrdini";
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.stampa6x3.com/json.php?azione=ordini"]];
AFJSONRequestOperation* operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:req
success:^(NSURLRequest *request, NSURLResponse *response, id JSON)
{
[[ordiniModel alloc] initWithDictionary:JSON];
}
failure:^(NSURLRequest *request, NSURLResponse *response, NSError
*error, id JSON) {
[self setTitle:#"Dictionary"];
NSLog(#"failed! %d",[error code]);
}];
[operation start];
ordiniModel*test;
NSLog(#"il valore è %#",test.ordini.description);
}
AFJSONRequestOperation is asynchronous, meaning it that the code continues to execute while the rest of the app runs. The completion block are run when the code actually completes.
So try:
NSLog(#"you are in a tableViewController");
self.title = #"NavigationOrdini";
ordiniModel *test; // <-- create variable here
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.stampa6x3.com/json.php?azione=ordini"]];
AFJSONRequestOperation* operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:req
success:^(NSURLRequest *request, NSURLResponse *response, id JSON)
{
test = [[ordiniModel alloc] initWithDictionary:JSON]; // <-- Assign here
NSLog(#"il valore è %#",test.ordini.description);
}
failure:^(NSURLRequest *request, NSURLResponse *response, NSError
*error, id JSON) {
[self setTitle:#"Dictionary"];
NSLog(#"failed! %d",[error code]);
}];
[operation start];

Using AFHTTPClient for multiple JSONRequestOperations

I'm relatively new to AFNetworking and would like to find a better solution for the following problem.
I am using 2 AFJSONRequestOperations at the start of my iOS application.
AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request1 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// do something
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error){
// do something
}];
AFJSONRequestOperation *operation2 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request2 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// do something
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error) {
// do something
}];
[operation1 start];
[operation2 start];
I wait for both operations to finish and start further processing.
I thought I can combine both of this operations into the enqueueBatchOfHTTPRequestOperationsWithRequests method.
AFHTTPClient *client = [[AFHTTPClient alloc] init];
NSArray *requests = [NSArray arrayWithObjects:operation1, operation2, nil];
[client enqueueBatchOfHTTPRequestOperationsWithRequests:requests progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
// track operations
}completionBlock:^(NSArray *operations) {
// do something
}];
So I would like to track the finished operations and execute the completion block for each operation separatly. Is it something I can perform with AFHTTPClient?

Error with AFNetworking for JSON Parsing

I have the following code for JSON Parsing:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.dropbox.com/s/qz16qyi3julygl9/facebook.json"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Request Success %#",[JSON class]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failure Because %#",[error userInfo]);
}];
[operation start];
but I have Request Failure with the following error message:
NSErrorFailingURLKey = "https://www.dropbox.com/s/qz16qyi3julygl9/facebook.json";
NSLocalizedDescription = "Expected content type {(\n \"text/json\",\n \"application/json\",\n \"text/javascript\"\n)}, got text/html";
can somebody help me?
In my errorlog it prints "got text/html". So just add
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:#"text/html"]]
It works.
[AFJSONRequestOperation addAcceptableContentTypes:#"text/plain"]
The above is deprecated from AFNetworking 2.x. Instead you can call the following on the instance of the AFHTTPRequestOperation as follows
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/plain"];
Where manager is your instance of AFHTTPRequestOperation.
Source: https://github.com/AFNetworking/AFNetworking/issues/1381
Because the link you provide doesn't hotlink the file. It links to an HTML page to download the file. Try going there in a browser...
Try this link instead: https://dl.dropbox.com/s/qz16qyi3julygl9/facebook.json?dl=1 No guarantees it will work though. A lot of companies frown on directly linking to files in this way.

Resources