# FireBase for SwiftUI小纸条（一）

首先创建一个firebase demo的Xcode项目：

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1660825640265/hu0HhKZpG.png align="left")

然后去[FireBase](https://firebase.google.com)创建一个项目：

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1660826215863/CJJ9XEIo0.png align="left")

当你创建好项目后，就可以在项目的首页看到一个关联到iOS项目的按钮，点它：

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1660826625751/uHSROSsPw.png align="left")

在注册应用中，记得在Apple软件包ID中输入你的iOS项目的Bundle Identifier：

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1660826727426/mMz-6YSpj.png align="left")

接下来按它的说明，将一个plist文件下载下来，放入你的iOS Project里去（记得要将它添加到你所有的target中去）：

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1660826823546/iAsUXrPfx.png align="left")

接下来，按它的要求在你的iOS项目中增加firebase-ios-sdk的Package（https://github.com/firebase/firebase-ios-sdk），下载时间比我想像的时间要长不少：

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1660827155291/lbbWmAx7Q.png align="left")

在这个SwiftPM里包括了非常多的模块，你可以通过[FireBase官方模块索引](https://firebase.google.com/docs/reference/swift/modules)来了解你需要加入的模块。接下来，我们会先尝试使用它的实时数据库功能来完成一个小游戏，所以我们选中FirebaseDatabase和FirebaseDatabaseSwift。

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1660914027710/O-8TVJ0D5.png align="left")

接下来需要将Firebase在你的Application进行初始化。因为我们需要在Application启动时就加载所以我们来个性App部分，加载初始化代码：

```
import SwiftUI
import FirebaseCore

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        return true
    }
}

@main
struct firebase_demoApp: App {
    // register app delegate for Firebase setup
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
```

接下来，我会开始使用Firebase的一些功能完成一些好玩的功能。但是无论做什么，都需要先使用FireBase for SwiftUI小纸条（一）部分来初始化一个Firebase和Xcode项目。
