码迷,mamicode.com
首页 > Web开发 > 详细

简单的webView的使用

时间:2016-01-09 22:55:55      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

@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];

}

 

简单的webView的使用

标签:

原文地址:http://www.cnblogs.com/hsxblog/p/5117439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!