将响应从AFHTTPRequestOperation传递到UIWebview [英] Pass response from AFHTTPRequestOperation to UIWebview

查看:84
本文介绍了将响应从AFHTTPRequestOperation传递到UIWebview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码向服务器发出了两个请求。一种直接进入uiwebview,另一种使用AFHTTPRequestOperation。我想使用AFHTTPRequestOperation并将响应传递到我的uiwebview中。将响应传递到uiwebview而不再次加载的正确语法是什么?如何在不从服务器调用两次的情况下执行此操作?我仍然希望能够测试加载请求的成功或失败,并发送用户名和密码以连接到url。

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


推荐答案

我建议使用AFNetworking的UIWebView类别正是这样做的;使用 AFHTTPRequestOperation 加载网页的内容,然后将其作为HTML字符串加载到Web视图中。

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.

否则,建议您查看类别,看看是否可以调整其代码以供使用。 https://github.com/AFNetworking/AFNetworking/blob /master/UIKit%2BAFNetworking/UIWebView%2BAFNetworking.m

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

这篇关于将响应从AFHTTPRequestOperation传递到UIWebview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