hopefully this is an easy question, tho, I cannot find any specific answers.
we've gone thru all the steps to update our enterprise app OTA. My question is, can I use AFNetworking to make the call? or what is the best way to call the link. (currently afnetworking is giving me errors but it may be something on our side.) I am using afnetworking exclusively, so would rather not change unless I have to.
thanks in advance
itms-services://?action=download-manifest&url=http://ourServer/Setup/manifest.plist
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kiPADSetupLink]];
[request setTimeoutInterval:300];
NSLog(#"begin downloading app update");
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
// handle success
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// handle error
} ];
[operation start];
No, you need the system to open that URL, you can't access it yourself. Use UIApplication's openURL: method.
Related
I'm trying to upload multiple photos in background mode using AFNetworking and I managed to make it work.
The main problem I'm facing now is memory, which, while uploading more than 10 photos, runs out terminating my app.
What I'm doing is just firing all the uploads simultaneously as I've read in some answers on StackOverflow.
Now that this problem arises I'm wondering if I'm doing something wrong with memory management or if a better strategy would be to serialize the uploads, start the first one and when it terminates start the upload of the next in the handleEventsForBackgroundURLSession method.
Before changing completely the upload design I would like to hear from someone if it's a good alternative, as the majority of answers I've seen regarding this matter state that one should fire all the requests together.
Thank you
Create NSOperationQueue and add all your upload image operation to that queue. This queue will manage your system memory. Please refer below sample code.
NSOperationQueue *myQueue = [[NSOperationQueue alloc]init];
NSURLRequest *request = [[AFHTTPRequestSerializer serializer]
multipartFormRequestWithMethod:#"POST"
URLString:apiPostPhoto(singleton.userId, #"icon")
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSString *filepath = [[CustomFunctions getFilesPath] stringByAppendingPathComponent:#"icon.png"];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:filepath] name:#"uploadicon" error:nil];
} error:nil
];
AFHTTPRequestOperation *operationUploadOne = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"uploadlogo:%#",operation.responseString);
[[NSUserDefaults standardUserDefaults]setObject:operation.responseString forKey:KEY_LOGO_TIMESTAMP];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"uploadlogo:%#",[error description]);
}];
[operation addObserver:self forKeyPath:#"isFinished" options:NSKeyValueObservingOptionNew context:nil];
[myQueue addOperation:operationUploadOne];
AFHTTPRequestOperation *operationUploadTwo = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[myQueue addOperation: operationUploadTwo];
you can add more number of operation using this method.
[myQueue addOperation: operationUploadTwo];
I am building an app that downloads a large number of images, sometimes 1500-5000 images depending on what the user requests. To do this, I am using AFNetworking 2. At first, I was just looping through all of my URLs and then making a request for each one.
for (NSString *url in urls) {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
[requestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
double percentDone = (double)totalBytesRead / (double)totalBytesExpectedToRead;
progress(percentDone);
}];
[requestOperation start];
}
But, after I got to about 900 downloads/requests I would start to get the following error:
The request timed out
I am assuming this error came directly from AFNetworking.
What is the best and most efficient way to make a large number of download requests like this without timing out? Should I be using dispatch_group to batch the requests as outlined here?
Or, should I use a recursive method that will download one image at a time, and only start the next request once the first one finishes?
Try this
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setTimeoutInterval:600]; /* 10 minutes */
But it would be best solution if you just download an archive of images and then unpack it.
This is probably something simple that I am just overlooking. I am trying to make an AFHTTPRequest from uStream's API using AFNetworking. I should be getting a JSON payload response back that lists all the videos on a uStream Channel. Here is my code:
NSURL *url = [NSURL URLWithString:#"https://api.ustream.tv/channels/12321320/videos.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"failed");
}];
[operation start];
Everything works fine when testing in a browser but when trying to use AFNetworking I get a NSURLErrorDomain failure. Anyone have any suggestions?
My code was fine but there was an issue with my iphone simulator. Reset Content and Settings did the trick. Thanks to k6sandeep for the help.
After few days of job I am not able to solve this issue, I really need your help because I am completely locked, I start to be crazy!!!!!!. I have a project in Objective c for iOS where I get data from my server to put in my application. I have some trouble to recover and save data from JSON.
I would like to use "id responseObject", and save and use the content in another area in my project. Each time I try to use the following method and use "id responseObject" outside of "setCompletionBlockWithSucess" the "id responseObject" is (null), how can I do ?
NSURL *URL = [NSURL URLWithString:#"…."];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
[[NSOperationQueue mainQueue] addOperation:op]
It sounds like you don't quite understand what scope is for variables. This project may be too advanced for you if that's the case. I urge you to read into scope + blocks to get a better understanding of what's going on.
What is happening is the setCompletionBlockWithSuccess is actually a block of code that gets executed if the URL request is a success. This means that responseObject is not immediately executed! It's being passed back some time after and you get access to it within setCompletionBlockWithSuccess. So that's why it's nil outside of the block.
To do what you're wanting is very simple. You need to read responseObject within the setCompletionBlockWithSuccess and set it to another variable that you have access to. Or you can immediately send it to another class to parse/save.
I haven't tested it, but I believe this should work with a simple JSON response. If not, use operation.responseString instead
NSString *jsonResponse;
NSURL *URL = [NSURL URLWithString:#"…."];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
jsonResponse = responseObject;
}
...
I have been using following code
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.allowsInvalidSSLCertificate=YES;
Now i have changed AFNetworking to latest version that is 2.0. operation.allowsInvalidSSLCertificate is not working anymore with AFHTTPRequestOperation. As per documents i used
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;
and my request code is
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog (#"success: %#", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"error: %#", error.description);
}
];
[operation start];
[operation waitUntilFinished];
But this is not working for HTTPS which require certificates. What should i do to make this work?
I solved this problem by adding following code before [operation start];
AFSecurityPolicy *sec=[[AFSecurityPolicy alloc] init];
[sec setAllowInvalidCertificates:YES];
operation.securityPolicy=sec;
This happened because AFHTTPRequestOperationManager is not connected to AFHTTPRequestOperation. So setting manager's security certificate can not do magic at requestOperation. So have to initialize and assign one to AFHTTPRequestOperation.
Hope this help somebody :)
As far as I know. It's an iOS 7 issue. Apple does not allow communication with self signed certificate websites. Unless you send the certificate to device and add as a trusted certificate list.
Supporting comment on other question: https://stackoverflow.com/a/20251011/753603
Couldn't find piece of documented text by Apple.