我正在尝试将一项功能添加到我的应用程序中,它可以获取一个 UIView,然后将该 UIView 发布到任何拥有该应用程序的人都可以看到的流中。任何建议或帮助都会很棒,我希望这对 future 的观众有所帮助。
最佳答案
尝试使用 Firebase API。 Firebase 是一个易于使用的后端,可让您创建实时更新显示。在 firebase 中,数据被格式化为 JSON。有一个根字典,其中包含(数组/字符串/其他字典)。
了解有关 JSON 的更多信息:http://developers.squarespace.com/what-is-json/
Firebase 将允许您观察 一组字典,其中包含您可以加载到客户端 UIView 中并显示的信息。当每个用户添加一个新 View 时,从 View 中提取信息,并将其发布到 firebase,firebase 将更新所有其他客户端。
Firebase 快速入门:https://www.firebase.com/docs/ios/quickstart.html
这是一个例子:
import Firebase // in your view controller
var myRootRef = Firebase(url:"https://<YOUR-FIREBASE-APP>.firebaseio.com")
override func viewDidLoad() {
super.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]] {
// UPDATE DISPLAY WITH THIS DATA
}
}, withCancelBlock: { error in
println(error.description)
})
}
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
}
viewDidLoad: 中的观察者是实时的,因此每次您从任何客户端添加数据时,它都会更新所有客户端。调用 postData:每次您的用户添加带有数据的新信息时。对于这个示例,我将每个数据模型都设为字典,但您可以根据需要进行更改。
JSON 格式的示例数据:
"app" : {
"viewData": [
0: {
"title": "This is the first view",
"number": 45
},
1: {
"title": "This is the next view",
"number": 32
}
]
}
关于ios - 在 Swift 中连接到互联网时,所有用户如何将 UIViews 发布到单个流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36903693/