Can AFNetworking return data synchronously (inside a block)?

To block the execution of the main thread until the operation completes, you could do [operation waitUntilFinished] after it’s added to the operation queue. In this case, you wouldn’t need the return in the block; setting the __block variable would be enough. That said, I’d strongly discourage forcing asynchronous operations to synchronous methods. It’s tricky … Read more

Are AFNetworking success/failure blocks invoked on the main thread?

They are invoked on the main queue, unless you explictly sets the queue on AFHTTPRequestOperation, as shown in setCompletionBlockWithSuccess:failure from AFHTTPRequestOperation.m self.completionBlock = ^{ if (self.error) { if (failure) { dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ failure(self, self.error); }); } } else { if (success) { dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ success(self, self.responseData); }); } } };

How to download a file and save it to the documents directory with AFNetworking?

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@”…”]]; AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@”filename”]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@”Successfully downloaded file to %@”, path); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@”Error: %@”, error); }]; [operation start];

AFNetworking and background transfers

A couple of thoughts: You have to make sure you do the necessary coding outlined in the Handling iOS Background Activity section of the URL Loading System Programming Guide says: If you are using NSURLSession in iOS, your app is automatically relaunched when a download completes. Your app’s application:handleEventsForBackgroundURLSession:completionHandler: app delegate method is responsible for … Read more

List saved files in iOS documents directory in a UITableView?

Here is the method I use to get the content of a directory. -(NSArray *)listFileAtPath:(NSString *)path { //—–> LIST ALL FILES <—–// NSLog(@”LISTING ALL FILES FOUND”); int count; NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL]; for (count = 0; count < (int)[directoryContent count]; count++) { NSLog(@”File %d: %@”, (count + 1), [directoryContent objectAtIndex:count]); } return … Read more

how to use Alamofire with custom headers

According to the official documentation, modifying the session configuration is not recommended: This is not recommended for Authorization or Content-Type headers. Instead, use URLRequestConvertible and ParameterEncoding, respectively. So an example usage of URLRequestConvertible for authorization would be: enum Router: URLRequestConvertible { static let baseUrlString = “some url string” case Get(query: String) var URLRequest: NSMutableURLRequest { … Read more

AFNetworking 2.0 add headers to GET request

Here’s an example using AFNetworking 2.0 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager.requestSerializer setValue:@”calvinAndHobbesRock” forHTTPHeaderField:@”X-I do what I want”]; [manager GET:@”http://localhost:3000″ parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@”JSON: %@”, responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@”Error: %@”, error); }]; The key are the following 2 lines: manager.requestSerializer … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)