Pass response from AFHTTPRequestOperation to UIWebview

梦想的初衷 提交于 2019-12-05 16:47:06

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!