zoukankan      html  css  js  c++  java
  • 简单的webView的使用

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

    }

  • 相关阅读:
    14个你可能不知道的JavaScript调试技巧
    数据库设计四步骤
    mac 卸载 jdk
    node版本管理
    mysql order by limit 问题
    计算机一些基本概念的认识
    SQL设置主外键关联时报错
    阻止表单autocomplete
    常见字符编码
    编程语言分类
  • 原文地址:https://www.cnblogs.com/hsxblog/p/5117439.html
Copyright © 2011-2022 走看看