From 7d059a12f3529a02b04ad8845dbf575b6cbbd658 Mon Sep 17 00:00:00 2001 From: David Yahalomi Date: Sun, 20 Mar 2016 18:18:08 -0700 Subject: [PATCH] docs(Embedding iOS): Added swift code for embedding react-native in an app Summary: Closes https://github.com/facebook/react-native/pull/4198 Differential Revision: D3075219 Pulled By: mkonicek fb-gh-sync-id: c350a4074e4ee586804c48b9587135f3fce7b243 shipit-source-id: c350a4074e4ee586804c48b9587135f3fce7b243 --- docs/EmbeddedAppIOS.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/EmbeddedAppIOS.md b/docs/EmbeddedAppIOS.md index dbdb5e31d9..5bb036022c 100644 --- a/docs/EmbeddedAppIOS.md +++ b/docs/EmbeddedAppIOS.md @@ -118,6 +118,7 @@ In a view controller that wants to manage this view, go ahead and add an outlet @property (weak, nonatomic) IBOutlet ReactView *reactView; @end ``` +__NOTE__ For Swift apps there is no need for that. Here I disabled **AutoLayout** for simplicity. In real production world, you should turn on AutoLayout and setup constraints by yourself. @@ -149,6 +150,34 @@ Then add it as a subview of the `ReactView`. rootView.frame = self.bounds; ``` +### Swift apps + +Add the following to ReactView.swift file: + +``` +import UIKit +import React + +class ReactView: UIView { + + let rootView: RCTRootView = RCTRootView(bundleURL: NSURL(string: "http://localhost:8081/index.ios.bundle?platform=ios"), + moduleName: "SimpleApp", initialProperties: nil, launchOptions: nil) + + override func layoutSubviews() { + super.layoutSubviews() + + loadReact() + } + + func loadReact () { + addSubview(rootView) + rootView.frame = self.bounds + } +} +``` + +And then make sure your view is added in a ViewContainer or story board file. + ## Start Development Server In root directory, we need to start React Native development server.