我的代码向服务器发出两个请求。一种是直接进入 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;
}
我建议使用 AFNetworking 的 UIWebView 类别,它正是这样做的;使用一个AFHTTPRequestOperation
加载网页内容,然后将其作为 HTML 字符串加载到 Web 视图中。
否则,我建议您查看该类别,看看您是否可以调整其代码以供您使用。https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/UIWebView%2BAFNetworking.m https://github.com/AFNetworking/AFNetworking/blob/master/UIKit%2BAFNetworking/UIWebView%2BAFNetworking.m
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)