### Install via Swift Package Manager Source: https://github.com/dimayurkovski/adsearchkit/blob/main/README.md Add the dependency to your Package.swift file to integrate the framework. ```swift dependencies: [ .package(url: "https://github.com/dimayurkovski/AdSearchKit.git", from: "1.1.0") ] ``` -------------------------------- ### Install AdSearchKit via CocoaPods Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Add the dependency to your Podfile and run pod install. ```ruby # Podfile platform :ios, '12.0' use_frameworks! target 'MyApp' do pod 'AdSearchKit' end # Then run: pod install ``` -------------------------------- ### Install via CocoaPods Source: https://github.com/dimayurkovski/adsearchkit/blob/main/README.md Add the pod dependency to your Podfile. ```ruby pod 'AdSearchKit' ``` -------------------------------- ### Install AdSearchKit via Swift Package Manager Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Add the framework as a dependency in your Package.swift file. ```swift // Package.swift import PackageDescription let package = Package( name: "MyApp", platforms: [ .iOS(.v12) ], dependencies: [ .package(url: "https://github.com/dimayurkovski/AdSearchKit.git", from: "1.1.0") ], targets: [ .target( name: "MyApp", dependencies: ["AdSearchKit"] ) ] ) ``` -------------------------------- ### Retrieve Attribution Token Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Get the raw attribution token from AdServices framework, valid for 24 hours. Useful for server-side attribution. Requires iOS 14.3+. ```swift import AdSearchKit // Retrieve the attribution token directly (iOS 14.3+) if let token = AdSearch.attributionToken() { print("Attribution token retrieved successfully") print("Token length: \(token.count) characters") // Send token to your server for server-side attribution // POST to https://api-adservices.apple.com/api/v1/ // Content-Type: text/plain // Body: token string sendTokenToServer(token) } else { print("Attribution token unavailable") print("Possible reasons:") print("- Device running iOS < 14.3") print("- AdServices framework not available") } func sendTokenToServer(_ token: String) { // Example: Send to your MMP or analytics server guard let url = URL(string: "https://your-server.com/attribution") else { return } var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("text/plain", forHTTPHeaderField: "Content-Type") request.httpBody = token.data(using: .utf8) URLSession.shared.dataTask(with: request) { data, response, error in // Handle server response }.resume() } ``` -------------------------------- ### Import AdSearchKit Source: https://github.com/dimayurkovski/adsearchkit/blob/main/README.md Include the framework in your Swift source files. ```swift import AdSearchKit ``` -------------------------------- ### Integrate AdSearchKit in AppDelegate Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Demonstrates fetching attribution data during app launch with error handling and local persistence to prevent duplicate requests. ```swift import UIKit import AdSearchKit class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Fetch attribution on app launch fetchAttributionOnLaunch() return true } private func fetchAttributionOnLaunch() { // Check if we've already fetched attribution this install guard !UserDefaults.standard.bool(forKey: "attributionFetched") else { return } AdSearch.attribution { [weak self] result in DispatchQueue.main.async { self?.handleAttributionResult(result) } } } private func handleAttributionResult(_ result: Result) { switch result { case .success(let attribution): // Mark as fetched to avoid duplicate requests UserDefaults.standard.set(true, forKey: "attributionFetched") guard attribution.attribution else { print("Organic install - no ad attribution") trackInstallSource(source: "organic") return } // Skip analytics for sandbox mode guard !attribution.isSandbox else { print("Sandbox mode - skipping analytics") return } // Track attributed install let installData: [String: Any] = [ "campaignId": attribution.campaignId ?? 0, "adGroupId": attribution.adGroupId ?? 0, "keywordId": attribution.keywordId ?? 0, "adId": attribution.adId ?? 0, "creativeSetId": attribution.creativeSetId ?? 0, "countryOrRegion": attribution.countryOrRegion ?? "", "conversionType": attribution.conversionType ?? "", "claimType": attribution.claimType ?? "" ] trackInstallSource(source: "apple_search_ads", data: installData) case .failure(let error): print("Attribution fetch failed: \(error)") // Retry logic or fallback handling } } private func trackInstallSource(source: String, data: [String: Any]? = nil) { // Send to your analytics service print("Install source: \(source)") if let data = data { print("Attribution data: \(data)") } } } ``` -------------------------------- ### Fetch Attribution Data with Async/Await Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Use this async function to fetch attribution data with modern Swift concurrency. Requires iOS 13.0+ or macOS 10.15+. Handles potential AdSearch errors. ```swift import AdSearchKit // Fetch attribution data using async/await (iOS 13.0+) func fetchAttributionData() async { do { let attribution = try await AdSearch.attribution() if attribution.attribution { print("Campaign ID: \(attribution.campaignId ?? 0)") print("Ad Group ID: \(attribution.adGroupId ?? 0)") print("Organization ID: \(attribution.orgId ?? 0)") print("Ad ID: \(attribution.adId ?? 0)") print("Creative Set ID: \(attribution.creativeSetId ?? 0)") // Determine attribution type if attribution.claimType == "Click" { print("Click-through attribution on: \(attribution.clickDate ?? \"N/A\")") } else if attribution.claimType == "Impression" { print("View-through attribution on: \(attribution.impressionDate ?? \"N/A\")") } } } catch let error as AdSearch.AdSearchError { switch error { case .invalidToken: print("Attribution token unavailable - device may be running iOS < 14.3") case .invalidUrl: print("API URL configuration error") case .invalidResponse: print("Failed to parse attribution response") } } catch { print("Unexpected error: \(error)") } } // Call from a Task context Task { await fetchAttributionData() } ``` -------------------------------- ### Fetch Attribution Data with Completion Handler Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Use the AdSearch.attribution method to retrieve campaign data asynchronously via a completion handler. ```swift import AdSearchKit // Fetch attribution data using completion handler AdSearch.attribution { result in switch result { case .success(let attribution): // Check if attribution was successful if attribution.attribution { print("Attribution successful!") print("Campaign ID: \(attribution.campaignId ?? 0)") print("Ad Group ID: \(attribution.adGroupId ?? 0)") print("Keyword ID: \(attribution.keywordId ?? 0)") print("Country/Region: \(attribution.countryOrRegion ?? "Unknown")") print("Conversion Type: \(attribution.conversionType ?? "Unknown")") print("Claim Type: \(attribution.claimType ?? "Unknown")") // Check for click-through attribution date if let clickDate = attribution.clickDate { print("Click Date: \(clickDate)") } // Check for view-through attribution date (available since March 2025) if let impressionDate = attribution.impressionDate { print("Impression Date: \(impressionDate)") } } else { print("No attribution data available") } case .failure(let error): // Handle specific error types if let adSearchError = error as? AdSearch.AdSearchError { switch adSearchError { case .invalidToken: print("Error: Could not retrieve attribution token") case .invalidUrl: print("Error: Invalid API URL") case .invalidResponse: print("Error: Could not parse API response") } } else { print("Network error: \(error.localizedDescription)") } } } ``` -------------------------------- ### Check Framework Version Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Retrieve the current version of the AdSearchKit framework using `AdSearch.version`. Useful for logging and compatibility checks. ```swift import AdSearchKit // Get the current framework version let version = AdSearch.version print("AdSearchKit version: \(version)") // Output: "AdSearchKit version: 1.2" // Use in analytics or debugging func logAttributionAttempt() { print("Attempting attribution fetch with AdSearchKit v\(AdSearch.version)") } ``` -------------------------------- ### Retrieve Attribution Data Source: https://github.com/dimayurkovski/adsearchkit/blob/main/README.md Use the attribution method to fetch campaign data, handling success and failure cases via a result enum. ```swift AdSearch.attribution { attribution in switch attribution { case .success(let attribution): print("Attribution Data: \(attribution)") case .failure(let error): print("Error: \(error)") } } ``` -------------------------------- ### Detect Sandbox Mode Source: https://context7.com/dimayurkovski/adsearchkit/llms.txt Check if attribution data originates from Apple's sandbox testing environment using `Attribution.isSandbox`. The sandbox organization ID is `1234567890`. ```swift import AdSearchKit AdSearch.attribution { result in switch result { case .success(let attribution): if attribution.isSandbox { print("Running in sandbox mode") print("Sandbox Org ID: \(AdSearch.Attribution.sandboxOrgId)") // Use test/debug analytics configuration } else { print("Production attribution data") print("Organization ID: \(attribution.orgId ?? 0)") // Send to production analytics } case .failure(let error): print("Error: \(error)") } } // Check sandbox org ID constant let sandboxId = AdSearch.Attribution.sandboxOrgId // Returns 1234567890 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.