首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >iOS 嵌入React Native 0.55版本

iOS 嵌入React Native 0.55版本

作者头像
井九
发布2024-10-12 09:20:05
发布2024-10-12 09:20:05
1470
举报
文章被收录于专栏:四楼没电梯四楼没电梯

之前配置过RN,但是新版本有些东西变了。重新配置了一下,记录一下过程

ref:

https://facebook.github.io/react-native/docs/integration-with-existing-apps

https://www.jianshu.com/p/a133d7e45aed

https://www.jianshu.com/p/284e05eba766

1、创建xcode project

2、pod init platform :ios, '9.0' target 'RNDemo' do   # Uncomment the next line if you're using Swift or would like to use dynamic frameworks   # use_frameworks!   platform :ios, '9.0'   # Pods for RNDemo   # Your 'node_modules' directory is probably in the root of your project,   # but if not, adjust the `:path` accordingly   pod 'React', :path => 'RNComponent/node_modules/react-native', :subspecs => [     'Core',     'CxxBridge', # Include this for RN >= 0.47     'DevSupport', # Include this to enable In-App Devmenu if RN >= 0.43     'RCTText',     'RCTNetwork',     'RCTWebSocket', # Needed for debugging     'RCTAnimation', # Needed for FlatList and animations running on native UI thread     # Add any other subspecs you want to use in your project   ]   # Explicitly include Yoga if you are using RN >= 0.42.0   pod 'yoga', :path => 'RNComponent/node_modules/react-native/ReactCommon/yoga' end

3、创建RNComponent目录 cd RNComponent

4、初始化node_modules brew install yarn yarn add react-native

yarn add react@16.3.1

{

"dependencies": {

"react": "16.3.1",

"react-native": "^0.55.4"

}

}

创建index.ios.js文件 import React from 'react'; import {AppRegistry, StyleSheet, Text, View} from 'react-native'; class RNHighScores extends React.Component {   render() {     var contents = this.props['scores'].map((score) => (       <Text key={score.name}>         {score.name}:{score.value}         {'\n'}       </Text>     ));     return (       <View style={styles.container}>         <Text style={styles.highScoresTitle}>2048 High Scores!</Text>         <Text style={styles.scores}>{contents}</Text>       </View>     );   } } const styles = StyleSheet.create({   container: {     flex: 1,     justifyContent: 'center',     alignItems: 'center',     backgroundColor: '#FFFFFF',   },   highScoresTitle: {     fontSize: 20,     textAlign: 'center',     margin: 10,   },   scores: {     textAlign: 'center',     color: '#333333',     marginBottom: 5,   }, }); // Module name AppRegistry.registerComponent('RNHighScores', () => RNHighScores); 5、pod install创建workspace sudo xcode-select --switch /Applications/Xcode.app xcrun -k --sdk iphoneos --show-sdk-path pod install 6、启动react-native yarn global add react-native-cli react-native start

这样http://localhost:8081/index.ios.bundle?platform=ios 就可以访问了

7、配置NSTemporaryExceptionAllowsInsecureHTTPLoads

<key>NSAppTransportSecurity </key> <dict> <key>NSExceptionDomains </key> <dict> <key>localhost </key> <dict> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads </key> <true/> </dict> </dict> </dict>

8、RCTRootView当做UIView就可以运行 NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios"];     RCTRootView *rootView =     [[RCTRootView alloc] initWithBundleURL: jsCodeLocation                                 moduleName: @"RNHighScores"                          initialProperties:      @{        @"scores" : @[                @{                    @"name" : @"Alex",                    @"value": @"42"                    },                @{                    @"name" : @"Joel",                    @"value": @"10"                    }                ]        }                              launchOptions: nil];

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档