EQOxx123 EQOxx123 - 8 months ago 18
Swift Question

How Can All Users Post UIViews To A Single Stream While Connected To Internet In Swift

I am trying to add a feature into my app where it can take a UIView and then post that UIView onto a stream that anyone who has the app can see. Any advice or help would be great and I hope this helps future viewers.


Try using the Firebase API. Firebase is a backend that is simple to work with and will allow you to create a realtime updating display. In firebase, data is formatted as JSON. There is a root dictionary with (arrays/strings/other dictionaries) within it.

Learn more about JSON: http://developers.squarespace.com/what-is-json/

Firebase will allow you to observe an array of dictionaries that can contain info that you can load into UIView's on the client side and display. When each user adds a new view, extract the info from the view, and post it to firebase- and firebase will update all the other clients.

Firebase Quickstart: https://www.firebase.com/docs/ios/quickstart.html

This is an example:

import Firebase // in your view controller

var myRootRef = Firebase(url:"https://<YOUR-FIREBASE-APP>.firebaseio.com")

override func viewDidLoad() {
    let views = myRootRef.childByAppendingPath(pathString: "viewData")    // this is where you will store an array of viewData objects

    // Attach a closure to read the data at our views reference 
    // Every time a view is added Firebase will execute this block on all listeners
    ref.observeEventType(.Value, withBlock: { snapshot in
        if let arr = snapshot.value as? [[String:AnyObject]] {
    }, withCancelBlock: { error in

func postData(data: [String:AnyObject]) {

    let views = myRootRef.childByAppendingPath(pathString: "viewData")    // this is where you will append new object
    let newObject = ref.childByAutoId() // only exists locally
    newObject.set(data) // Firebase handles getting this onto the server

The observer in viewDidLoad: is realtime so every time you add data from any client, it will update all the clients. Call postData: every time your user adds a new info with data. For this example I made each data model a dictionary, but you can change that as per your needs.

Sample data in JSON format:

"app" : { 
    "viewData": [ 
        0: { 
            "title": "This is the first view",
            "number": 45 
        1: { 
            "title": "This is the next view", 
            "number": 32