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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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

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

Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Google Pixel 9 smartphones will not launch with Android 15 despite seven-year update commitment Aug 01, 2024 pm 02:56 PM

The Pixel 9 series is almost here, having been scheduled for an August 13 release. Based on recent rumours, the Pixel 9, Pixel 9 Pro and Pixel 9 Pro XL will mirror the Pixel 8 and Pixel 8 Pro (curr. $749 on Amazon) by starting with 128 GB of storage.

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

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

Google\'s new Chromecast \'TV Streamer\' rumoured to launch with Ethernet and Thread connectivity Google\'s new Chromecast \'TV Streamer\' rumoured to launch with Ethernet and Thread connectivity Aug 01, 2024 am 10:21 AM

Google is roughly a fortnight away from fully revealing new hardware. As usual, countless sources have leaked details about new Pixel devices, whether that be the Pixel Watch 3, Pixel Buds Pro 2 or Pixel 9 smartphones. It also seems that the company

Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Aug 23, 2024 am 06:42 AM

Google's new Pixel 9 series has introduced a new variant that wasn't present in previous lineups, the Pixel 9 Pro XL. It is essentially identical to the non-XL variant (pre-order at Amazon), but as the name suggests, it has a larger screen. The two e

See all articles