UIWebView各种加载网页的方式
阅读原文时间:2023年07月09日阅读:1

UIWebView加载网页的方法

最近在使用UIWebView的时候遇到各种不同形式加载网页的方式,总结起来共有三种方式,分别为:使用URL加载,使用HTML源码加载,使用HTML文件加载,各种方法的使用如下:

  • 直接使用URL加载

      NSURL *url = [NSURL URLWithString: @"http://ss.zhiless.com/hjyAuth.do?token=61cbdd8c97273f9aa7d878b04e2c94c1"];
      NSURLRequest *request = [NSURLRequest requestWithURL: url];
      [self.webView loadRequest:request];
  • 加载HTML源码

      [self.testWebView loadHTMLString:@"
      <!doctype html>
      <html>
      <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
      <style>
      h1,
      h2,
      h3,
      h4,
      h5,
      h6,
      p,
      blockquote {
          margin: 0;
          padding: 0;
      }
      body {
          font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif;
          font-size: 13px;
          line-height: 18px;
          color: #737373;
          background-color: white;
          margin: 10px 13px 10px 13px;
      }
      table {
          margin: 10px 0 15px 0;
          border-collapse: collapse;
      }
      td,th {
          border: 1px solid #ddd;
          padding: 3px 10px;
      }
      th {
          padding: 5px 10px;
      }
    
      a {
          color: #0069d6;
      }
      a:hover {
          color: #0050a3;
          text-decoration: none;
      }
      a img {
          border: none;
      }
      p {
          margin-bottom: 9px;
      }
      h1,
      h2,
      h3,
      h4,
      h5,
      h6 {
      color: #404040;
      line-height: 36px;
      }
      h1 {
          margin-bottom: 18px;
          font-size: 30px;
      }
      h2 {
          font-size: 24px;
      }
      h3 {
          font-size: 18px;
      }
      h4 {
          font-size: 16px;
      }
      h5 {
          font-size: 14px;
      }
      h6 {
          font-size: 13px;
      }
      hr {
          margin: 0 0 19px;
          border: 0;
          border-bottom: 1px solid #ccc;
      }
      blockquote {
          padding: 13px 13px 21px 15px;
          margin-bottom: 18px;
          font-family:georgia,serif;
          font-style: italic;
      }
      blockquote:before {
          content:"\201C";
          font-size:40px;
          margin-left:-10px;
          font-family:georgia,serif;
          color:#eee;
      }
      blockquote p {
          font-size: 14px;
          font-weight: 300;
          line-height: 18px;
          margin-bottom: 0;
          font-style: italic;
      }
      code, pre {
          font-family: Monaco, Andale Mono, Courier New, monospace;
      }
      code {
          background-color: #fee9cc;
          color: rgba(0, 0, 0, 0.75);
          padding: 1px 3px;
          font-size: 12px;
          -webkit-border-radius: 3px;
          -moz-border-radius: 3px;
          border-radius: 3px;
      }
      pre {
          display: block;
          padding: 14px;
          margin: 0 0 18px;
          line-height: 16px;
          font-size: 11px;
          border: 1px solid #d9d9d9;
          white-space: pre-wrap;
          word-wrap: break-word;
      }
      pre code {
          background-color: #fff;
          color:#737373;
          font-size: 11px;
          padding: 0;
      }
      sup {
          font-size: 0.83em;
          vertical-align: super;
          line-height: 0;
      }
      * {
          -webkit-print-color-adjust: exact;
      }
      @media screen and (min-width: 914px) {
          body {
          width: 854px;
          margin:10px auto;
          }
      }
      @media print {
          body,code,pre code,h1,h2,h3,h4,h5,h6 {
          color: black;
      }
              table, pre {
              page-break-inside: avoid;
          }
      }
      </style>
      <title>UIWebView加载网页的方法</title>
    
      </head>
      <body>
      <h3>UIWebView加载网页的方法</h3>
    
      <ul>
      <li><p>直接使用URL加载</p>
    
      <pre>  NSURL *url = [NSURL URLWithString: @"http://ss.zhiless.com/hjyAuth.do?token=61cbdd8c97273f9aa7d878b04e2c94c1"];
              NSURLRequest *request = [NSURLRequest requestWithURL: url];
              [self.webView loadRequest:request];
              </pre></li>
      <li><p>加载HTML源码</p>
    
      <pre>  [self.testWebView loadHTMLString:@"" baseURL:nil];
              </pre></li>
      <li></li>
      </ul>
    
      </body>
      </html>" baseURL:nil];
  • 加载HTML文件

      NSString *path = [[NSBundle mainBundle] bundlePath];
      NSURL *baseURL = [NSURL fileURLWithPath:path];
      NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
                                                        ofType:@"html"];
      NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath
                                                  encoding:NSUTF8StringEncoding
                                                     error:nil];
      [self.ui_webView loadHTMLString:htmlCont baseURL:baseURL];

h1, h2, h3, h4, h5, h6, p, blockquote { margin: 0; padding: 0 }
body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif; font-size: 13px; line-height: 18px; color: rgba(115, 115, 115, 1); background-color: rgba(255, 255, 255, 1); margin: 10px 13px }
table { margin: 10px 0 15px; border-collapse: collapse }
td, th { border: 1px solid rgba(221, 221, 221, 1); padding: 3px 10px }
th { padding: 5px 10px }
a { color: rgba(0, 105, 214, 1) }
a:hover { color: rgba(0, 80, 163, 1); text-decoration: none }
a img { border: none }
p { margin-bottom: 9px }
h1, h2, h3, h4, h5, h6 { color: rgba(64, 64, 64, 1); line-height: 36px }
h1 { margin-bottom: 18px; font-size: 30px }
h2 { font-size: 24px }
h3 { font-size: 18px }
h4 { font-size: 16px }
h5 { font-size: 14px }
h6 { font-size: 13px }
hr { margin: 0 0 19px; border-top: 0; border-right: 0; border-bottom: 1px solid rgba(204, 204, 204, 1); border-left: 0 }
blockquote { padding: 13px 13px 21px 15px; margin-bottom: 18px; font-family: georgia, serif; font-style: italic }
blockquote:before { content: "“"; font-size: 40px; margin-left: -10px; font-family: georgia, serif; color: rgba(238, 238, 238, 1) }
blockquote p { font-size: 14px; font-weight: 300; line-height: 18px; margin-bottom: 0; font-style: italic }
code, pre { font-family: Monaco, Andale Mono, Courier New, monospace }
code { background-color: rgba(254, 233, 204, 1); color: rgba(0, 0, 0, 0.75); padding: 1px 3px; font-size: 12px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px }
pre { display: block; padding: 14px; margin: 0 0 18px; line-height: 16px; font-size: 11px; border: 1px solid rgba(217, 217, 217, 1); white-space: pre-wrap; word-wrap: break-word }
pre code { background-color: rgba(255, 255, 255, 1); color: rgba(115, 115, 115, 1); font-size: 11px; padding: 0 }
sup { font-size: 0.83em; vertical-align: super; line-height: 0 }
* { -webkit-print-color-adjust: exact }
@media screen and (min-width: 914px) { body { width: 854px; margin: 10px auto } }
@media print { body, code, pre code, h1, h2, h3, h4, h5, h6 { color: rgba(0, 0, 0, 1) } table, pre { page-break-inside: avoid } }