4

My code is making two requests to the server. One into the uiwebview directly and one with the AFHTTPRequestOperation. I'd like to use the AFHTTPRequestOperation and just pass the response into my uiwebview. What is the correct syntax for passing the response into the uiwebview and not loading it again? How do I do that without calling twice from the server? I still want to be able to test the success or failure of the load request and also send username and password to connect to the url.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[itemField resignFirstResponder];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userName = [defaults objectForKey:@"storedUserName"];
NSString *passWord = [defaults objectForKey:@"storedPassWord"];

NSURL *url = [NSURL URLWithString:@"http://www.example.net/"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL: url];

[client setAuthorizationHeaderWithUsername:userName password:passWord];

NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:[@"/example/itemlookup.php?item=" stringByAppendingString:itemField.text] parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    //calling again here was the only way I could figure out to get this into my webview
    //need to get response from above and put into the uiwebview
    [webView loadRequest:request];
    NSLog(@"Success");
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failure"); 
}];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];


return YES; 
}
jjclarkson
  • 5,890
  • 6
  • 40
  • 62
  • I don't have a real answer, just a couple of thoughts.... What actual kind of thing is given to the block as `responseObject`? Also, clients have been known to send a 'HEAD' method request to verify a resource before loading but I don't know if that supports authorization. – Phillip Mills Jul 26 '12 at 19:07

2 Answers2

1

I'd recommend using AFNetworking's UIWebView category which does exactly this; uses an AFHTTPRequestOperation to load the content of a webpage and then loads it into the web view as an HTML string.

Otherwise I'd recommend taking a look at the category to see if you can adapt its code for your use. https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/UIWebView%2BAFNetworking.m

Jon Shier
  • 12,200
  • 3
  • 35
  • 37
0

If that URL is returning HTML, you can use the - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL method of the UIWebView.

Something like:

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    [webView loadHTMLString:responseObject baseURL:url]; //If responseObject is HTML 
    NSLog(@"Success");
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failure"); 
}];

The only thing that might change is the response object. You may be not getting a string, but a NSURLResponse or something to that nature.

James Paolantonio
  • 2,194
  • 1
  • 15
  • 32
  • This resulted in: -[__NSCFData dataUsingEncoding:]: unrecognized selector sent to instance 0x6894300 2012-07-30 11:18:21.533 item_lookup[398:11503] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData dataUsingEncoding:]: unrecognized selector sent to instance 0x6894300' I guess that means that the responseObject is not just returning HTML. – jjclarkson Jul 30 '12 at 20:30
  • 1
    That means responseObject is data and not a string. Convert it by using these instructions: http://stackoverflow.com/questions/2467844/convert-utf-8-encoded-nsdata-to-nsstring – James Paolantonio Jul 30 '12 at 21:24
  • You can actually use `operation.responseString` to get the response as a string. – Aaron Brager Mar 20 '15 at 15:46