android - 在 iOS 和 Android 之间共享代码的最佳方式

标签 android c++ ios azure cross-platform

我为 iOS 和 Android 开发了一个应用程序,并且正在寻找在这两个平台之间共享代码的最佳方式。

我想做的是在 native 中创建所有 View (UI 部分),但共享逻辑代码( Controller + 模型)。

根据我发现的所有内容,有 3 件事似乎相当不错:

1) C++ --> 使用 C++ 构建库文件作为逻辑,以便我能够在 2 个平台中使用 .dll 文件

2) Azure 移动应用服务。是否可以将所有逻辑都放在一个 Web 服务中?问题是,如果我无法访问互联网,我的应用程序将无法使用,对吧?

3)我听说了很多关于 Facebook 使用 React Native 的消息,但它似乎是用来创建 UI 的,但我从来不使用 Native 创建它。我可以仅将 React 用于逻辑吗?

最佳答案

您似乎有三个选择:

1。 C++

<小时/>

您不能只拥有一个已编译的 .dll 并期望它适用于 iOS 和 Android。它们都必须在不同的架构中编译,并且必须是 iOS 上的静态库。

Dropbox 就是这样做的,他们提供了大量注释和示例代码,以及您可以使用的代码 can take a look.

优点

• 成功设置后非常简单
• 没有额外的依赖层、错误等(例如 Xamarin/React Native)

缺点

• 设置和使用它需要大量额外的工作:您需要设置额外的编译步骤并为两个平台编写包装器。
• 尝试为两种不同的架构编译相同的代码时,您肯定会遇到一些其他挑战

Here's a SO post on how to do it in detail...

2。 Xamarin

<小时/>

在这种情况下使用这个选项似乎有点极端。您被迫使用 C# 并引入另一层依赖关系和错误。你说你不想使用另一种语言作为 UI,所以我不推荐它。

3。 react native

<小时/>

现在这是一个可行的选择。您可以使用 JS 编写脚本,并在 Android 和 iOS 的 native 代码中使用它们。

Here's an article on how to share code with code examples...

不幸的是,它使用 React Native 作为 UI,但你可以 easily call React Native functions from native code.

使用它有很多缺点,包括调用是异步的,因为它们是在另一个线程上执行的,因此您必须为返回某些内容的函数实现某种回调系统。

优点

• 似乎很容易设置和编写

缺点

• 您必须为每个返回内容的函数实现 native 回调
• 使用它有很多缺点,如文档所述:

• As events can be sent from anywhere, they can introduce spaghetti-style dependencies into your project.

• Events share namespace, which means that you may encounter some name collisions. Collisions will not be detected statically, what makes them hard to debug.

• If you use several instances of the same React Native component and you want to distinguish them from the perspective of your event, you'll likely need to introduce some kind of identifiers and pass them along with events (you can use the native view's reactTag as an identifier).

结论

<小时/>

我想我会选择 C++,主要是因为一家大公司 (Dropbox) 尝试过它并取得了成功,并在生产中实际使用它。您可以尝试使用 React Native 作为实验,这将是一个很好的研究案例!

关于android - 在 iOS 和 Android 之间共享代码的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37047483/

相关文章:

android - 获取 TextView ID 作为字符串

android - 如何在 EditText 中添加可点击的文本

c++ - setf(ios::left, ios::adjustfield) 的组合有什么作用?

ios - UITableView 索引路径行为奇怪

ios - 从表中查找空值并将其替换为空格-sqlite

android - 让宽网页适合移动设备的宽度

android - 如何更改 EditText 光标高度?

c++ - 析构函数中的链表 RAII 代码崩溃

c++ - "D3DX11CreateShaderResourceViewFromFile"让它工作或寻找替代品

ios - Xcode 5 崩溃——Xcode 意外退出