Wednesday, 20 January 2016

iOS Swift- Sending Photos peer-to-peer using NSNetServices and NSStream

Recently I have been exploring NSNetServices and NSStream to send data between two iOS devices. The easiest way to do it is using Apple Multipeer Connectivity. However, I wanted to explore NSNetService and NSStream. There are few reasons why I used those APIs. First this has helped me to understand the complexity and issues while implementing networking Apps at a lower level. Second the concepts are useful while connecting the iOS devices with low Bluetooth energy devices.

In the sample App two iOS devices can send photos to each other. You can download my sample App from here.  Please click on the video to see the demo of the App.

I have used NSNetService to publish and browse photo sharing service. NSNetService can be used to publish the service and NSNetServiceBrowser can be used to browse published services. Once service is found and resolved then NSOutputStream is used to send photos and NSInputStream is used to receive photos.

I have used following resources to implement the sample App:
NSNetServices and CFNetworking programming guide
WiTap Sample
Stream Programming Guide

I plan to write more about the topic and update my code as I get a better understanding of the topic. Please let me know if you find any problems in the code.