iOS與H5互動

來源:互聯網
上載者:User

標籤:

前提:在oc控制器中載入UIWebView,設定代理,遵守UIWebViewDelegate協議。

   

一、iOS調用JS方法

   通過iOS調用JS代碼實現起來比較方便直接調用UIWebView的方法- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

   1.查詢標籤

      // 查詢標籤
      NSString *str = @"var word = document.getElementById(‘word‘);"
                             @"alert(word.innerHTML)";
      [webView stringByEvaluatingJavaScriptFromString:str];

   2.為網頁添加標籤:

      NSString *str = @"var img = document.createElement(‘img‘);"
                      "img.src = ‘icon5.jpg‘;"
                      "img.width = 300;"
                      "img.heigth = 100;"
                      "document.body.appendChild(img);";
     [webView stringByEvaluatingJavaScriptFromString:str];

   3.刪除網頁標籤:

      // 刪除標籤
      NSString *str1 = @"var word = document.getElementById(‘word‘);"
                                @"word.remove();";
      [webView stringByEvaluatingJavaScriptFromString:str1];

   4.更改標籤:

      // 更改
      NSString *str2 = @"var change = document.getElementsByClassName(‘change‘)[0];"
                                "change.innerHTML = ‘hello‘;";
      NSString *result =  [webView stringByEvaluatingJavaScriptFromString:str2];

 

   HTML端代碼:

     <!DOCTYPE html>
     <html lang="en">
     <head>
            <meta charset="UTF-8">
            <title>iOS和H5互動</title>
     </head>
     <body>
            <p id="word">6666666666</p>
            <ul>
                 <li class="change">111111</li>
                 <li class="haha">222222</li>
                 <li>333333</li>
                 <li>444444</li>
            </ul>
            <input class="name" placeholder="請輸入密碼">
            <button onclick="buttonClick()">提交資訊</button>
    <script type="text/javascript">
            alert(‘這個一個彈框‘);
    </script>
    </body>
    </html>

二、JS調用iOS方法:

   1.第一種方法比較簡單,通過字串的比對。這種方式iOS端代碼比較簡單,網頁載入完成後後台需要重新定義網頁url,將移動端需要的參數拼接到url上返回,或者按照和後台約定好的欄位來進行字串比對以達到調用iOS方法的目的。下面貼代碼。

     oc代碼:(需要實現webView的協議)

     // 攔截協議頭,調取系統網路攝影機
     #pragma mark UIWebViewDelegate
     - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:            (UIWebViewNavigationType)navigationType
    {
        NSString *str = request.URL.absoluteString;
        if ([str containsString:@"wxd://"]) {
             [self getImage];
         }
        return YES;
     }

    - (void)getImage
   {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { //調用相簿
            //執行個體化控制器
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.delegate = self;
            // 是否有圖片選取框
            picker.allowsEditing = YES;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }
   HTML端代碼:
   <!DOCTYPE html>
   <html lang="en">
          <head>
          <meta charset="UTF-8">
          <title>在html中調用oc的方法</title>
          </head>
          <body>
                  <button onclick="getImage()">訪問相簿</button>
          <script type="text/javascript">
                  function getImage(){
                        window.location.href = "wxd://getImage";
                  }
          </script>
          </body>
   </html>

   2.第二種方法,JS直接用oc方法名來調用oc方法,類似於安卓.addJavascriptInterface(new JsObject(), "Android")方法,標頭檔需要匯入#import <JavaScriptCore/JavaScriptCore.h>。

      首先建立一個繼承自NSObject的類,在這裡我命名為JSTestObjext,.h代碼如下:

     .m中實現協議方法,代碼如下:

  之後在載入webView的控制器中調用:

到此為止,oc代碼就已經寫完了,我們只需告訴JS端使用testobject類,就可以調oc的方法了。下面附上JS調用的代碼:

到此公司裡所使用的iOS與H5互動的方法就都寫在這裡了。

 

iOS與H5互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.