Objective c 将响应从AFHTTPRequestOperation传递到UIWebview
我的代码向服务器发出两个请求。一个直接进入uiwebview,另一个使用AFHTTPRequestOperation。我想使用AFHTTPRequestOperation并将响应传递到我的uiwebview中。将响应传递到uiwebview并不再加载它的正确语法是什么?如何在不从服务器调用两次的情况下执行此操作?我仍然希望能够测试加载请求的成功或失败,并发送用户名和密码以连接到urlObjective c 将响应从AFHTTPRequestOperation传递到UIWebview,objective-c,xcode,uiwebview,afnetworking,Objective C,Xcode,Uiwebview,Afnetworking,我的代码向服务器发出两个请求。一个直接进入uiwebview,另一个使用AFHTTPRequestOperation。我想使用AFHTTPRequestOperation并将响应传递到我的uiwebview中。将响应传递到uiwebview并不再加载它的正确语法是什么?如何在不从服务器调用两次的情况下执行此操作?我仍然希望能够测试加载请求的成功或失败,并发送用户名和密码以连接到url - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- (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;
}
如果该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");
}];
唯一可能改变的是响应对象。您可能没有得到字符串,但得到了NSURL响应或类似的性质。我建议使用AFNetworking的UIWebView类别,它正好可以做到这一点;使用
AFHTTPRequestOperation
加载网页内容,然后将其作为HTML字符串加载到web视图中
否则,我建议您查看一下该类别,看看您是否可以根据自己的使用调整其代码 我没有一个真正的答案,只是一些想法。。。。作为
responseObject
给块的实际类型是什么?此外,已知客户端在加载之前发送“HEAD”方法请求以验证资源,但我不知道该请求是否支持授权。这导致:-[\u NSCFData datausingencode:]:无法识别的选择器发送到实例0x6894300 2012-07-30 11:18:21.533项\u查找[398:11503]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[\uu NSCFData dataUsingEncoding:]:发送到实例0x6894300的选择器无法识别”我猜这意味着responseObject不仅仅返回HTML。这意味着responseObject是数据而不是字符串。使用以下说明将其转换:您实际上可以使用operation.responseString
以字符串形式获取响应。