Table of Contents
回复内容:
Home Web Front-end H5 Tutorial 微信如何实现向浏览器注入JS API,并且调用方式就像浏览器原生API一样?

微信如何实现向浏览器注入JS API,并且调用方式就像浏览器原生API一样?

Jun 07, 2016 am 08:44 AM
api google ios

首先,我是一名前端。最近公司在试验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的通信
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Google Pixel 9 Pro XL gets tested with desktop mode Google Pixel 9 Pro XL gets tested with desktop mode Aug 29, 2024 pm 01:09 PM

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 Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Aug 24, 2024 am 06:43 AM

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 | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Update | Hacker explains how to install Epic Games Store and Fortnite on iPad outside the EU Aug 18, 2024 am 06:34 AM

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

New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative New Google Pixel desktop mode showcased in fresh video as possible Motorola Ready For and Samsung DeX alternative Aug 08, 2024 pm 03:05 PM

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 opens AI Test Kitchen & Imagen 3 to most users Google opens AI Test Kitchen & Imagen 3 to most users Sep 12, 2024 pm 12:17 PM

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

Pixel 9 Pro XL vs iPhone 15 Pro Max camera comparison reveals surprising Google wins in video and zoom performance Pixel 9 Pro XL vs iPhone 15 Pro Max camera comparison reveals surprising Google wins in video and zoom performance Aug 24, 2024 pm 12:32 PM

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

Apple iOS / iPadOS 18 developer preview version Beta 7 released Apple iOS / iPadOS 18 developer preview version Beta 7 released Aug 21, 2024 am 07:41 AM

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.

Google confirms Pixel 9 Pro Fold incompatibility with official wireless charging accessories thanks to curious design Google confirms Pixel 9 Pro Fold incompatibility with official wireless charging accessories thanks to curious design Sep 01, 2024 am 06:31 AM

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

See all articles