├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── bin └── cli.js ├── package.json └── react-native.config.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | yarn.lock 8 | .DS_Store 9 | 10 | # Runtime data 11 | pids 12 | *.pid 13 | *.seed 14 | *.pid.lock 15 | *.xcuser* 16 | *.xcwork* 17 | # Directory for instrumented libs generated by jscoverage/JSCover 18 | lib-cov 19 | 20 | # Coverage directory used by tools like istanbul 21 | coverage 22 | 23 | # nyc test coverage 24 | .nyc_output 25 | 26 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 27 | .grunt 28 | 29 | # Bower dependency directory (https://bower.io/) 30 | bower_components 31 | 32 | # node-waf configuration 33 | .lock-wscript 34 | 35 | # Compiled binary addons (http://nodejs.org/api/addons.html) 36 | build/Release 37 | 38 | # Dependency directories 39 | node_modules/ 40 | jspm_packages/ 41 | 42 | # Typescript v1 declaration files 43 | typings/ 44 | 45 | # Optional npm cache directory 46 | .npm 47 | 48 | # Optional eslint cache 49 | .eslintcache 50 | 51 | # Optional REPL history 52 | .node_repl_history 53 | 54 | # Output of 'npm pack' 55 | *.tgz 56 | 57 | # Yarn Integrity file 58 | .yarn-integrity 59 | 60 | # dotenv environment variables file 61 | .env 62 | 63 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ray Deck 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-swift 2 | 3 | Fixes a React Native Xcode project to permit Swift-based native components. 4 | 5 | [![npm version](https://badge.fury.io/js/react-native-swift.svg?style=flat)](https://badge.fury.io/js/react-native-swift) 6 | [![platform](https://img.shields.io/badge/platform-iOS-lightgrey.svg?style=flat)](https://github.com/rhdeck/react-native-swift) 7 | [![GitHub license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat)](https://github.com/rhdeck/react-native-swift/blob/master/LICENSE) 8 | 9 | ## Requirements 10 | 11 | - XCode 9.0 or newer. 12 | - React Native 0.60 or newer (haven't tested it lower than that) 13 | 14 | ## NEW: Add react-native-swift-cli to simplify development! 15 | 16 | _[react-native-swift-cli](https://npmjs.org/react-native-swift-cli/)_ includes a helper utility for initializing new swift-based components. 17 | 18 | ```bash 19 | yarn global add react-native-swift-cli 20 | ``` 21 | 22 | To learn how it works: 23 | 24 | ```bash 25 | react-native-swift --help 26 | ``` 27 | 28 | ## Adding to your app 29 | 30 | Even when not using _react-native-swift-cli_ you can add a Swift-based native module to you app relatively easily. 31 | 32 | ```bash 33 | yarn add myproject 34 | yarn add react-native-swift 35 | react-native swiftify 36 | ``` 37 | 38 | The _react-native-swift_ package will, via react-native link, take care of compatibility between your react native and the Swift based component. 39 | Done! 40 | 41 | ## How it works 42 | 43 | Starting in XCode 9.0, you can create static libaries that contain swift code. [Just create swift code the way found on the react-native documentation](https://facebook.github.io/react-native/docs/native-modules-ios.html) and add it to a static library. For reasons unknown, a couple flags need to get set inside the Xcode project file for the app to work with the library. This package forces that issue by adding a blank swift file to the build phases of the app targets, and setting the swift version flag. 44 | 45 | Future versions of Xcode may get less stupid and obviate the need for this package! Hopefully the templates remain a little helpful. 46 | 47 | ## 48 | 49 | ## Tips 50 | 51 | If you create a swift-based native component on your own (e.g. without react-native-swift init) the best practice is to add "react-native-swift" to the peerDependencies object. Will help others know how to make your module "just work." It will have pretty much no effect if it is buried in the tree. 52 | 53 | - See the repository [react-native-swift-demo-module](https://github.com/rhdeck/react-native-swift-demo-module) for an example that uses this as a peerDependency. 54 | 55 | Time for me to confess that this is my first FOSS project. Let me know how it works for you! @ me: @ray_deck on twitter and rhdeck on Github. 56 | -------------------------------------------------------------------------------- /bin/cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | console.log("I think you meant to run the react-native-swift cli version."); 3 | console.log("Try yarn global add react-native-swift-cli and try again"); 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-swift", 3 | "version": "1.2.3", 4 | "license": "MIT", 5 | "dependencies": { 6 | "glob": "^7.1.2", 7 | "@raydeck/xcode": "^2.2.0" 8 | }, 9 | "author": { 10 | "name": "Ray Deck", 11 | "email": "ray@raydeck.com", 12 | "url": "https://github.com/rhdeck" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/rhdeck/react-native-swift.git" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /react-native.config.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var pbxproj = require("@raydeck/xcode"); 3 | var fs = require("fs"); 4 | var path = require("path"); 5 | var glob = require("glob"); 6 | var cp = require("child_process"); 7 | function swiftify() { 8 | //Get my directory 9 | //Work with current dir 10 | var thisPath = process.cwd(); 11 | //Look for dependency 12 | var iosPath = path.join(thisPath, "ios"); 13 | if (!fs.existsSync(iosPath)) { 14 | console.log("Could not find ios in ", thisPath, iosPath); 15 | console.log(fs.readdirSync(thisPath)); 16 | process.exit(); 17 | } 18 | const globs = glob.sync(path.join(iosPath, "*.xcodeproj")); 19 | if (!globs || globs.length == 0) { 20 | console.log("Could not find xcodeproj directory"); 21 | process.exit(); 22 | } 23 | const xpdir = globs[0]; 24 | if (xpdir.length === 0) { 25 | console.log("Could not find xcodeproj directory inside: ", iosPath); 26 | process.exit(); 27 | } 28 | let filename = xpdir + "/project.pbxproj"; 29 | let properties = { 30 | SWIFT_VERSION: "4.0" 31 | }; 32 | if (!fs.existsSync(filename)) { 33 | console.log("Could not find pbxproj file:", filename); 34 | process.exit(); 35 | } 36 | const placeholder = "RNPlaceholder.swift"; 37 | const placeholderPath = iosPath + "/" + placeholder; 38 | if (!fs.existsSync(placeholderPath)) { 39 | console.log("Writing to ", placeholderPath); 40 | fs.writeFileSync(placeholderPath, ""); 41 | } 42 | var proj = pbxproj.project(filename); 43 | proj.parseSync(); 44 | proj.addSourceFileNew(placeholder); 45 | for (var key in properties) { 46 | proj.addBuildProperty(key, properties[key]); 47 | } 48 | const out = proj.writeSync(); 49 | fs.writeFileSync(filename, out); 50 | 51 | const packagePath = path.join(thisPath, "package.json"); 52 | if (fs.existsSync(packagePath)) { 53 | var package = require(packagePath); 54 | if (!package.isSwift) { 55 | package.isSwift = true; 56 | fs.writeFileSync(packagePath, JSON.stringify(package, null, 2)); 57 | cp.spawnSync("react-native", ["fixpods"]); 58 | } 59 | } 60 | } 61 | 62 | module.exports = { 63 | commands: [ 64 | { 65 | name: "swiftify", 66 | description: "Enable swift on the iOS app", 67 | func: swiftify 68 | } 69 | ] 70 | }; 71 | --------------------------------------------------------------------------------