微信如何实现向浏览器注入JS API,并且调用方式就像浏览器原生API一样?
首先,我是一名前端。最近公司在试验iOS与H5混合开发,那么肯定少不了js与OC的通信。之前了解到微信浏览器内置了大量JS API来调用原生功能,我猜测应该是通过OC注入的。并且这些API是可以执行js回调的,灵活度很高,不过接口好像都是异步的。最近我在google如何实现,发现以前是有一种OC通过拦截页面请求的方式,让js调用OC接口,但似乎有很多局限性。请问有更好的解决办法吗?iOS7 开放的JavascriptCore框架,可以在此应用吗?
回复内容:
泻药请阅读资料:
JavaScriptCore
当然可以在这里用。应该说JavaScriptCore就是用在这里的。
当然,你也可以看看ReactNative是怎么把Objective-C和JavaScript联合起来的。 泻药
偶没整过微信和端开发
估摸着是这样的
你姑且看看好了
在高局限性的ios上
肯定是通过OC通过拦截页面请求的方式,让js调用OC接口来实现的
只不过 jsb (jsBridge)是在页面插入个 iframe
然后根据传入的 action 字符串名字构建出特殊的 scheme 给 iframe
然后 OC 监听这个 frame 的 url change 事件来截获 scheme
并分析其字符串
调用指定 OC 方法
由于监听事件后才能继续处理
所以jsb 相关 api 设计都是异步的。
方法运行完成后
根据全局内注册的hash(用来与回调对应)来找出该执行哪个回调
用webview api 内具有 execute script 功能的 api 拼接出js代码字符串来执行,
比如 :
webview.frame.get(0).execscript("window.jsb.getCallback('" + actionname +', '" + param+ "' )")
当然偶写的不是OC语法的玩意
凑合看看吧。
反正偶觉得在功能提供有限的移动端webview上也就这样了,没更好的方法。
(要是 PC 端的QT之类,才不用这么麻烦呢,只要是C++静态函数(方法)
直接就能挂在webvie内指定的js对象(包括全局对象)上
然后js就能直接调用了,还是同步的) oc有个 webview.stringByEvaluatingJavaScriptFromString 来通过字符串执行js,注入js就是执行代码,比如接口声明,windows.wxBridge=function 之类的就行了
真正通信是莫大的答案里,拦截网络通信方式 js bridge 可以去唐巧的博客上逛逛,主要看js和oc的通信

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Google has introduced DisplayPort Alternate Mode with the Pixel 8 series, and it's present on the newly launched Pixel 9 lineup. While it's mainly there to let you mirror the smartphone display with a connected screen, you can also use it for desktop

Google recently responded to the performance concerns about the Tensor G4 of the Pixel 9 line. The company said that the SoC wasn't designed to beat benchmarks. Instead, the team focused on making it perform well in the areas where Google wants the c

Update: Saunders Tech has uploaded a tutorial to his YouTube channel (video embedded below) explaining how to install Fortnite and the Epic Games Store on an iPad outside the EU. However, not only does the process require specific beta versions of iO

A few months have passed since Android Authority demonstrated a new Android desktop mode that Google had hidden away within Android 14 QPR3 Beta 2.1. Arriving hot on the heels of Google adding DisplayPort Alt Mode support for the Pixel 8 and Pixel 8

Google's AI Test Kitchen, which includes a suite of AI design tools for users to play with, has now opened up to users in well over 100 countries worldwide. This move marks the first time that many around the world will be able to use Imagen 3, Googl

The Google Pixel 9 Pro and Pro XL are Google's answers to the likes of the Samsung Galaxy S24 Ultra and the Apple iPhone 15 Pro and Pro Max. Daniel Sin on YouTube(watch below) has compared the Google Pixel 9 Pro XL to the iPhone 15 Pro Max with some

Thanks to netizens Yaochi Qinglian, DREAMM_, and Ji Ren e for submitting clues! [Click here to go directly to the upgrade tutorial] According to news on August 21, Apple today pushed the iOS/iPadOS18 developer preview version Beta7 update (internal version number: 22A5346a) to iPhone and iPad users. This update is separated from the last Beta/RC release. 8 days. Apple's Beta5 and Beta6 updates tend to be stable and do not introduce significant new features (mainly adjusting the details of the control center). Apple focuses on fixing bugs and improving the experience in preparation for the official release in September. Therefore, the Beta7 update released by Apple today should theoretically be the last Beta version update of iOS/iPadOS18.

The Pixel 9 Pro Fold is akin to a volte-face for Google's nascent foldable division. In fact, such are the differences between thePixel 9 Pro Foldand the Pixel Fold that the former has been compared more against the OnePlus Open (curr. $1,399.99 on A
