jjclarkson
jjclarkson

Reputation: 5954

Pass response from AFHTTPRequestOperation to UIWebview

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; 
}

Upvotes: 4

Views: 7378

Answers (2)

Jon Shier
Jon Shier

Reputation: 12770

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

Upvotes: 1

James Paolantonio
James Paolantonio

Reputation: 2194

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.

Upvotes: 0

Related Questions