|
16 | 16 |
|
17 | 17 | - MacOS 12 或更高版本 |
18 | 18 | - React-Native 0.66 或更高版本 |
19 | | -- NodeJs 16.18 或更高版本 |
| 19 | +- NodeJs 18 或更高版本 |
20 | 20 |
|
21 | 21 | 对于 iOS 应用: |
22 | 22 |
|
|
40 | 40 | 2. 打开终端,进入需要创建项目的目录,输入命令创建 React Native 项目: |
41 | 41 |
|
42 | 42 | ```sh |
43 | | -npx @react-native-community/cli init --skip-install simple_demo |
44 | | -cd simple_demo |
| 43 | +npx @react-native-community/cli init --skip-install --version 0.76 quick_start_demo |
| 44 | +cd quick_start_demo |
| 45 | +yarn set version 1.22.19 |
45 | 46 | yarn |
46 | 47 | ``` |
47 | 48 |
|
48 | | -创建的项目名称为 `simple_demo`。 |
| 49 | +创建的项目名称为 `quick_start_demo`。 |
49 | 50 |
|
50 | 51 | 3. 在终端命令行,输入以下命令添加依赖: |
51 | 52 |
|
@@ -95,7 +96,7 @@ cd ios && pod install && cd .. |
95 | 96 |
|
96 | 97 | ## 实现发送和接收单聊消息 |
97 | 98 |
|
98 | | -建议使用 `visual studio code` 打开文件夹 `simple_demo`,打开文件 `App.js`,删除全部内容,并添加如下内容: |
| 99 | +建议使用 `visual studio code` 打开文件夹 `quick_start_demo`,打开文件 `App.js`,删除全部内容,并添加如下内容: |
99 | 100 |
|
100 | 101 | ```javascript |
101 | 102 | // 导入依赖包。 |
@@ -292,99 +293,164 @@ const App = () => { |
292 | 293 | // UI 渲染。 |
293 | 294 | return ( |
294 | 295 | <SafeAreaView> |
295 | | - <View> |
296 | | - <Text>{title}</Text> |
| 296 | + <View style={styles.titleContainer}> |
| 297 | + <Text style={styles.title}>{title}</Text> |
297 | 298 | </View> |
298 | 299 | <ScrollView> |
299 | | - <View> |
| 300 | + <View style={styles.inputCon}> |
300 | 301 | <TextInput |
301 | 302 | multiline |
| 303 | + style={styles.inputBox} |
302 | 304 | placeholder="Enter username" |
303 | 305 | onChangeText={(text) => setUsername(text)} |
304 | 306 | value={username} |
305 | 307 | /> |
306 | 308 | </View> |
307 | | - <View> |
| 309 | + <View style={styles.inputCon}> |
308 | 310 | <TextInput |
309 | 311 | multiline |
| 312 | + style={styles.inputBox} |
310 | 313 | placeholder="Enter chatToken" |
311 | 314 | onChangeText={(text) => setChatToken(text)} |
312 | 315 | value={chatToken} |
313 | 316 | /> |
314 | 317 | </View> |
315 | | - <View> |
316 | | - <Text onPress={login}>SIGN IN</Text> |
317 | | - <Text onPress={logout}>SIGN OUT</Text> |
| 318 | + <View style={styles.buttonCon}> |
| 319 | + <Text style={styles.eachBtn} onPress={login}> |
| 320 | + SIGN IN |
| 321 | + </Text> |
| 322 | + <Text style={styles.eachBtn} onPress={logout}> |
| 323 | + SIGN OUT |
| 324 | + </Text> |
318 | 325 | </View> |
319 | | - <View> |
| 326 | + <View style={styles.inputCon}> |
320 | 327 | <TextInput |
321 | 328 | multiline |
| 329 | + style={styles.inputBox} |
322 | 330 | placeholder="Enter the username you want to send" |
323 | 331 | onChangeText={(text) => setTargetId(text)} |
324 | 332 | value={targetId} |
325 | 333 | /> |
326 | 334 | </View> |
327 | | - <View> |
| 335 | + <View style={styles.inputCon}> |
328 | 336 | <TextInput |
329 | 337 | multiline |
| 338 | + style={styles.inputBox} |
330 | 339 | placeholder="Enter content" |
331 | 340 | onChangeText={(text) => setContent(text)} |
332 | 341 | value={content} |
333 | 342 | /> |
334 | 343 | </View> |
| 344 | + <View style={styles.buttonCon}> |
| 345 | + <Text style={styles.btn2} onPress={sendmsg}> |
| 346 | + SEND TEXT |
| 347 | + </Text> |
| 348 | + </View> |
| 349 | + <View> |
| 350 | + <Text style={styles.logText} multiline={true}> |
| 351 | + {logText} |
| 352 | + </Text> |
| 353 | + </View> |
335 | 354 | <View> |
336 | | - <Text onPress={sendmsg}>SEND TEXT</Text> |
| 355 | + <Text style={styles.logText}>{}</Text> |
337 | 356 | </View> |
338 | 357 | <View> |
339 | | - <Text multiline={true}>{logText}</Text> |
| 358 | + <Text style={styles.logText}>{}</Text> |
340 | 359 | </View> |
341 | 360 | </ScrollView> |
342 | 361 | </SafeAreaView> |
343 | 362 | ); |
344 | 363 | }; |
345 | 364 |
|
| 365 | +// 设置样式 |
| 366 | +const styles = StyleSheet.create({ |
| 367 | + titleContainer: { |
| 368 | + height: 60, |
| 369 | + backgroundColor: "#6200ED", |
| 370 | + }, |
| 371 | + title: { |
| 372 | + lineHeight: 60, |
| 373 | + paddingLeft: 15, |
| 374 | + color: "#fff", |
| 375 | + fontSize: 20, |
| 376 | + fontWeight: "700", |
| 377 | + }, |
| 378 | + inputCon: { |
| 379 | + marginLeft: "5%", |
| 380 | + width: "90%", |
| 381 | + height: 60, |
| 382 | + paddingBottom: 6, |
| 383 | + borderBottomWidth: 1, |
| 384 | + borderBottomColor: "#ccc", |
| 385 | + }, |
| 386 | + inputBox: { |
| 387 | + marginTop: 15, |
| 388 | + width: "100%", |
| 389 | + fontSize: 14, |
| 390 | + fontWeight: "bold", |
| 391 | + }, |
| 392 | + buttonCon: { |
| 393 | + marginLeft: "2%", |
| 394 | + width: "96%", |
| 395 | + flexDirection: "row", |
| 396 | + marginTop: 20, |
| 397 | + height: 26, |
| 398 | + justifyContent: "space-around", |
| 399 | + alignItems: "center", |
| 400 | + }, |
| 401 | + eachBtn: { |
| 402 | + height: 40, |
| 403 | + width: "28%", |
| 404 | + lineHeight: 40, |
| 405 | + textAlign: "center", |
| 406 | + color: "#fff", |
| 407 | + fontSize: 16, |
| 408 | + backgroundColor: "#6200ED", |
| 409 | + borderRadius: 5, |
| 410 | + }, |
| 411 | + btn2: { |
| 412 | + height: 40, |
| 413 | + width: "45%", |
| 414 | + lineHeight: 40, |
| 415 | + textAlign: "center", |
| 416 | + color: "#fff", |
| 417 | + fontSize: 16, |
| 418 | + backgroundColor: "#6200ED", |
| 419 | + borderRadius: 5, |
| 420 | + }, |
| 421 | + logText: { |
| 422 | + padding: 10, |
| 423 | + marginTop: 10, |
| 424 | + color: "#ccc", |
| 425 | + fontSize: 14, |
| 426 | + lineHeight: 20, |
| 427 | + }, |
| 428 | +}); |
| 429 | + |
346 | 430 | export default App; |
347 | 431 | ``` |
348 | 432 |
|
349 | 433 | ## 编译和运行项目 |
350 | 434 |
|
351 | 435 | 现在你可以开始在目标平台创建和运行项目。 |
352 | 436 |
|
353 | | -编译并在 iOS 真机运行: |
354 | | - |
355 | | -1. 连接苹果手机,设置为开发者模式; |
356 | | -2. 打开 `simple_demo/ios`,使用 `xcode` 打开 `simple_demo.xcworkspace`; |
357 | | -3. 依次点击 **Targets** > **simple_demo** > **Signing & Capabilities** 在签名选项下设置应用签名; |
358 | | -4. 点击 `Build` 构建并运行项目。程序构建完成后,自动安装和运行,并显示应用界面。 |
359 | | - |
360 | | - |
361 | | - |
362 | | -编译并在 iOS 模拟器中运行: |
363 | | - |
364 | | -1. 打开 `simple_demo/ios`,使用 `xcode` 打开 `simple_demo.xcworkspace`; |
365 | | -2. 在 `xcode` 中,选择模拟器 `iphone13`; |
366 | | -3. 点击 `Build` 构建并运行项目。程序构建完成后,自动安装和运行,并显示应用界面。 |
367 | | - |
368 | | - |
369 | | - |
370 | | -编译并在 Android 真机运行: |
371 | | - |
372 | | -1. 在 Android Studio 中打开 `simple_demo/android`; |
373 | | -2. 连接 Android 系统手机,设置为开发者模式,并且设置 USB 可调式; |
374 | | -3. 设置数据转发:在终端命令行输入 `adb reverse tcp:8081 tcp:8081`; |
375 | | -4. 启动服务:执行 `package.json` 里面的命令:`"start": "react-native start"`,在终端中运行命令 `yarn start`: |
| 437 | +编译运行 ios 设备: |
376 | 438 |
|
377 | 439 | ```sh |
378 | | -yarn start |
| 440 | +yarn run ios |
379 | 441 | ``` |
380 | 442 |
|
381 | | -5. 程序构建完成后,自动安装和运行,并显示应用界面。 |
| 443 | +编译运行 android 设备: |
382 | 444 |
|
383 | | - |
| 445 | +```sh |
| 446 | +yarn run android |
| 447 | +``` |
384 | 448 |
|
385 | | -demo 的界面: |
| 449 | +运行本地服务 |
386 | 450 |
|
387 | | - |
| 451 | +```sh |
| 452 | +yarn run start |
| 453 | +``` |
388 | 454 |
|
389 | 455 | ## 测试你的 app |
390 | 456 |
|
|
0 commit comments