ios 跨域

I was working on a certain project at work, in which I needed to connect several varying components via messages. Each had its own logic and code language. This made me want to understand all the ways different platforms enable communication.

我正在工作的某个项目上,需要通过消息连接几个不同的组件。 每个都有自己的逻辑和代码语言。 这使我想了解不同平台实现通信的所有方式。

This article’s aim is to explain these cross-origin communication bridges and present simple, yet informative, examples to achieve them.

本文的目的是解释这些跨域通信桥梁,并提供实现这些目标的简单但有用的示例。

There will also be plenty of bridge puns ?

还会有很多双关语吗?

YOU WERE WARNED.

您被警告。

If you just want to get your hands dirty with the code, there are links to the GitHub repositories at the bottom of this article.

如果您只是想弄清楚代码,可以在本文底部找到指向GitHub存储库的链接。

Typically, the JavaScript you write will run inside a browser. On iOS, it can either be a UIWebView or a WKWebView. On Android, a WebView.

通常,您编写JavaScript将在浏览器中运行。 在iOS上 , 它可以是UIWebView或WKWebView。 在Android上 ,是WebView。

Since iOS can be the more exasperating of the platforms, I’ll describe the communication bridge there first.

由于iOS可能会使平台更加恼人,因此我将首先在此描述通信桥。

伦敦桥倒塌(iOS) (London Bridge is Falling Down (iOS))

From iOS 8 onwards, Apple recommends using WKWebView instead of UIWebView, so the following will only address the bridge on a WKWebView.

从iOS 8开始,Apple建议使用WKWebView而不是UIWebView,因此以下内容仅解决WKWebView上的

For a UIWebView reference, please go here.

有关UIWebView的参考,请转到此处 。

To send messages from the WKWebView to JavaScript, you use the method below:

要将消息从WKWebView发送到JavaScript,请使用以下方法:

- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *error))completionHandler;

To receive messages from JavaScript inside your WKWebView, you must do the following:

要从WKWebView内部JavaScript接收消息,您必须执行以下操作:

  1. Create an instance of WKWebViewConfiguration

    创建WKWebViewConfiguration的实例

  2. Create an instance of WKUserContentController

    创建WKUserContentController的实例

  3. Add a script message handler to your configuration (this part bridges the gap). This action also registers your message handler on the window object under the following path: window.webkit.messageHandlers.MSG_HANDLER_NAME

    将脚本消息处理程序添加到您的配置中(这部分弥合了差距)。 此操作还将您的消息处理程序注册到以下路径下的window对象上: window.webkit.messageHandlers.MSG_HANDLER_NAME

  4. Make the class implement the message handler protocol by adding <WKScriptMessageHandler> at the top of the file通过在文件顶部添加<WKScriptMessageHandler>,使类实现消息处理程序协议。
  5. Implement userContentController:didReceiveScriptMessage (this method handles receiving the messages from JavaScript)

    实现userContentController:didReceiveScriptMessage (此方法处理从JavaScript接收消息)

建筑桥梁 (Building Bridges)

Let’s say we have the following HTML page set up:

假设我们设置了以下HTML页面:

<html><head><title>Javascript-iOS Communication</title></head><body><script>window.webkit.messageHandlers.myOwnJSHandler.postMessage("Hello World!");</script></body></html>

And in our native code we implement the steps described above:

在我们的本机代码中,我们实现了上述步骤:

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>// 4
@interface ViewController : UIViewController <WKScriptMessageHandler>@property(nonatomic, strong) WKWebView *webview;

And violà! Now you have full JavaScript - iOS Communication!

和提琴! 现在,您已经拥有完整JavaScript-iOS通信!

过桥(Android) (Crossing The Bridge (Android))

Things are much simpler and more friendly here. In order to set up our communication bridge, there are only a few steps:

这里的事情更加简单和友好。 为了建立我们的沟通桥梁,只有几个步骤:

  1. Create an instance of a WebView object

    创建一个WebView对象的实例

  2. Enable JavaScript inside this WebView (setJavaScriptEnabled)

    在此WebView中启用JavaScript( setJavaScriptEnabled )

  3. Set your own JavaScript Interface (which will hold methods that are visible to your JavaScript)设置自己JavaScript接口(将包含对JavaScript可见的方法)
  4. Any method that you want exposed to your JavaScript must have the @JavascriptInterfaceannotation before its declaration

    您想要公开给JavaScript的任何方法都必须具有@JavascriptInterface 注解 在宣布之前

Like before, let’s assume we have created this HTML file:

像以前一样,假设我们已经创建了这个HTML文件:

And we have created the following simple Android Application:

我们创建了以下简单的Android应用程序:

And there you go!

然后你去了!

You can now consider yourself a Native Communication Ninja!

您现在可以认为自己是本地交流忍者!

Here are the links to the repositories:

以下是存储库的链接:

AndroidtoJS RepositoryAndroidtoJS存储库 iOStoJS RepositoryiOStoJS存储库

iOS关于iOS的重要说明⚠️ (⚠️ Important Note Regarding iOS ⚠️)

