bhylu.com

Advertisements

Swift Privacy and Permissions

Ask for access at a meaningful action and prefer a narrower system capability when it meets the task. Requires Mac/Xcode; this Apple-only source was not SDK-compiled or run here.

How it works

Permissions are platform-specific and can be refused or changed later. This iOS 17+ source uses PhotosPicker for an explicit user selection instead of requesting blanket photo-library access. The selected item is loaded only after selection. No photo is uploaded or saved by this app, and the code does not claim every permission works this way.

Apple-only setup and review status

Create a separate iOS SwiftUI App learning project in a suitable Xcode version, choose an appropriate simulator/device and replace the generated app/view Swift source with this complete file. Keep one @main entry point. Widget and App Clip lessons explicitly require their own extension/clip targets instead; capability, purpose-string, entitlement and account steps in the lesson must be completed separately. No Xcode project, signing certificate, account or service was provisioned here.

Bhylu has no Swift browser runner. The JavaScript/Python editors do not run these sources; Apache, PHP and MySQL are not required. Downloads are source text, never executable teaching endpoints.

Download the complete privacy-permissions.swift · Download all source files with hash checks

For the optional collection, save download-examples.mjs in a new folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/swift/. It refuses an existing bhylu-swift-examples folder and installs or executes no Swift automatically. Apple-only files remain separate Xcode learning examples, not a portable Swift package target.

Example

privacy-permissions.swift

// Apple-only learning file: iOS 17+ target with a suitable Xcode SDK.
// Not compiled or run in this Windows/Linux review.
import SwiftUI
import PhotosUI


struct DemoView: View {
    @State private var selection: PhotosPickerItem?
    @State private var notice = "No photo selected"
    var body: some View {
        VStack(spacing: 20) {
            PhotosPicker("Choose a reference photo", selection: $selection, matching: .images)
            Text(notice)
            Text("Local selection demo; no upload.").font(.caption)
        }.padding()
        .onChange(of: selection) { _, item in
            Task {
                do {
                    if let data = try await item?.loadTransferable(type: Data.self) {
                        notice = "Selected data: \(data.count) bytes"
                    } else { notice = "No readable selection" }
                } catch { notice = "Selection could not be read" }
            }
        }
    }
}

@main
struct WorkshopDemoApp: App {
    var body: some Scene { WindowGroup { DemoView() } }
}

Expected behaviour and manual checks

Manual check on an Apple target: select, cancel and select an unavailable/cloud-backed item. Inspect actual permission and loading behaviour. Large-image and metadata policies need separate testing. None of these device checks ran here.

The UI reports what the picker actually returns instead of inventing a successful upload. Loading a complete Data value is not a streaming-size limit; a production image workflow needs bounded decoding and a clear metadata policy. Explain each requested capability truthfully.

Primary references: The Swift Programming Language and Apple framework documentation. Use the selected platform’s current requirements. Fictional workshop examples do not establish production safety or deployment approval.

Advertisements