@interface ViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建WebView
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:self.webView];
//webView加载请求
// [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
//
// [webView loadHTMLString:<#(NSString *)#> baseURL:<#(NSURL *)#>]
[self getDataWithURL:@"https://moment.douban.com/api/stream/date/2015-10-11?alt=json&apikey=0bcf52793711959c236df76ba534c0d4&app_version=1.7.0&douban_udid=144e7e4c418d047b06ba92d569afcf1a4d05aa11&format=full&udid=27a3ce9559b7d68ff28dbe0836676286f048e565&version=6"];
self.webView.delegate = self;
}
//进行网络请求
- (void)getDataWithURL:(NSString *)urlStr {
//创建session
NSURLSession *session = [NSURLSession sharedSession];
//创建请求任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (data) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
NSLog(@"%@", obj);
//获取第一个字典内的Content
NSString *firstContent = [obj[@"posts"] firstObject][@"content"];
//字符串替换
firstContent = [firstContent stringByReplacingOccurrencesOfString:@"<img class=\"auth_author_mark\"/>" withString:@"<img src=\"https://img1.doubanio.com/icon/u55362758-4.jpg\">"];
[self.webView loadHTMLString:firstContent baseURL:nil];
}
}];
//开启任务
[task resume];
}
原文:http://www.cnblogs.com/hsxblog/p/5117439.html