xcode 将响应从 AFHTTPRequestOperation 传递到 UIWebview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11676126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:02:22  来源:igfitidea点击:

Pass response from AFHTTPRequestOperation to UIWebview

objective-cxcodeuiwebviewafnetworking

提问by jjclarkson

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.

我的代码向服务器发出两个请求。一种是直接进入uiwebview,一种是用AFHTTPRequestOperation。我想使用 AFHTTPRequestOperation 并将响应传递到我的 uiwebview 中。将响应传递到 uiwebview 并且不再加载它的正确语法是什么?如何在不从服务器调用两次的情况下做到这一点?我仍然希望能够测试加载请求的成功或失败,并发送用户名和密码以连接到 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; 
}

回答by Jon Shier

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

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

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

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

回答by James Paolantonio

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

如果该 URL 返回 HTML,则可以使用 UIWebView 的 - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL 方法。

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.

唯一可能改变的是响应对象。你可能没有得到一个字符串,而是一个 NSURLResponse 或类似的东西。