Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
246: App Tracking Transparency Implementation
If you're planning to use any third-party analytics or ad networks that track users across other companies' apps and websites, you have to deal with App Tracking Transparency (ATT). It's not optional. If you submit an app that tracks users without this prompt, Apple will reject it faster than you can hit the upload button.
For this lesson, we're going to build a small "User Preferences" module for a hypothetical news app. The app wants to track the user's reading habits across other news sites to suggest better articles. Let's get this implemented.
Adding the justification to the plist
Before we write a single line of Swift code, we have to tell the system why we want this permission. If you don't provide a string for the user to read, the app will crash the moment you request authorization. I'll be honest: I actually forgot this during my last project and spent ten minutes staring at a crash log wondering why the ATTrackingManager was killing my process.
Open your Info.plist and add the key NSUserTrackingUsageDescription. I'm going to set the value to: "We use your activity across other apps to suggest news stories that actually match your interests." Be specific here; Apple's reviewers hate vague descriptions like "To improve user experience."
Requesting permission in Swift
Now for the logic. We need to import the AppTrackingTransparency framework. I like to wrap this logic in a dedicated service so my View Controllers don't get cluttered with permission boilerplate.
import AppTrackingTransparency
import AdSupport
class TrackingService {
static let shared = TrackingService()
func requestPermission() {
ATTrackingManager.requestTrackingAuthorization { status in
switch status {
case .authorized:
print("Permission granted! We can now access the IDFA.")
// Here is where you'd initialize your ad SDKs
case .denied:
print("Permission denied. Using generic tracking.")
case .notDetermined:
print("The user hasn't been asked yet.")
case .restricted:
print("Permission restricted (e.g., parental controls).")
@unknown default:
print("Something unexpected happened.")
}
}
}
}
The timing trap
Here is a subtle point that trips up a lot of developers. If you call requestPermission() inside viewDidLoad or init of your first screen, the prompt might never appear. Why? Because the app hasn't fully transitioned to the active state yet, and the system simply ignores the request.
I've found the most reliable way to handle this is to trigger the request when the app becomes active. In a modern SwiftUI app, you can use onChange(of: scenePhase). In a UIKit app, you'd typically do this in applicationDidBecomeActive. Let's use a simple approach for our news app by calling it slightly after the view appears:
// Inside your main view or coordinator
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
TrackingService.shared.requestPermission()
}
That one-second delay is a bit of a "hack," but it ensures the app is fully in the foreground and the system is ready to present the alert. In a production app, I'd recommend showing your own "pre-prompt" screen first—a simple view explaining the value of tracking—before triggering the official system popup. Users are much more likely to hit "Allow" if they understand the benefit first.
📋 Practical Task
Implementing ATT for the 'FitTrack' Analytics Module
You are adding a new feature to a fitness app called 'FitTrack' that integrates with third-party health platforms. Your goal is to implement the App Tracking Transparency flow to ensure the app is compliant with App Store guidelines.
- Add the
NSUserTrackingUsageDescriptionkey to yourInfo.plistwith a descriptive message explaining that tracking is used to sync fitness data across platforms. - Create a
TrackingManagersingleton class that handles theATTrackingManager.requestTrackingAuthorizationcall. - Implement a mechanism (such as a delayed call or a response to app state changes) to ensure the prompt appears only after the app has fully entered the foreground.
- Print the resulting
ATTrackingManager.AuthorizationStatusto the console so you can verify that 'Authorized', 'Denied', and 'Restricted' states are handled correctly.
There are no comments for now.