The communication between WKWebView JavaScript and Objective-C involves passing messages back and forth to exchange information

Exploring the functionality of WKWebView: Sending a message from JavaScript to Objective-C and receiving a return value.

For example:

let result = window.webkit.messageHandlers.name.postMessage();

Answer №1

When adding a script message handler with a specific name, it will create the JavaScript function window.webkit.messageHandlers.name.postMessage(messageBody) across all frames within web views utilizing the user content controller.

Visit here for more information.

Answer №2

After some experimenting, I discovered an alternative method by passing the WKUserScriptInjectionTimeAtDocumentStart parameters into JavaScript to access the web's localStorage.

NSString *token = [NSString stringWithFormat:@"localStorage.setItem(\"accessToken\",'%@');",@"74851c23358c"];

WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:sendToken injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init()];

[config.userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"iOS"];

[config.userContentController addUserScript:wkUScript];

WKWebView *webview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) configuration:config);

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

Does an invisible property value exist?

Instead of: if ( ! $('#XX').is(':visible) ) Is there a property named invisible? I tested that one out, but it seems to not work. Appreciate the help! ...

Reducing an array group using index in JavaScript: A beginner's guide

Do you have coding questions? Check out this sample array group: myArray = { tab1 : [], tab2 : [], tab3 : [], tab4 : [] } I'm looking to always retain the first tab (tab1) and an additional tab based on an index (ranging from 2 to 4) For instance, ...

Tips for building a diverse array of data types and effectively utilizing them based on their specific type in Typescript

Trying to store both custom types, Graphic and Asset, in the same array is proving to be a challenge. The goal is to access them and retain their individual type information. const trail: Array<Graphic | Asset> = []; for (let index = 0; index < t ...

Ways of extracting specific information from a JSON file with the help of jQuery

I am currently attempting to parse a JSON file that is stored locally on my system using jQuery. I am specifically interested in retrieving certain data from the file, which is structured like this: {"statements":[{"subject":{"uriString":"A","localNameIdx ...

Looping through data in Vue.js with a specific condition

There is an object structured like this: groupedContacts: { 4: [ {name: 'foo'}, {name: 'bar'} ], 6: [ {name: 'foo bar'}, ] } Additionally, we have another array: companyList.models: models: [ ...

Continuous Div Carousel with Looping in jQuery

I have developed a basic carousel to cycle through a series of divs, but I am facing some issues. I want the carousel to loop continuously so that after reaching "slide 07," it goes back to "slide 01." Unlike using a carousel plugin, I prefer having an ove ...

How can I ensure that all row checkboxes are automatically marked as checked upon loading the Material Table in ReactJS?

I'm currently working on a project using React Material Table and I need to have the selection option pre-checked by default. Is there a way to accomplish this? function BasicSelection() { return ( <MaterialTable title="Basic Selec ...

What could be causing the events of the confirm dialog buttons in Telerik's RadWindow to not fire?

Due to privacy concerns, I am unable to provide specific details about this issue. The confirm dialog feature is functioning flawlessly for the majority of users, except for one individual who is accessing the same environment using identical Internet Expl ...

Troubleshooting: AngularJS not displaying $scope variables

I have a question that has already been answered, but the suggested solutions did not work for me. Everything seems to be fine, but the content within curly brackets is not displaying on screen. <div ng-controller="Hello"> <p>The I ...

Modify background color of a div depending on the text within the div

At this website, I have a unique "status tag" (such as Diesel, Auto, Manual, etc) displayed on each inventory picture. I am looking to dynamically change the background color of the tag based on the text within the DIV element. Below is the code snippet t ...

Default setting to display modal on page load and prevent it from closing

I've written a JavaScript script to open a Modal on page load using .modal('show'). I also want this Modal to stay open and not close when clicked. I found online that adding .modal({ backdrop: 'static', keyboard: false }) should a ...

Form fields generated dynamically by Rails using fields_for method may not be recognized as inputs when using form_tag

My goal is to generate multiple objects within a single form, without any parent-child relationship involved. All the objects are identical. Currently, everything works smoothly when the code is structured like this: <%= form_tag create_object_path, i ...

Tips for boosting the efficiency of replaceWith on Internet Explorer 11

When rendering an array of data in a table element, there is a need for the table to be updated with new data as it can be filtered dynamically. However, I have encountered a performance issue specifically on IE11 when trying to render 1700 data entries us ...

Transform stereo sound to mono using JavaScript

Recently, I encountered an audio file in stereo with a .raw extension that needs to be converted into mono using Node. Despite my efforts, I haven't been successful in finding examples or libraries outlining the process. Any assistance on this matter ...

Transform the post data into a JSON string within the controller

Hello everyone, I have a sample table that I want to share: <table class="table table-bordered" width="100%" cellspacing="0" id="tableID"> <thead> <tr> <th>A</th> <th>B</th> <th>C< ...

Turn off all VuetifyJS transitions

Is there a way to completely turn off all transitions, such as the slide-x-transition or dialog modal scale transition, in VuetifyJS? ...

Is there a way to add another item to a repeated DOM element without having to modify the ng-repeat scope?

I am working with a list of items using ng-repeat <div layout="column" class="md-whiteframe-1dp" ng-repeat="item in child.items track by item._id" id={{item._id}}child> </div> I am looking to add an additional DOM ele ...

How can I use regular expressions to locate a React JSX element that contains a specific attribute?

Currently conducting an audit within a vast codebase, my task involves searching for all instances of a component where it is utilized with a specific prop. I believe that using regex could prove beneficial in this situation; however, the challenge lies in ...

Why are the sounds not working in my iOS application?

I am having issues with my app's sound functionality. I have defined 3 distinct sounds in my ViewDidLoad method: AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"2357_02" ofType:@"m4a"]], & ...

JS limits browsers to downloading only 10 images simultaneously

Is there a way to increase the number of simultaneous downloads beyond the current limit of 10 in the code below? transferFiles(){ this.checkMark = true let i = 0 this.finalImages.forEach((image) =>{ i++ saveAs(image, 'imag ...