In the last two weeks I've been learning how to properly do some networking in Swift2. I was totally desperate at some point because nothing worked as I expected, but yesterday finally I finished everything and my game is going to be on AppleTV - with multiplayer support like Crossy road. If you want to learn how to make a network connection between your devices you my friend are on the right place. In this post I am going to show you the basics, so you will be able to make a remote from your iOS device to support OS X or tvOS.
Download all the source!
Here is the link for the projects: github
If you want to create a killer app, you might want to target OS X, iOS, watchOS and soon tvOS as well. This snippet is going to help you to do detect your current platform:
#if os(iOS) let platform = "iOS" #elseif os(OSX) let platform = "OS X" #elseif os(watchOS) let platform = "watchOS" #elseif os(tvOS) let platform = "tvOS" #endif NSLog(platform)
I always forgot these macros...
Bonjour with TCP
In this example i used NSNetService to detect all the services, and the standard NSStream to make the connection and send messages to the server application. The whole thing is pretty easy. From the aspect of server side you just have to create the NSNetService object, and listen to the incoming connections (NSNetServiceDelegate). If you have the connections, just listen to the stream events to handle incoming data (NSStreamDelegate). I don't want to go into the details, just download the example and check it out for yourself. You will need Xcode 7.1, first start the tvOS or OS X target then the iOS. You have to be on the same WiFi network with both devices / simulators (Bonjour). Remember: this is just an example project, you still have to handle all other delegates, and errors. ==DO NOT USE IT IN PRODUCTION SOFTWARE!!! ==
Bonjour with UDP - CocoaAsyncSocket
Note: if you want to run this example please run: carthage update
This version has some external dependency. I am not satisfied with CocoaPods nowadays so I tried Carthage, and this tutorial helped me a lot. You can use whatever you want, but you will need the CocoaAsyncSocket library to be prepared for the project. This solution was the absolute winner for me (and probably it is for everyone who wants to create a fast network connection) because it's way more efficient than the other two methods. UDP is not going to handle errors for you so it's not reliable but that's a different story. If I wanted to build a file transfer app I'd definitely go with Bonjour + TCP, but for a real-time action game it was not the right fit. The setup here is the same as in the previous project. I created a server and a client class. These classes will handle all the networking communications for us. Check them out for yourself, and remember: ==DO NOT USE IT IN PRODUCTION SOFTWARE!!! ==
About the concept here: I am using an NSNetService UDP discovery to find the devices, and after that I am making the connection with the CocoaAsyncSocket library. In the docs they are saying that you should use the GCD classes (they are the new ones) so in my example the GCDAsyncUdpSocket. The usage is very easy, you should check out the docs, but after 5 minutes I could easily figure it out what's going on. One thing: for tvOS you will get a warning because the library is built for iOS. This will be fixed when tvOS comes out, you can check out the activity log for more info about this topic.
This one was not familiar to me at all. I basically just followed and ported the tutsplus.com code example to Swift2. I felt the bluetooth API somehow strange with all those messy delegates, socket programming is more my style. So if you are a CoreBluetooth expert you will think, hey this guy knows nothing about bluetooth networking at all. This was my first example and I learnt a lot from this tutorial so if you want to do your communication based on this technique it might help you. At least you will get the Swift2 port for the tutorial for free. :) So yes I am not a bluetooth expert, but if you want to know more go to tutsplus.com
- NSNetServices and CFNetServices Programming Guide
- UDPEcho sample code
- Networking Programming Topics
- Beej's Guide to Network Programming Using Internet Sockets
- Using Bonjour in Swift
- Creating a Game with Bonjour - Client and Server Setup
- CocoaAsyncSocket library
- iOS 7 SDK: Core Bluetooth - Practical Lesson