Firebase快速集成-iOS篇

简介

Firebase是一款好用的云端实时数据库,官网戳这里
https://www.firebase.com
最近朋友写了Android版的快速集成
http://www.jianshu.com/p/06a15b6d400e ,现在来看看iOS平台如何操作。

创建Firebase开发者账户

关于云端数据的操做参考上面Android版的链接,可以共用同一个账户。
还没有注册开发者的戳这里注册 https://www.firebase.com/login/

安装Firebase.framework

Firebase可以通过Cocoapods集成,非常方便:

pod 'Firebase', '>= 2.5.0'  

没有用过Cocoapods的朋友可以看这里:
https://guides.cocoapods.org/using/getting-started.html

Cocoapods安装有问题也可以尝试手动安装

把Firebase.framework拖入Xcode工程,添加下面依赖:

CFNetwork
Security
SystemConfiguration
libicucore
libc++

编译,OK

PS: 遇到Firebase.framework下载有问题的,可以使用我放在网盘的拷贝
http://pan.baidu.com/s/1jHlsDz4

FireWeather

iOS版本开发环境 Xcode 7.2.1,iOS >= 9.0,Swift 2.1.1
首先准备好UI如下,一个UILabel用来展示天气,两个UIButton来改变天气:

fire-weather-ui.png

这里我使用了Storyboard来创建UI:

fire-weather-storyboard.png
引入Firebase库
import Firebase
初始化Firebase引用

我们这里和Android平台一样,操作"condition"

let firebaseRef = Firebase(url: "https://fire-weather.firebaseio.com/condition")
写数据

两个按钮的Action响应分别更改condition为"Sunny"和"Foggy"

@IBAction func sunny() {
    firebaseRef.setValue("Sunny")
}

@IBAction func foggy() {
    firebaseRef.setValue("Foggy")
}
读数据

监听"condition", 在label上实时展示

    firebaseRef.observeEventType(.Value, withBlock: { [weak self] snapshot in
        guard let condition = snapshot.value as? String,
            strongSelf = self else {
                return
        }
        
        strongSelf.weatherLabel.text = condition
    })

只有短短几十行,好了,您已经完成了:

fire-weather-viewcontroller-png.png

Build and Run ! 享受在不同设备上实时同步数据的乐趣吧!

演示项目源码(Swift 2 & ObjC):

https://github.com/little2s/FireWeather-iOS.git

What's Next:

https://www.firebase.com/docs/ios/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,846评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,489评论 2 45
  • 先来看几张我在穷游路上的照片吧 看到这里,你建议你还是先收藏吧,说不定万一哪天想要去体验穷游的时候就用上了...
    牛友果星球大萌阅读 4,930评论 153 310
  • drawnode整个结构如下: 和cocos风格一样,二段式构建方法,create->init,create函数n...
    核桃啊阅读 2,757评论 0 0
  • 背景:初次相遇时,是在阴雨天里,这狗子出来玩,到处跑,它经过真功夫门口时停住了,往里面看了一眼就进去了还跑到一个客...
    嘉娜卡suki阅读 291评论 2 1