mobile-ios
Native iOS with Swift and SwiftUI: state with Observation, navigation stacks, lists, concurrency with async/await, actors and MainActor, networking with URLSession, persistence with SwiftData or Core Data, the Keychain, app lifecycle and background tasks, permissions and privacy manifests, accessibility, testing, and build configurations. Read when the project is a native iOS app.
- License 许可
- Apache-2.0
- 所属仓库
- enowdev/enowxcli
- 最近更新
- 2026-10-06
- Stars
- ★ 67
- Forks
- 21
01 前言 Front Matter
- name
- mobile-ios
- description
- Native iOS with Swift and SwiftUI: state with Observation, navigation stacks, lists, concurrency with async/await, actors and MainActor, networking with URLSession, persistence with SwiftData or Core Data, the Keychain, app lifecycle and background tasks, permissions and privacy manifests, accessibility, testing, and build configurations. Read when the project is a native iOS app.
03 SKILL.md 正文
Native iOS with Swift and SwiftUI
The naive iOS app: NavigationView and ObservableObject copied from old
tutorials, DispatchQueue.main.async sprinkled until the warnings stop, a
fetch in onAppear that reruns on every return and is never cancelled, the
token in UserDefaults, dates that fail to decode because the server sends
fractional seconds, no privacy manifest, and a crash on the first camera
request because the usage string is missing. This skill is current SwiftUI
practice for iOS 17 and later. Conventions in mobile-ux, sync in
mobile-data, signing and stores in mobile-release.
1. Targets and project
- SwiftUI first; UIKit through
UIViewRepresentableandUIViewControllerRepresentablewhere SwiftUI falls short,UIHostingControllerto bring SwiftUI into a UIKit app. - Deployment target iOS 17 for a new app (Observation, SwiftData,
ContentUnavailableView); newer APIs behindif #available(iOS 26, *). - Swift 6 language mode with complete concurrency checking. Xcode 26 projects default to main-actor isolation (Swift 6.2 approachable concurrency); keep the settings of a project you did not create.
- Features in local Swift packages (faster builds, working previews); new dependencies through Swift Package Manager, since CocoaPods trunk is scheduled to become read-only in December 2026.
- Environments as configurations and schemes, values in xcconfig files read
through Info.plist (
APIBaseURL = $(API_BASE_URL)):
// Config/Staging.xcconfig; "/$()/" stops "//" starting a comment
API_BASE_URL = https:/$()/api.staging.acme.com
PRODUCT_BUNDLE_IDENTIFIER = com.acme.app.staging
2. State with Observation
@MainActor @Observable
final class OrdersModel {
private(set) var orders: [Order] = []
private(set) var failure: Error?
private let api: OrdersAPI
init(api: OrdersAPI) { self.api = api }
func load() async {
do { orders = try await api.orders(); failure = nil }
catch is CancellationError {}
catch { failure = error }
}
}
struct OrdersView: View {
@State private var model: OrdersModel
init(api: OrdersAPI) { _model = State(initialValue: OrdersModel(api: api)) }
var body: some View {
List(model.orders) { order in
NavigationLink(value: Route.order(order.id)) { OrderRow(order: order) }
}
.task { await model.load() } // cancelled when the view goes away
.refreshable { await model.load() }
}
}
@Stateowns what the view creates;@Bindablefor bindings into an observable passed in;@Environment(Model.self)for app-wide models. Observation tracks only the propertiesbodyreads.- A model where there is loading, validation or logic to test; plain
@Stateotherwise.ObservableObjectin existing code: migrated a feature at a time.@SceneStoragefor per-window UI state.
3. Navigation
enum Route: Hashable { case order(Order.ID), settings }
@State private var path: [Route] = []
NavigationStack(path: $path) {
OrdersView(api: api)
.navigationDestination(for: Route.self) { route in
switch route { case .order(let id): OrderDetailView(id: id); case .settings: SettingsView() }
}
}
.onOpenURL { url in if let route = Route(url: url) { path = [route] } }
- One
NavigationStackper tab;NavigationSplitViewon iPad. Sheets with.presentationDetents([.medium, .large])and.interactiveDismissDisabled(hasChanges). - Universal Links: Associated Domains (
applinks:acme.com) plus/.well-known/apple-app-site-associationon the server.
4. Lists and the cost of body
Listfor standard lists (reuse, swipe actions, edit mode);LazyVStackin aScrollViewfor custom ones.ForEachover stable ids: never indices of a mutable array, never\.selfon values that repeat.bodyruns often: no formatters built in it (Text(date, format: .dateTime.day().month())), no sorting of large arrays, no I/O. NoAnyView; anifchanges identity, a modifier value keeps it.AsyncImagehas no disk cache: feeds use Nuke or Kingfisher. Debug withlet _ = Self._printChanges()inbody(mobile-performance).
5. Concurrency
.taskand.task(id:)for work tied to a view (cancelled and restarted for you);Task {}inherits the actor;Task.detachedalmost never.@MainActoron UI models; actors for shared mutable state;Sendableacross boundaries (@unchecked Sendableonly with a lock and a comment).- Under approachable concurrency, nonisolated async functions run on the
caller's actor; mark heavy work
@concurrentto take it off the main one. async letfor a fixed set of parallel calls,withThrowingTaskGroupfor a dynamic set;try Task.checkCancellation()in long loops;CancellationErroris never shown to the user.- One token refresh for all callers, despite actor reentrancy:
actor TokenProvider {
private var token: Token?
private var refreshing: Task<Token, Error>?
func current() async throws -> Token {
if let token, !token.expiresSoon { return token }
if let refreshing { return try await refreshing.value }
let task = Task { try await refreshToken() }
refreshing = task
defer { refreshing = nil }
let fresh = try await task.value
token = fresh
return fresh
}
}
6. Networking
let (data, response) = try await session.data(for: request), status checked, then decoded. One configured session:timeoutIntervalForRequest = 20(default 60),waitsForConnectivity = truefor requests that may wait (bounded bytimeoutIntervalForResource),allowsConstrainedNetworkAccess = falsefor optional heavy downloads..iso8601rejects fractional seconds; accept both:
decoder.dateDecodingStrategy = .custom { decoder in
let text = try decoder.singleValueContainer().decode(String.self)
if let date = try? Date.ISO8601FormatStyle(includingFractionalSeconds: true).parse(text) { return date }
return try Date.ISO8601FormatStyle().parse(text)
}
- Errors mapped in one place (
URLError, HTTP status, decoding); retries only for idempotent requests (mobile-data). Large transfers on a background session continue while suspended. NoNSAllowsArbitraryLoads.
7. Persistence and the Keychain
| Data | Where |
|---|---|
| Records used offline | SwiftData; Core Data or GRDB for heavy queries or older targets |
| Small preferences | UserDefaults: never secrets or large data |
| Files the app manages | Application Support; re-downloadable ones in Caches (purgeable) |
| Tokens, secrets | Keychain |
- SwiftData:
.modelContainer(for: Draft.self),@Query(sort: \Draft.updatedAt, order: .reverse),context.insert,try context.save()at explicit points. Schema changes throughVersionedSchemaand aSchemaMigrationPlan, tested from every shipped version. With CloudKit: no unique attributes, every property optional or defaulted, relationships optional.
func saveRefreshToken(_ token: String) throws {
let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: "com.acme.app.auth",
kSecAttrAccount as String: "refresh"]
SecItemDelete(query as CFDictionary)
var item = query
item[kSecValueData as String] = Data(token.utf8)
item[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
let status = SecItemAdd(item as CFDictionary, nil)
guard status == errSecSuccess else { throw KeychainError(status: status) }
}
AfterFirstUnlockThisDeviceOnlywhen background work needs the token,WhenUnlockedThisDeviceOnlyotherwise;ThisDeviceOnlykeeps it out of backups. Keychain items survive deleting the app: on the first launch after install (no flag inUserDefaultsyet), delete stale ones.
8. Lifecycle, background work and push
scenePhase:.backgroundsaves and stops work,.activerefreshes stale data;.inactive(the app switcher) is not background. A save that must finish runs insidebeginBackgroundTask(about 30 seconds).- BGTaskScheduler: app refresh (about 30 seconds, when the system chooses
from usage) and processing tasks (minutes, usually idle and charging).
Identifiers in
BGTaskSchedulerPermittedIdentifiers, Background Modes on,.backgroundTask(.appRefresh("com.acme.refresh")) { await sync() }, the next request submitted each run. iOS 26 addsBGContinuedProcessingTaskRequestfor user-started work with progress. - Push: APNs with a token-based
.p8key on the server;requestAuthorization(options: [.alert, .badge, .sound])at a sensible moment, or.provisional; the device token sent on every launch (it changes); taps routed throughUNUserNotificationCenterDelegateto a deep link. Silent pushes are throttled: never the only way data arrives.
9. Permissions and privacy
- Every protected resource needs its Info.plist string
(
NSCameraUsageDescription,NSLocationWhenInUseUsageDescription,NSMicrophoneUsageDescription,NSFaceIDUsageDescription); without it the app crashes on the request. PrivacyInfo.xcprivacyfor the app and each SDK: data collected, tracking domains, and a reason per required-reason API underNSPrivacyAccessedAPITypes:CA92.1(UserDefaults),C617.1(file timestamps),35F9.1(system boot time),E174.1(disk space). The Organizer builds the combined privacy report from an archive.- App Tracking Transparency only when data is linked with other companies' apps or sites for ads or brokers, never for first-party analytics.
10. Accessibility
- Text styles for all text;
@ScaledMetric var icon = 24beside text;dynamicTypeSize.isAccessibilitySizeorViewThatFitsto stack rows at the largest sizes. - VoiceOver:
.accessibilityLabelon icon-only controls,.accessibilityAddTraits(.isHeader),.accessibilityElement(children: .combine)for rows,.accessibilityAction(named:),Image(decorative:)for decoration. accessibilityReduceMotionswaps movement for fades (motion-stacks); previews for empty, error, dark and.dynamicTypeSize(.accessibility3).
11. Testing
import Testing
@testable import Acme
@MainActor struct OrdersModelTests {
@Test func showsOrdersAfterLoad() async {
let model = OrdersModel(api: StubOrdersAPI(orders: [.sample]))
await model.load()
#expect(model.orders.count == 1 && model.failure == nil)
}
}
- Swift Testing for new unit tests (
#require,@Test(arguments:)); XCTest for UI and performance tests; HTTP stubbed withURLProtocol. - XCUITest for a few journeys: elements by
accessibilityIdentifier, a scenario passed inapp.launchEnvironment,waitForExistence(timeout: 5), andtry app.performAccessibilityAudit()on key screens.
Check it
xcrun simctl list devices available
xcodebuild -scheme Acme -destination 'platform=iOS Simulator,name=iPhone 17' build test | xcbeautify
swift test --package-path Packages/Features
xcrun simctl openurl booted "acme://orders/42"
- Once with the Thread Sanitizer on; Simulate Memory Warning on heavy screens; no new concurrency warnings in the Swift 6 build.
Avoid
NavigationView; new ObservableObject code in an Observation app; work in
onAppear without cancellation; DispatchQueue.main.async to silence the
checker; Task.detached by habit; formatters or sorting in body;
AnyView; tokens in UserDefaults; missing usage strings or privacy
manifest entries; ATT prompts without tracking; background work assumed to
run on time.
04 同仓库更多 Skills
demo-skill
Project-scope demo skill for tests.
writing
Writing words people read: the specific thing instead of claims, words to drop, sentences, interface copy (buttons, labels, errors, empty states, confirmations), page copy, notifications and emails, alt text and link text, numbers and dates, words that translate well, documentation and voice. Read before writing a page's copy, an interface's text or a document.
ui
Designing or restyling an interface: direction, layout, type, colour, icons, components, states, responsive behaviour. Read before building a page, a screen or a set of components.
ui-themes
Light and dark themes, and colour that holds in both: tokens with a value per theme, and what each kind of content needs to stay visible (icons, SVG drawings and line art, logos, photos, screenshots, charts, code, maps, borders, shadows, focus, form controls), switching without a flash, and checking both. Read before building a second theme or a theme toggle, and before any drawing, logo or chart on a page that has two themes.
ui-structure
Structure before styling: the blueprint written before any interface code (the screen's job, the regions its kind of page needs, their order and why, the layout skeleton in numbers, the components and their states), the anatomy and order of a homepage, a dashboard, a list, a detail page, settings, a form, sign-in, pricing, docs, an article and a checkout, and how to turn the blueprint into code (tokens, layout primitives, components, sections, page). Read first, before building or rebuilding any page or screen.
ui-stack-vue
Building interfaces in Vue 3 and Nuxt: project layout, script setup with typed defineProps, defineEmits and defineModel, attribute fallthrough, slots and scoped slots, template refs, ref, reactive and computed, watch versus watchEffect, composables and VueUse, provide and inject, Pinia, keyed v-for, transitions, Nuxt 4 (app directory, pages, layouts, useFetch and useAsyncData keys, $fetch in handlers, server routes, runtime config, useSeoMeta, NuxtImg, middleware), VeeValidate forms, Reka UI and Nuxt UI, testing, vue-tsc, and the traps. Read when the project uses Vue or Nuxt.