Are there any specific methods within UIWebView on iOS that allow for interfacing with the DOM?

When working with Android, there is a method called addJavascriptInterface() that allows you to import an Android JAVA object to the Javascript context. I am wondering if there is an equivalent of this on iOS. I have searched for similar questions but haven't found a satisfactory solution:
androids-addjavascriptinterface-equivalent-in-ios
calling-objective-c-function-from-javascript-in-ios-applications

function initiateAddCall(){
        var locus = reader.getPlace().getLocus();
        RoomHelper.postReaderLocus(JSON.stringify(locus));
}

RoomHelper is the JavaScript handler that I am using. Is there a way to implement something similar in UIWebView on iOS?

Answer №1

I recently utilized EasyJSWebView and found it to be quite effective. There are alternative options such as WebViewBridge, but I found EasyJSWebView to be more user-friendly.

RoomTool *roomTool = [RoomTool new];
[self.myWebView addJavascriptInterfaces:interface WithName:@"RoomTool"];
[self.myWebView injectJS:webView];

When using JavaScript, you would call:

RoomTool.test()

To implement the test method in RoomTool:

RoomTool.h :

@interface RoomTool : NSObject

- (void) test;

@end

Answer №2

Recently, I had a project for a mobile application that required the integration of D3.JS for displaying graphs. To achieve this, I utilized the following code snippet to trigger the JavaScript function:

    NSString *jsString = [NSString stringWithFormat:@"showGraphsFromDevice('%@',%d)",sectionName, position];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];

Within the code above, the essential JavaScript function is defined as follows:

function showGraphsFromDevice(name, position) 

The critical aspect here lies in constructing the correct JavaScript string with the necessary quotations.

Answer №3

One way to incorporate addJavascriptInterface is by using JavascriptCore.

  • Check out this resource for a detailed guide on implementing JavascriptCore
  • For more information about JavascriptCore, visit this link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Please use Shift + Enter feature only when using desktop devices

In my React chat application, I have implemented a textarea for message input to allow multiline support. This works smoothly on mobile devices as pressing Enter creates a new line and a send button is available to submit the message. However, I want a di ...

React: Incorrect state mutations occurring when using setTimeout lead to unexpected outcomes

I have an array of notifications stored in my state. I want to give users the option to dismiss a notification by clicking on it manually or have it automatically disappear after a few seconds. Currently, only the third notification is being cleared auto ...

A guide on looping through a JSON response from Symfony2 with JavaScript

Currently, I am trying to iterate through a JSON response from Symfony and place it within table cells. This is the code for my action: $search = $this->getDoctrine->...; $serializer = $this->get('serializer'); foreach ($se ...

Is there a way to verify if a nested array contains two values that are larger than those in the preceding array within an array of arrays?

I am currently working on this code, but I keep encountering an error. The main objective is to generate, by the end of the program, an array consisting of arrays that fulfill the condition of having both values greater than the values in the preceding a ...

Navigating Angular QueryList through loops

I am currently trying to gather all the images in my component and store them in an array. To achieve this, I am utilizing Angular's @ViewChildren which returns a QueryList of ElementRef: @ViewChildren('img', { read: ElementRef }) images: Q ...

Canvg | Is there a way to customize canvas elements while converting from SVG?

Recently, I encountered an issue with styling SVG graphics dynamically generated from data. The SVG graphic appears like this: https://i.sstatic.net/xvIpE.png To address the problem, I turned to Canvg in hopes of converting the SVG into an image or PDF us ...

SignalR HubConnection in React fails to transmit data to server

Hey there, I'm currently working on an application that is supposed to connect to a SignalR hub and send data when certain buttons are clicked. However, I've encountered an issue where nothing seems to be getting sent. const bannerId = 1; con ...

Revise the reply within ExpressJS

I need help with editing the response to a request in Express. When the request is made via XHR, I want to encapsulate the body inside a JavaScript object. This way, different parts of the page can be accessed individually within the JavaScript object, suc ...

Implementing a personalized cell with a UISearchController within a UITableViewController (search results)

I have encountered a frustrating issue while trying to implement a UISearchController instead of the deprecated UISearchDisplayController. When I attempt to use dequeueResusableCellWithIdentifier with my custom CellAutocompletion (a UITableViewCell subcla ...

The jQuery UI Dialog is experiencing an issue with a button that is triggering a HierarchyRequest

I am facing an issue with a piece of javascript that works perfectly on other pages but is now throwing a HierarchyRequestError on a new page. This leads me to believe that there may be an HTML problem on this particular page. Here is a simplified version ...

What is the best way to ensure that the scroll position on each page in shinydashboard remains unaffected by changes in the scroll position on other pages?

When navigating between pages A and B in my shinydashboard app, I noticed that the scroll position of one page affects the scroll position of the other. How do I make the scrolls independent? To illustrate my issue, I've included a stable shinydashbo ...

Clean up the incoming data to make sure it is formatted correctly for conversion to valid JSON

My mobile app relies on data imported by customers. Unfortunately, they often paste data from Word or other programs, leading to invalid JSON serialization. Oddly, this incorrect data can be serialized in C# and sent to the iOS app without any problems. Th ...

Are queued events in React discarded upon a state change?

As I develop a React form component with simple validation, I encounter an issue where the onBlur event of a field and the onSubmit function of the form are triggered simultaneously. If there is a change in the state during onBlur, the onSubmit function do ...

JavaScript Custom Asynchronous function not functioning as expected

console.log('Begin'); const executeAsync = async () => { for(let i=0; i<10000000000000; i++){} console.log('After loop'); } executeAsync(); console.log('finish'); I am looking for asynchronous behavior using async/a ...

The requested Javascript function could not be found

I have the following JavaScript function that creates a button element with a click event attached to it. function Button(id, url, blockMsg){ var id = id; var url = url; var blockMsg = blockMsg; var message; this.getId = function(){ return id; }; th ...

Create a hover effect on HTML map area using CSS

Is it possible to change the background color of an image map area on hover and click without using any third-party plugins? I attempted the following code: $(document).on('click', '.states', function(){ $(this).css("backgro ...

What are some strategies for implementing dynamic script generation within an AJAX response?

I am exploring a new AJAX design approach where a script is returned to handle and show data. The JSON response below is currently functional, but I would appreciate advice on how to better organize the application for future maintenance. { payload: " ...

Error: Attempting to access the 'getProvider' property of an undefined variable

I am encountering an error that says "property of undefined (reading getProvider)" while deploying my function to Firebase. I am currently trying to figure out how to obtain the auth instance. When I attempt to use firebase.auth, it results in another er ...

Avoiding cross-site cookie warnings when working with PostgreSQL through pgAdmin

Currently, my tech stack includes Python 3.7.4, Django 3.0.6, JavaScript, and Postgres 12.3.1. Upon loading my page, I am encountering the following warnings in the console: Cookie “PGADMIN_KEY” will be soon treated as cross-site cookie against “http ...

Javascript onclick events failing to toggle video play/pause functionality

In my website, I have background music and a background video (webm format) embedded. I am facing an issue with making play/pause buttons in the form of png images work for both the video and the music. The background music is added using the embed tag wi ...