【问题标题】:Pass response from AFHTTPRequestOperation to UIWebview将响应从 AFHTTPRequestOperation 传递到 UIWebview
【发布时间】:2012-07-26 18:53:24
【问题描述】:

我的代码向服务器发出两个请求。一个直接进入 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; 
}

【问题讨论】:

  • 我没有真正的答案,只是有几个想法....responseObject 的块是什么实际的东西?此外,已知客户端会在加载之前发送“HEAD”方法请求以验证资源,但我不知道这是否支持授权。

标签: objective-c xcode uiwebview afnetworking


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    类似:

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

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

    【讨论】:

    • 这导致:-[__NSCFData dataUsingEncoding:]:无法识别的选择器发送到实例 0x6894300 2012-07-30 11:18:21.533 item_lookup[398:11503] *** 由于未捕获的异常而终止应用程序'NSInvalidArgumentException', reason: '-[__NSCFData dataUsingEncoding:]: unrecognized selector sent to instance 0x6894300' 我猜这意味着 responseObject 不只是返回 HTML。
    • 这意味着 responseObject 是数据而不是字符串。使用以下说明进行转换:stackoverflow.com/questions/2467844/…
    • 您实际上可以使用operation.responseString 以字符串形式获取响应。
    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    相关资源
    最近更新 更多