[React Native]通过Pod移植RN到iOS原生项目及

2016-07-20  本文已影响631人  laznrbfe

前言

众所周知,react-native init [工程名]创建出来的iOS项目是Objective-C语言的。那我们怎么构建Swift工程呢?或者怎么通过Pod移植RN到iOS原生项目中呢?

步骤

react-native init ReactNativeProjectName
cd ios
vim Podfile

复制以下内容到Podfile文件(注:iOSProjectName改成你的Target名,没有特殊需要,改为iOS的工程名就行.[]里面的内容,按需添加.path后面的路径也可以按照你的需要进行修改.)。

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'
use_frameworks!

target 'iOSProjectName' do

  pod 'React', :path => '../node_modules/react-native', :subspecs => [
    'RCTText',
    'RCTImage',
    'RCTWebSocket',
    'RCTPushNotification',
    'RCTActionSheet',
    'ART',
    'RCTCameraRoll'
  ]
end
pod install
// 在AppDelegate.swift中.注:ReactNativeProjectName按照你自己的需求进行修改
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        let moduleName:String = "ReactNativeProjectName"
        let jsCodeLocation: NSURL

        jsCodeLocation = NSURL(string: "http://localhost:8081/index.ios.bundle?platform=ios&dev=true")!

//        jsCodeLocation = NSBundle.mainBundle().URLForResource("main", withExtension: "jsbundle")!

        let rootView: RCTRootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: moduleName, initialProperties: nil, launchOptions: launchOptions)
        
        rootView.backgroundColor = UIColor.whiteColor()
        
        let rootViewController = UIViewController()
        rootViewController.view = rootView
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = rootViewController
        window?.makeKeyAndVisible()
        
        
        return true
    }
npm start
上一篇下一篇

猜你喜欢

热点阅读