HarmonyOS Next is integrated with iOS development
Guide for iOS developers migrating to HarmonyOS development
The purpose of this article is to discuss the technical details related to the integration of Huawei's HarmonyOS Next system (API 12 as of now) and iOS development, and summarize it based on actual development practices. Mainly as a carrier of technology sharing and exchange, it is inevitable that mistakes and omissions will be missed, and colleagues are welcome to put forward valuable opinions and questions in order to make progress together. This article is original content, and the source and original author must be indicated for any form of reprinting.
Chapter 1: Analysis of similarities and differences
1. Develop languages and tools
HarmonyOS Next
HarmonyOS Next is developed primarily in the ArkTS language, which is a concise and efficient TypeScript-based programming language. In terms of development tools, DevEco Studio is mainly used, which provides rich functions, such as code editing, debugging, interface design, etc., to facilitate application development.
iOS
iOS development uses either Swift or Objective-C. Swift is a modern, secure, and efficient programming language, while Objective-C is the traditional language for iOS development. The development tool is Xcode, which integrates a series of functions such as code writing, interface building, and testing, and is a powerful tool for iOS development.
2. Architecture and system characteristics
HarmonyOS Next architecture
HarmonyOS Next uses a distributed architecture that emphasizes collaboration between devices, enabling seamless connectivity and resource sharing across devices. Its system service layer provides rich distributed capabilities, such as distributed task scheduling and distributed data management, so that applications can better adapt to multi-device environments.
iOS architecture
iOS has a layered architecture, including a core operating system layer, a core services layer, a media layer, and an application layer. It is characterized by strong closure, high stability and security of the system, but relatively weak in cross-device collaboration.
3. User interface development
HarmonyOS Next UI development
HarmonyOS Next uses ArkUI for UI development, which uses declarative programming to build user interfaces by describing the structure and style of the interface. ArkUI provides a wide range of components and layouts to help developers quickly create beautiful and efficient user interfaces.
iOS UI development
iOS uses Interface Builder or code for UI development. Interface Builder provides a visual interface design tool that allows developers to build interfaces by dragging and dropping components, and also supports fine-grained control of the interface through code.
Fourth, the table of key differences and similarities
|Compare Items| HarmonyOS Next|iOS|
|---|---|---|
|Development Language| ArkTS (based on TypeScript) | Swift/Objective-C|
|Development Tools| DevEco Studio| Xcode|
Architectural features: Distributed architecture, strong cross-device collaboration, hierarchical architecture, strong closedness
| How to develop UI| ArkUI Declarative Programming| Interface Builder Visualization/Code|
|Security mechanism|Multi-level security protection, focusing on distributed security|System closure brings high security|
Application Distribution|HUAWEI AppGallery, etc App Store|
Chapter 2: Convergence Strategies and Methods
1. Functional module division and selection
Divided according to the advantages of the platform
In a convergence project, functional modules can be divided according to the strengths of HarmonyOS Next and iOS. For example, for functions that need to work together across devices, such as smart home control and distributed office, HarmonyOS Next can be developed first. For some features that require high user experience and interface interaction, and only run on a single device, such as music playback and game interfaces, you can use iOS to develop. This allows you to take advantage of the best of both platforms and improve the overall quality of your app.
Code reuse strategy
For some common business logic and algorithms, you can try code reuse. For example, if you use the same backend interface and data format for data processing and network requests, you can encapsulate this part of the code into independent modules and reference them separately in HarmonyOS Next and iOS projects. Code reuse can be aided by cross-platform development frameworks or tools, such as writing core algorithms in C++ and then calling them in a specific way on both platforms.
2. Data interaction and synchronization
The interface design is unified with the data format
In order to realize data exchange and synchronization between HarmonyOS Next and iOS, it is necessary to design a unified interface and data format. For example, you can use a RESTful API as a data interface and JSON as the data transfer format. In this way, both the HarmonyOS Next app and the iOS app can easily interact with the backend server, and be able to understand and process the data sent by the other party.
Code samples
Let's say we have a simple to-do app that needs to sync to-do data between HarmonyOS Next and iOS. Here's an example of a simplified backend API (using the Node.js and Express frameworks):
javascript
Code interpretation
Copy the code
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors());
// 存储待办事项数据的数组
let todoList = [];
// 获取待办事项列表
app.get('/api/todos', (req, res) => {
res.json(todoList);
});
// 添加待办事项
app.post('/api/todos', (req, res) => {
const newTodo = req.body;
todoList.push(newTodo);
res.json(newTodo);
});
// 启动服务器
const port = 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
In HarmonyOS Next, use the module to send network requests to get and add to-dos:http
typescript
Code interpretation
Copy the code
import http from '@ohos.net.http';
// 获取待办事项列表
async function getTodoList() {
let httpRequest = http.createHttp();
let response = await httpRequest.request('http://localhost:3000/api/todos', {
method: http.RequestMethod.GET
});
let todoList = JSON.parse(response.result.toString());
httpRequest.destroy();
return todoList;
}
// 添加待办事项
async function addTodo(todo: any) {
let httpRequest = http.createHttp();
let response = await httpRequest.request('http://localhost:3000/api/todos', { method: http.RequestMethod.POST,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(todo) });
let newTodo = JSON.parse(response.result.toString());
httpRequest.destroy();
return newTodo;
}
In iOS, use Send Network Request:URLSession
swift
Code interpretation
Copy the code
import Foundation
// 获取待办事项列表
func getTodoList(completion: @escaping ([String: Any]) -> Void) { let url = URL(string: "http://localhost:3000/api/todos")! let task = URLSession.shared.dataTask(with: url) { (data, response, error) in if let error = error { print("Error: \(error)") return } if let data = data { do { let todoList = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] completion(todoList?? []) } catch { print("Error parsing JSON: \(error)") } } } task.resume() } // 添加待办事项 func addTodo(todo: [String: Any], completion: @escaping ([String: Any]) -> Void) { let url = URL(string: "http://localhost:3000/api/todos")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") do { request.httpBody = try JSONSerialization.data(withJSONObject: todo, options: []) } catch { print("Error encoding JSON: \(error)") return } let task = URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { print("Error: \(error)") return } if let data = data { do { let newTodo = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] completion(newTodo?? []) } catch { print("Error parsing JSON: \(error)") } } } task.resume() }
As can be seen from the above code examples, HarmonyOS Next and iOS use different methods to interact with backend APIs to obtain and add data, thus achieving data synchronization.
Chapter 3: Convergence Case Sharing
1. Case: Cross-platform social application
Features & Implementation
This cross-platform social app has features like chat, dynamic sharing, friend management, and more. In the implementation process, HarmonyOS Next's distributed task scheduling and data management capabilities were leveraged to ensure that messages could be delivered between different devices in a timely and accurate manner. In terms of dynamic sharing and friend management, iOS's Interface Builder was used to build a beautiful and smooth user interface and provide a good user experience. At the same time, the backend server uses a unified interface and data format to enable data interaction and synchronization between HarmonyOS Next and the iOS client.
Lessons learned and gained
Technology Integration Challenges and Solutions: During the integration process, compatibility issues between different development languages and frameworks were encountered. For example, HarmonyOS Next's ArkUI and iOS's Interface Builder have different implementations when it comes to handling UI layout and data binding, which takes time to understand and coordinate. These issues were finally resolved through in-depth documentation of both technologies, as well as multiple code debugging and optimizations.
Team collaboration and communication: Since there are two different platforms involved in the development team, team collaboration and communication are crucial. A regular communication mechanism has been established, including technical exchange meetings, problem discussion groups, etc., to ensure that the two teams can share information and solve problems in a timely manner. At the same time, a unified code specification and project management process have been formulated to improve development efficiency.
User experience improvements: By combining the advantages of HarmonyOS Next and iOS, apps have been significantly improved in terms of functionality and user experience. Users can seamlessly switch between apps on different devices, enjoy a smooth chat experience and beautiful interface display, and improve user satisfaction and loyalty.
Through this cross-platform social application case, we can see that the convergence of HarmonyOS Next and iOS development is feasible and has potential. In actual projects, developers can flexibly use the advantages of both platforms to create more competitive applications according to their specific needs. At the same time, it is necessary to focus on the challenges in the process of technology integration and strengthen teamwork to achieve the success of the project. We hope that this case will provide some useful references and enlightenment for developers to explore the integration of HarmonyOS Next and iOS development.
作者:SameX
链接:https://juejin.cn/post/7442711650389032975
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。