Pass response from AFHTTPRequestOperation to UIWebview

那年仲夏 提交于 2019-12-07 11:05:27

问题


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

回答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




回答2:


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.



来源:https://stackoverflow.com/questions/11676126/pass-response-from-afhttprequestoperation-to-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!