使用MongoDB Stitch构建iOS应用

I’m not sure why, but there’s not much resources to help out setting up MongoDB for iOS apps on the web. My first time using it was slightly frustrating — especially because the documentation was not too clear.

我不确定为什么,但是没有太多资源可以帮助您在网络上为iOS应用设置MongoDB。 我第一次使用它有点令人沮丧-尤其是因为文档不太清楚。

Hopefully, this tutorial might help some devs out there trying to learn how to use it.

希望本教程可以帮助一些开发人员尝试学习如何使用它。

What is MongoDB?

什么是MongoDB?

MongoDB is a document database that stores data in a ‘JSON-like’ format known as ‘BSON’ which basically stands for Binary JSON.

MongoDB是一个文档数据库,它以类似于JSON的格式存储数据,称为BSON,基本上代表Binary JSON

How will we implement it on our app?

我们将如何在我们的应用程序中实现它?

We’ll use MongoDB Stitch which is a server-less platform that helps developers create apps without having to manually set up a server. The great thing is that it will be directly integrated with your database.

我们将使用MongoDB Stitch ,它是一个无服务器平台,可帮助开发人员创建应用程序而无需手动设置服务器。 很棒的是,它将直接与您的数据库集成。

For this tutorial, we’ll run through the following steps:

对于本教程,我们将执行以下步骤:

  1. Create an iOS project & install Stitch using CocoaPods

    创建一个iOS项目并使用CocoaPods安装Stitch
  2. Create a server & stitch app on MongoDB

    在MongoDB上创建服务器和Stitch应用程序
  3. Write our code & upload a document

    编写我们的代码并上传文档

You can clone or download the project template from here: https://github.com/danielkioko/MongoApp

您可以从此处克隆或下载项目模板: https : //github.com/danielkioko/MongoApp

开始项目 (Beginning the Project)

  • Create an Xcode project in Single View App, let’s call it MongoApp

    在Single View App中创建一个Xcode项目,我们称它为MongoApp
  • Close it, and open Terminal. Navigate to the project folder, and run ‘pod init’

    关闭它,然后打开终端。 导航到项目文件夹,然后运行“ pod init”
  • Run ‘open podfile’ to open the Pod-file.

    运行“ open podfile”以打开Pod文件。
  • Type in pod 'StitchSDK', '~> 6.0.0' on a new line after useframeworks!

    pod 'StitchSDK', '~> 6.0.0'在新行中键入pod 'StitchSDK', '~> 6.0.0' useframeworks!

  • run ‘pod install’ and when it’s done open MongoApp.xcworkspace

    运行“ pod安装”,完成后打开MongoApp.xcworkspace

设置MongoDB (Setting Up MongoDB)

  • Next we’ll create our database with MongoDB, you’ll need an SignUp for that. PS: It’s free for your first cluster.

    接下来,我们将使用MongoDB创建数据库,为此您将需要一个SignUpPS:这对于您的第一个集群是免费的。

  • Once you’ve created your Cluster, you’ll create your database and it’s first collection.

    创建集群后,将创建数据库,它是第一个集合。
Image for post
  • Before we move to coding, we’ll need to create our StitchApp

    在进行编码之前,我们需要创建我们的StitchApp
Image for post
  • After that, On the side menu click Rules, create a rule by selecting a database. For now, don’t use a template and select Read & Write then save. Make sure to click on Review & Deploy Changes on the top bar to confirm any changes you make.

    之后,在侧面菜单上单击“规则”,然后通过选择数据库来创建规则。 目前,不要使用模板,而是选择“读写”然后保存。 确保单击顶部栏上的“ 查看并部署更改 ”,以确认所做的任何更改。

  • Click on Advanced mode to make sure you can: Read, Write, Insert & Delete from your database.

    单击“高级”模式以确保您可以:从数据库中读取,写入,插入和删除。
Image for post

让我们编码 (Let’s Code)

We’ll begin by creating our Data Model, A contact.

我们将从创建数据模型A联系人开始。

Image for post

In AppDelegate. Swift, we’ll initialize our stitch app, then link our app with the database and collection. Notice how Mongo uses our Data Model’s class as reference to how or data should be structured.

在AppDelegate中。 Swift,我们将初始化我们的缝制应用程序,然后将我们的应用程序与数据库和集合链接。 请注意,Mongo如何使用我们的数据模型的类作为结构或数据结构的参考。

Image for post

Inside ViewController.Swift, you’ll login Anonymously with Stitch

在ViewController.Swift内部,您将使用Stitch匿名登录

Image for post

Finally, create a new file, name it UploadViewController. Here, we’ll upload a contact right after you login.

最后,创建一个新文件,将其命名为UploadViewController。 登录后,我们将在此处立即上传联系人。

Image for post

That’s All! Run your app now 😄.

就这样! 立即运行您的应用😄。

Just in-case, here’s a link to the project on GitHub: https://github.com/danielkioko/MongoApp

以防万一,这里是指向GitHub上项目的链接: https : //github.com/danielkioko/MongoApp

I hope you had fun, feel free to let me know how goes in the comments.

希望您玩得开心,随时让我知道评论的进展。

Happy Coding! 🌟

编码愉快! 🌟

翻译自: https://medium.com/thecodr/build-an-ios-app-with-mongodb-stitch-893c2ec815e2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值