iOS interacts with H5

Source: Internet
Author: User

Prerequisite: Load UIWebView in OC Controller, set up agent, abide by Uiwebviewdelegate protocol.

First, the iOS call JS method

Using the iOS call JS code to make it easier to call the UIWebView method directly-(nullable NSString *) stringbyevaluatingjavascriptfromstring: (NSString *) Script

1. Query tags

//Query label
NSString *str = @ "var word = document.getElementById (' word ');"
@ "alert (word.innerhtml)";
[WebView STRINGBYEVALUATINGJAVASCRIPTFROMSTRING:STR];

2. Add tags to your Web page:

nsstring *str = @ "var img = document.createelement (' img ');"
"img.src = ' icon5.jpg ';"
"img.width = +;"
"img.heigth =";
"Document.body.appendChild (IMG);";
[WebView stringbyevaluatingjavascriptfromstring:str];

3. Delete Page Labels:

//Remove tags
nsstring *str1 = @ "var word = document.getElementById (' word ');"
@ "Word.remove ();";
[WebView STRINGBYEVALUATINGJAVASCRIPTFROMSTRING:STR1];

4. Change the Label:

//Change
NSString *str2 = @ "var = document.getelementsbyclassname (' change ') [0];"
"change.innerhtml = ' hello ';";
NSString *result = [WebView stringbyevaluatingjavascriptfromstring:str2];

HTML-side code:

<! DOCTYPE html>


<meta charset= "UTF-8" >
<title>ios and H5 Interactive </title>

<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= "Please enter password" >
<button onclick= "ButtonClick ()" > Submit information </button>
<script type= "Text/javascript" >
alert (' This one box ');
</script>
</body>

Second, JS call iOS method:

1. The first method is relatively simple and is a comparison of strings. This way the iOS side code is relatively simple, after the Web page loading is completed, the background needs to redefine the URL of the page, the parameters required by the mobile side to the URL to return, or in accordance with the background of the field agreed to the string to achieve the purpose of calling the iOS method. Paste the code below.

OC Code: (Requires implementation of WebView protocol)

//Intercept protocol header, adjust system camera
#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]) {//Call album
//instancing Controller
Uiimagepickercontroller *picker = [[Uiimagepickercontroller alloc] init];
picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
picker.delegate = self;
//Do you have a picture selection box
picker.allowsediting = YES;
[self presentviewcontroller:picker animated:yes completion:nil];
        }
    }
HTML-side code:
<! DOCTYPE html>


<meta charset= "UTF-8" >
<title> How to call oc in HTML </title>

<body>
<button onclick= "getImage ()" > Access albums </button>
<script type= "Text/javascript" >
function GetImage () {
window.location.href = "Wxd://getimage";
                  }
</script>
</body>

2. The second method, JS directly using the OC method name to invoke the OC method, similar to the Android. Addjavascriptinterface (New Jsobject (), "Android") method, header file needs to be imported #import < Javascriptcore/javascriptcore.h>.

First create a class that inherits from NSObject, where I name the Jstestobjext,.h code as follows:

Implement the Protocol method in. m, with the following code:

It is then called in the controller that loads WebView:

So far, the OC code has been written, we just tell the JS side using the Testobject class, you can adjust the OC method. The following code is attached to the JS call:

Here's how iOS and H5 interact with each other in this company.

iOS interacts with H5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.