Building with AI Coding Agents · Course book
Chapter 3: The Anatomy of a Mobile App
A photo-sharing app looks simple from the outside: sign in, post a picture, see what friends posted. Behind that sit several separate systems that must cooperate, and an AI coding agent can only assemble them well if the person directing it knows what they are. This chapter describes the parts of a modern mobile app, including the screen-side code, sign-in, live data, image storage, monitoring and the route to the app stores, so that you can ask for the right things and recognise a sound result.
One codebase, two platforms
Phones run one of two operating systems, iOS on Apple devices and Android on most others. Each has its own programming languages and tools. Building an app natively means writing it twice, once for each, usually with two teams.
A cross-platform framework avoids the duplication. You write the app once, and the framework produces a version for each system. React Native is a widely used example: screens are written in JavaScript or TypeScript using the ideas of the React web library, and the framework turns them into genuine native buttons, lists and text on each platform. Expo is a set of tools built round React Native that handles the awkward parts, such as building the installable files, reaching the camera and sending updates.
| Approach | Codebases to maintain | Strengths | Weaknesses |
|---|---|---|---|
| Native, built separately for each system | Two | Full access to every device feature; best possible performance | Twice the work; two sets of skills |
| Cross-platform, such as React Native | One | One team and one language; most code shared; real native controls | Occasional platform-specific code still needed |
| A website wrapped as an app | One | Quickest route for an existing website | Feels less like a real app; limited device access |
For a small team or a single person working with an AI agent, the cross-platform route is usually the sensible choice. It halves the code the agent must write and you must review, and TypeScript, which checks that values have the expected types, catches many of an agent's mistakes before the app is even run.
The client and the backend
The code on the phone is the client. It draws the screens and responds to taps. It cannot be the whole app, for two reasons: data must be shared between users, and anything on a phone can be inspected by its owner, so it cannot be trusted to enforce rules. Shared data and rules live on servers, collectively called the backend.
You rarely build these services yourself today. Hosted products provide each one, and the app connects to them. That is a great help, but it hides a responsibility. Every rule about who may read or change which data, such as "only the author can delete a post", must be checked on the backend. If it is checked only in the client, anyone with modest technical skill can get round it. When reviewing agent-written code, this is the first thing to look for.
This division also suggests how to work with an agent. Asking for "a photo-sharing app" in one request invites a muddle. Asking for one slice at a time works far better: first sign-in, then the data model for posts, then the feed screen, then likes, and so on, with the app run and checked after each. Every slice touches the client and one backend service, which is small enough to review properly and to undo if it goes wrong.
Signing in without handling passwords
Storing passwords safely is hard, and a mistake is serious. Most new apps therefore avoid it and let people sign in with an account they already hold, such as a Google or Apple account. The approach, based on a standard called OAuth, works like this.
- The user taps "Sign in with Google" and is passed to Google's own sign-in screen.
- They enter their password there. Your app never sees it.
- Google hands the app a signed token, a piece of data stating who the person is and that they have just signed in.
- The app sends the token to your backend, which checks the signature to confirm it is authentic and has not expired.
- From then on, every request from that phone carries proof of identity, and the backend uses it to decide what the user may do.
The benefits are considerable: no passwords to guard, no "forgot my password" emails, and a sign-in screen people already trust. A dedicated sign-in service wraps all this up, including the fiddly differences between providers. Note that the app stores have rules here too; Apple generally expects an app offering sign-in through other companies to offer an equivalent privacy-minded option, such as its own, alongside.
Live data: polling and subscriptions
In a social app, a new like or message should appear by itself. There are two ways to arrange that.
With polling, the app asks the server "anything new?" at fixed intervals. It is simple, but most of the questions are wasted, and the load grows quickly. Take 1,000 people with the app open, each polling every 5 seconds. Each sends 12 requests a minute, so together they send 12,000 a minute, which is 200 every second and 200 × 86,400 = 17,280,000 a day, nearly all of them answered "no".
With a subscription, the app keeps one connection open and tells the server what it is watching, such as "the comments on this post". The server sends a message only when that data changes. Nothing is wasted, and updates arrive at once. A real-time database provides this ready-made: the client declares the data a screen needs, and the screen redraws whenever that data changes, with no refresh code to write.
Some features need a clock as well. A "story" that vanishes after 24 hours is stored with the time it was created, and the query that fetches stories returns only those created less than 24 × 60 × 60 = 86,400 seconds ago. That comparison must use the server's clock, since the clock on a phone can be changed by its owner.
Photos, reliability and release
Images. Photos are large, and handling them carelessly makes an app slow and expensive. A 12-megapixel photo is 4,000 by 3,000 pixels, or 12,000,000 pixels. Uncompressed at 3 bytes a pixel that is 36 megabytes; even as a compressed file it is typically several megabytes. A phone screen needs far less. Resized to 1,080 pixels wide, the image is 1,080 by 810, or 874,800 pixels, about 7 percent of the original. Good practice is to resize and compress on the phone before upload, keep the file in file storage and not in the database, and store only its address in the database record.
Reliability. When an app crashes on a stranger's phone you see nothing unless you have arranged to. A crash reporting service records each failure with the device type, the app version and the line of code responsible, and groups identical failures so you can see which affects the most people. It is worth adding before launch, not after the first complaint.
Release. Publishing differs from putting up a website. Both stores require a developer account and review each submission, and both expect certain things to be in place.
| Requirement | Why the stores ask for it |
|---|---|
| A privacy policy at a public web address | Users must be able to read what data is collected and why |
| A declaration of the data the app collects | Shown on the store listing before download |
| Terms of service | Expected wherever users can post their own content |
| A way to report content and block other users | Required of apps with user-generated content |
| A way to delete one's account | Users must be able to remove their data |
| Screenshots, a description and an icon | The store listing itself |
A small companion website usually holds the privacy policy, the terms and a landing page. These items are not afterthoughts: a missing privacy policy or account-deletion option is among the commonest reasons a first submission is rejected. An agent can draft the pages, but what they promise must be true of your app, and that is yours to check.
Check your understanding
- What does a cross-platform framework save, and what does it not entirely remove?
- Why must rules such as "only the author can delete a post" be enforced on the backend?
- In sign-in through another company, which party sees the user's password? (Answer: only the provider, such as Google; your app receives a signed token.)
- 2,000 users poll every 10 seconds. How many requests is that each second, and each day? (Answer: 2,000 ÷ 10 = 200 a second, and 200 × 86,400 = 17,280,000 a day.)
- A 4,000 by 3,000 photo is resized to 800 by 600. What share of the original pixels remains? (Answer: 480,000 ÷ 12,000,000 = 4 percent.)
- Why should the 24-hour limit on a story be checked with the server's clock?
- Name three things the app stores expect before they will approve an app with user-generated content.
This course book is available to read online only at tech-academy.chrysolites.org.