When I set out to learn ReactiveCocoa I couldn’t find any guides that quickly got me setup and working with the latest version and Swift 2.0. So, after a bit of experimentation and trial and error, I’ve found a way to get things working with the 4.0 alpha 1 version of ReactiveCocoa and Swift 2.0 with Xcode 7. This has been cobbled together with my poking around in the source of ReactiveCocoa (v4 is available here) and going through their examples. If you’ve played with the RAC examples before then this should look somewhat familiar. All this app does is create a SignalProducer on a text field and make a network request to a server to pull down what was typed into the field and display this on the screen. It’s not meant to be a complete “best practices” guide about ReactiveCocoa but rather simple environment setup instructions. My code is available on GitHub but this guide should provide the nuts and bolts of what an average user needs to get started. Here we go.
First, create a new Single Page iOS application in Xcode 7. Once this has been created, add a Cartfile to the root directory that contains just the single line:
1 |
github "ReactiveCocoa/ReactiveCocoa" >= 4.0 |
Now, ensure that you’re using the Swift 2.0 version of ‘swiftc’ as the 2.1 version can cause problems. To change it follow this explanation. Once you’ve done this run the Carthage update command as follows:
1 |
carthage update --platform iOS --no-use-binaries |
This should now build your appropriate Framework files. Head into the Finder and drag the Frameworks into your Xcode project. Be sure to add them to the Embedded Binaries portion of the app configuration. It should look like so:
With iOS 9 it now disallows all communications to any insecure (ie HTTP) endpoints unless explicitly told to whitelist some or all sites. This is easy enough to fix with the NSAppTransportSecurity key in Info.plist. Add this key as a dictionary then add the key NSAllowsArbitraryLoads to this dictionary as a boolean set to YES.
Next, place a UITextField and UILabel on the main screen in Storyboard and wire them to the textField and nameLabel properties in the ViewController.swift file. Test by compiling and fix any AutoLayout or compiler errors.
Finally, since this app is hitting the network looking for a JSON response we need a server to do that. I quickly made a Node.js server that will work for our needs. To use this make sure you have Node.js installed and save the following source in a file named server.js . Using a terminal, navigate to where you saved it and run npm install express (express is the only node module dependency). Finally, ensure nothing else is using port 8001 and start the server with node server.js . Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var express = require('express'); var app = express(); var server = require('http').Server(app); var _port = 8001; app.use(express.Router()); app.on('listening', onListening); app.listen(_port); function onListening() { console.log("Listening on port " + _port); } app.get('/hello/:name', function(req, res) { res.send({ name : req.params.name }); }); |
Once the server is up and running your iOS app should be able to communicate with it! This is a very simple example of using ReactiveCocoa but there were enough pitfalls when trying to use the latest version that I thought I’d write this up. Again, all iOS source is available on my GitHub account. Please send any questions in!