美文网首页ReactNative
iOS原生应用中集成RN记录-个人笔记

iOS原生应用中集成RN记录-个人笔记

作者: hophia | 来源:发表于2016-06-21 18:19 被阅读1199次

    1.新建iOS应用, 并创建一个UIViewController, 指定为window的跟视图控制器

    2.使用cocoapods

    vim Podfile
    platform :ios, '7.0'
    pod 'AFNetworking'
    然后pod install
    完成之后的工程目录和文件目录如下:

    屏幕快照 2016-06-11 下午9.05.22.png
    屏幕快照 2016-06-11 下午9.06.44.png

    3.安装react-native

    我们在项目中建一个名为reactnative的文件夹, 用于存放我们react-native的相关文件, 再创建一个.packagejson文件, 用于初始化react-native.


    屏幕快照 2016-06-11 下午9.28.11.png

    在终端执行安装, $cd reactnative $npm install
    安装成功后,reactnative目录会产生一个node_modules,里面就是react-native依赖的所有项目包。

    4.在reactnative目录下创建 index.ios.js

    文件:

    屏幕快照 2016-06-11 下午10.00.37.png

    这就是我们js程序的入口文件, ok,以上我们已经完成react-native的准备工作,接下来要开始集成啦~

    5.Cocoapods集成react-native

    在Podfile文件中加上

    取决于你的工程如何组织,你的node_modules文件夹可能会在别的地方。

    请将:path后面的内容修改为正确的路径。

    pod 'React', :path => './reactnative/node_modules/react-native', :subspecs => [
    'Core',
    'RCTNetwork',
    'RCTText',
    'RCTWebSocket',

    添加其他你想在工程中使用的依赖。

    ]

    记得要添加所有你需要的依赖。举例来说,<Text>元素如果不添加RCTText依赖就不能运行
    依赖文件安装进入工程目录,执行 $ pod install ,就完成了react-native工程包的集成。
    此时目录结构是:


    屏幕快照 2016-06-19 下午10.34.35.png

    6.在ViewController.m文件夹中:

    屏幕快照 2016-06-21 下午6.22.37.png

    7.启动开发服务器

    在运行我们的项目之前,我们需要先启动我们的开发服务器。进入 reactnative目录 ,然后启动。 $ cd reactnative $ react-native start

    运行项目界面如下:

    Simulator Screen Shot 2016年6月11日 下午9.55.55.png

    8、更新App Transport Security

    直接运行项目会报 Could not connect to development server 错误,官网中有这段话:

    在iOS 9以上的系统中,除非明确指明,否则应用无法通过http协议连接到localhost主机。 我们建议你在Info.plist文件中将localhost列为App Transport Security的例外。 如果不这样做,在尝试通过http连接到服务器时,会遭遇这个错误 - Could not connect to development server.

    打开工程中的 Info.list 文件,添加下面配置即可:

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

    屏幕快照 2016-06-11 下午9.58.06.png

    9、运行iOS项目

    Simulator Screen Shot 2016年8月5日 下午4.32.08.png

    相关文章

      网友评论

      • hzy1314:哥们,你好,按照你的思路,我的还是报Could not connect to development server这个错误
      • N最珍贵:运行就崩溃,大神求解决

        objc[10478]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x10f5a0998) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x10f3c2880). One of the two will be used. Which one is undefined.
        2017-03-30 16:31:59.022 [warn][tid:NSOperationQueue 0x61000003ddc0 :: NSOperation 0x61000004aec0 (QOS: DEFAULT)][RCTDevMenu.m:211] RCTSourceCode module scriptURL has not been set
      • tinyjoy:清晰

      本文标题:iOS原生应用中集成RN记录-个人笔记

      本文链接:https://www.haomeiwen.com/subject/nrugdttx.html