When you get to the point that you want to destroy your WKWebView, it is imperativethat you remove your script message handler. If you do not do so, the script message handler will still hold a reference to your WKWebView and memory leaks will ensue upon creating new WKWebViews.

当您到达要销毁WKWebView的地步时,它就是 势在必行 您删除脚本消息处理程序。 如果不这样做,脚本消息处理程序将仍然保留对WKWebView的引用,并且在创建新的WKWebViews时将发生内存泄漏。

翻译自: https://www.freecodecamp.org/news/how-to-build-cross-origin-communication-bridges-in-ios-and-andriod-7baef82b3f02/

ios 跨域

ios 跨域_如何在iOS和Android中建立跨域通信桥相关推荐

  1. ios 动态化视图_如何在iOS应用中使高度收集视图动态化

    ios 动态化视图 by Payal Gupta 通过Payal Gupta 如何在iOS应用中使集合视图的高度动态化 (How to make height of collection views ...

  2. ios 启用 证书_如何在iOS 10中启用就寝提醒,轻柔的唤醒和睡眠跟踪

    ios 启用 证书 If you have trouble regularly getting a full night's sleep, the new Bedtime feature in iOS ...

  3. ios手写_如何在iOS 10中发送手写和数字触摸消息

    ios手写 We live in a very high-tech world. We walk around with our heads bowed down to our phones tapp ...

  4. ios 按钮下面加下划线_如何在iOS按钮上的文字添加下划线

    在开发ios的时候很多用户们都会想如何在iOS按钮上的文字添加下划线,那么接下来的内容中我们就一起去看看在iOS按钮上的文字添加下划线的方法. 问题:实现下图中右侧的按钮文字效果 方法: [MyToo ...

  5. 苹果隐藏应用_如何在iOS 14上隐藏应用页?苹果手机上管理应用更方便

    Apple的新iOS 14带来了一种新方法,可以帮助您整理iPhone上数量不断增长的应用程序.这是一种隐藏几乎从未或从未使用过的应用程序的方法. 即使您设法将iPhone保持在一个或两个应用程序的屏 ...

  6. ios禁用更新_如何为iOS应用启用或禁用自动更新

    ios禁用更新 Automatic downloads in iOS allow apps, as well as other items, to be updated automatically. ...

  7. 转移到ios下载安卓_转移到ios下载-转移到IOS 安卓版v2.10.0-PC6安卓网

    转移到ios官方版也叫move to ios,是一款连接安卓与苹果系统的工具.转移到ios相当于安卓系统与苹果系统之间的桥梁,实现两者之间的文件.音乐.视频等资源的传输! 软件介绍 Move to i ...

  8. 转移到ios下载安卓_转移到iOS下载_转移到iOS怎么用_转移到iOS安卓版下载_转移到iOS app_易玩网...

    <转移到iOS>是一款实用的安卓数据移动软件,让你快捷的进行掌上数据导入和转移,便捷的进行数据备份,让你更好地进行实用的换机服务,还等什么?快来网体验下载吧. <转移到iOS> ...

  9. ios激活锁_如何检查iOS设备的激活锁状态

    ios激活锁 Starting with iOS 7, Apple introduced a feature named Activation Lock for iOS devices. When y ...

最新文章

  1. 用TCP/IP进行网际互联一
  2. windows server 2003 域控制器重命名
  3. ExtJS4.2学习 php版(八)
  4. 一张照片攻破人脸识别系统:能点头摇头张嘴
  5. msvcprt.lib(MSVCP90.dll) : error LNK2005:已经在libcpmtd.lib(xmutex.obj) 中定义
  6. leveldb原理和使用
  7. 003_Servlet生命周期
  8. 网络编程(part4)--刷新缓冲区
  9. C语言调试技巧:stdin,stdout,stderr
  10. Win10搭建python3环境
  11. php ajax跨域cookies,解决ajax跨域请求数据cookie丢失问题
  12. (1) python 将numpy数组导出excel
  13. 编程基本功:代码都写不好,还写什么注释
  14. 美术☀️PR去掉视频黑边、旋转视频、减小视频体积、设置视频封面
  15. leetcode#246 中心对称数
  16. 线性代数基础知识点回顾与总结(一):行列式与矩阵
  17. centos7挂载nas存储_CentOS 7配置NAS(网络共享存储)
  18. Photoshop设计中用羽化消除边缘锯齿
  19. 朴素贝叶斯(naive bayes)原理小结
  20. Android开发——新建库、删除库

热门文章

  1. Java面试必备的集合源码详解,砥砺前行!
  2. 终于有人把安卓程序员必学知识点全整理出来了,BAT大厂面试总结
  3. java实现k-means算法(用的鸢尾花iris的数据集,从mysq数据库中读取数据)
  4. 用php生成HTML文件的类
  5. js进阶 12-5 jquery中表单事件如何使用
  6. springmvc ajax 页面无法重定向问题!!!!
  7. unity如何让canvas总是显示在所有层的最上方?
  8. Spring Cloud 学习 (五) Zuul
  9. 【362】python 正则表达式
  10. 「hadoop」cdh5.12离线安装(未完成)