Quickstarters
CRUD Samples
How to Develop a CRUD App for iOS using Swift?
27 min
overview this walkthrough will guide you in building a simple crud (create, read, update, delete) application for ios using back4app as your cloud backend, you'll learn how to configure your project, design a flexible data model, and implement crud functionality with swift this tutorial demonstrates how to integrate your ios app with back4app, leveraging either the parse ios sdk or rest api, ensuring smooth data management and secure authentication initially, you'll set up a back4app project—named basic crud app ios —that provides a scalable, non relational database for your app you will define your data structures by creating classes and fields either manually or by using back4app’s ai agent next, you’ll explore how to manage your backend using the intuitive back4app admin app, a drag and drop tool designed to simplify data manipulation finally, you’ll connect your ios application with the backend, using either the parse ios sdk (when applicable) or rest/graphql api calls, and incorporate secure user authentication by the end of this guide, you will have built a production ready ios application that performs core crud operations, including secure user sign in and data handling main insights learn to create an ios crud application with an efficient backend understand how to architect a scalable backend and connect it seamlessly with an ios app discover how to use the back4app admin app for streamlined data creation, retrieval, modification, and deletion explore options for containerized deployment and further enhancements for your ios project prerequisites before you begin, ensure you have a back4app account with an active project need help? see getting started with back4app https //www back4app com/docs/get started/new parse app an ios development setup use xcode (version 12 or later) and ensure your development environment is up to date a solid grasp of swift, object oriented programming, and rest api fundamentals refer to the swift documentation https //developer apple com/swift/ for more details step 1 – setting up your project creating a new back4app project log in to your back4app account click on the “new app” button from your dashboard name your project basic crud app ios and follow the on screen instructions to complete the setup create new project after the project is created, it will appear in your dashboard, laying the foundation for your backend configuration step 2 – crafting your data model defining your data structures for this ios crud application, you will establish several classes (collections) in your back4app project below are the primary classes and their fields required to support your crud operations 1\ items class this class will store information about each item field data type description id objectid automatically generated unique identifier title string the name or title of the item description string a brief summary or details about the item createdat date timestamp marking when the item was created updatedat date timestamp marking the last update to the item 2\ users class this class manages user credentials and authentication data field data type description id objectid auto generated unique identifier username string unique identifier for the user email string unique email address of the user passwordhash string encrypted password for secure authentication createdat date timestamp of when the account was created updatedat date timestamp of the latest account update you can define these classes and fields directly in the back4app dashboard create new class you can add columns by selecting a data type, naming the field, setting a default value, and marking it as required if needed create column utilizing the back4app ai agent for schema configuration the integrated ai agent in your dashboard can automatically generate your data schema from a description this helps streamline the setup and ensures your data model supports all necessary crud operations how to use the ai agent open the ai agent access it via your project settings in the back4app dashboard describe your data model provide a comprehensive prompt detailing the classes and their respective fields review and implement examine the proposed schema and confirm to apply the configuration example prompt create the following classes in my back4app project 1\) class items \ fields \ id objectid (auto generated) \ title string \ description string \ createdat date (auto generated) \ updatedat date (auto updated) 2\) class users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ passwordhash string \ createdat date (auto generated) \ updatedat date (auto updated) this ai powered approach saves time and ensures a consistent, optimized data structure for your ios app step 3 – launching the admin app & managing crud operations overview of the admin app the back4app admin app provides a no code interface to efficiently manage your backend data its user friendly drag and drop features simplify the process of creating, reading, updating, and deleting records enabling the admin app go to the “more” section in your back4app dashboard select “admin app” and click “enable admin app ” set up your admin credentials by creating the initial admin account, which will also establish system roles (such as b4aadminuser ) enable admin app after activation, log into the admin app to manage your data admin app dashboard managing data with the admin app within the admin app you can insert records use the “add record” feature within any class (e g , items) to introduce new entries review/edit records select a record to view its details or modify its fields remove records delete entries that are no longer needed this intuitive interface simplifies backend management and enhances productivity step 4 – connecting your ios app with back4app with your backend ready, the next step is to link your ios application to back4app option a utilizing the parse ios sdk install the parse ios sdk you can integrate the sdk using swift package manager or cocoapods for cocoapods, add the following to your podfile pod 'parse' initialize parse in your app in your appdelegate, configure parse by inserting the following code // appdelegate swift import uikit import parse @uiapplicationmain class appdelegate uiresponder, uiapplicationdelegate { func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]?) > bool { let configuration = parseclientconfiguration { $0 applicationid = "your application id" $0 clientkey = "your ios key" // replace with your ios key $0 server = "https //parseapi back4app com" } parse initialize(with configuration) return true } // } implement crud operations create a service class in swift to handle your data operations for example, a service to fetch and display items // itemsservice swift import foundation import parse class itemsservice { func fetchitems(completion @escaping (\[pfobject]?) > void) { let query = pfquery(classname "items") query findobjectsinbackground { (objects, error) in if let error = error { print("error retrieving items \\(error localizeddescription)") completion(nil) } else { completion(objects) } } } func additem(title string, description string) { let item = pfobject(classname "items") item\["title"] = title item\["description"] = description item saveinbackground { (success, error) in if success { print("item successfully created ") } else if let error = error { print("creation error \\(error localizeddescription)") } } } func modifyitem(objectid string, newtitle string, newdescription string) { let query = pfquery(classname "items") query getobjectinbackground(withid objectid) { (item, error) in if let item = item { item\["title"] = newtitle item\["description"] = newdescription item saveinbackground { (success, error) in if success { print("item updated ") } else if let error = error { print("update error \\(error localizeddescription)") } } } else if let error = error { print("error finding item \\(error localizeddescription)") } } } func removeitem(objectid string) { let query = pfquery(classname "items") query getobjectinbackground(withid objectid) { (item, error) in if let item = item { item deleteinbackground { (success, error) in if success { print("item deleted ") } else if let error = error { print("deletion error \\(error localizeddescription)") } } } else if let error = error { print("error locating item \\(error localizeddescription)") } } } } option b using rest or graphql if the parse ios sdk does not suit your needs, you can execute crud operations via rest calls for instance, to fetch items using rest in swift import foundation class restclient { func retrieveitems() { guard let url = url(string "https //parseapi back4app com/classes/items") else { return } var request = urlrequest(url url) request httpmethod = "get" request addvalue("your application id", forhttpheaderfield "x parse application id") request addvalue("your rest api key", forhttpheaderfield "x parse rest api key") urlsession shared datatask(with request) { data, response, error in if let error = error { print("error fetching items \\(error localizeddescription)") return } guard let data = data else { return } if let json = try? jsonserialization jsonobject(with data, options \[]) { print("fetched items \\(json)") } } resume() } } integrate these api methods into your swift classes as necessary step 5 – securing your backend configuring access control lists (acls) safeguard your data by setting up acls for instance, to create an item that only its owner can access import parse func createprivateitem(title string, description string, owner pfuser) { let item = pfobject(classname "items") item\["title"] = title item\["description"] = description let acl = pfacl() acl setreadaccess(true, for owner) acl setwriteaccess(true, for owner) acl publicreadaccess = false acl publicwriteaccess = false item acl = acl item saveinbackground { (success, error) in if success { print("private item successfully created ") } else if let error = error { print("error saving item \\(error localizeddescription)") } } } class level permissions (clps) within the back4app dashboard, adjust the clps for your classes to enforce default security measures this ensures that only authenticated users or designated roles have access to specific data step 6 – implementing user authentication setting up user accounts back4app uses the built in parse user class to manage authentication in your ios application, handle user registration and login as illustrated below import parse class authservice { func registeruser(username string, password string, email string) { let user = pfuser() user username = username user password = password user email = email user signupinbackground { (succeeded, error) in if succeeded { print("user registered successfully!") } else if let error = error { print("registration failed \\(error localizeddescription)") } } } func loginuser(username string, password string) { pfuser loginwithusername(inbackground username, password password) { (user, error) in if let user = user { print("logged in as \\(user username ?? "unknown")") } else if let error = error { print("login error \\(error localizeddescription)") } } } } a similar approach can be implemented for managing sessions, password resets, and other authentication features step 7 – conclusion and future enhancements congratulations! you have successfully built an ios based crud application that integrates with back4app you configured a project named basic crud app ios , designed the items and users classes, and managed your data using the back4app admin app additionally, you connected your ios app through the parse sdk (or via rest/graphql) and implemented essential security measures future steps expand the application consider adding features like advanced filtering, detailed item views, or real time updates enhance backend capabilities experiment with cloud functions, third party api integrations, or more granular role based access control deepen your expertise visit the back4app documentation https //www back4app com/docs and explore additional tutorials to further optimize your application happy coding and best wishes on your journey to building robust ios applications!