Swift framework for allowing users to manage data stored in iCloud. This project is based on the sample code provided by Apple.
- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 3.0+
- Xcode 10.2+
- Swift 5.0+
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthageTo integrate CloudKitGDPR into your Xcode project using Carthage, specify it in your Cartfile:
github "arturgrigor/CloudKitGDPR" ~> 2.1
Run carthage update to build the framework and drag the built CloudKitGDPR.framework into your Xcode project.
❗️ Please note that since version 1.2 this is a Static Framework and it does not need to be included in the carthage copy-frameworks Build Phase. For more information please consult the Build static frameworks to speed up your app’s launch times section.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsCocoaPods 1.1.0+ is required.
To integrate CloudKitGDPR into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'CloudKitGDPR', '~> 2.1'
endThen, run the following command:
$ pod installThe Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but CloudKitGDPR does support its use on supported platforms.
Once you have your Swift package set up, adding CloudKitGDPR as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [
.Package(url: "https://github.com/arturgrigor/CloudKitGDPR.git", majorVersion: 2)
]import CloudKitGDPR
let defaultContainer = CKContainer.default()
let documents = CKContainer(identifier: "iCloud.com.example.myexampleapp.documents")
let settings = CKContainer(identifier: "iCloud.com.example.myexampleapp.settings")
let metadata: GDPR.RecordTypesByContainer = [
defaultContainer: ["log", "verboseLog"],
documents: ["textDocument", "spreadsheet"],
settings: ["preference", "profile"]
]
let maping: GDPR.ContainerNameMapping = [
defaultContainer: "default",
documents: "docs",
settings: "settings"
]
let gdpr = GDPR(metadata: metadata, containerNameMapping: maping)Export all user's private data as JSON files.
gdpr.exportData(usingTransformer: JSONDataTransformer.default) { result in
switch result {
case .failure(let error):
print("GDPR export data error: \(error)")
case .success(let value):
print("User's private data: \(value)")
}
}Supported transformers
ZeroDataTransformer: This will give you the CloudKit records directly without any other transformation.CSVDataTransformer: This will give you a list of CSV files.JSONDataTransformer: This will give you a list of JSON files.
gdpr.deleteData { result in
switch result {
case .failure(let error):
print("GDPR delete data error: \(error)")
case .success(_):
// TODO: Maybe cleanup the local data too
print("All user's private data deleted.")
}
}Export data as JSON files in a ZIP archive using the ZIPFoundation framework.
import CloudKitGDPR
import ZIPFoundation
lazy var applicationCachesDirectory: URL = {
let urls = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
return urls[urls.count-1]
}()
gdpr.exportData(usingTransformer: JSONDataTransformer.default) { result in
switch result {
case .failure(let error):
print("GDPR export data error: \(error)")
case .success(let value):
DispatchQueue.global(qos: .background).async {
let url = self.applicationCachesDirectory.appendingPathComponent("data.zip")
let archive = Archive(url: url, accessMode: .create)
for (fileName, csvContents) in value {
let data = Data(bytes: Array(csvContents.utf8))
try? archive?.addEntry(with: fileName, type: .file, uncompressedSize: UInt32(data.count), provider: { data[$0..<$0+$1] })
}
DispatchQueue.main.async {
let viewController = UIActivityViewController(activityItems: [url], applicationActivities: [])
viewController.popoverPresentationController?.sourceView = self.exportDataCell
viewController.completionWithItemsHandler = { _, _, _, _ in
try? FileManager.default.removeItem(at: url)
}
self.present(viewController, animated: true, completion: nil)
}
}
}
}- Change the identifier for the
defaultContainerin theGDPR+App.swiftfile to one that's accessible to you. - Replace the
"SomeRecordType"record type in the same file with one that's actually used in that container. - Use the same container identifier for the
com.apple.developer.icloud-container-identifierskey in theDemo.entitlementsfile.
Let me know if you're using or enjoying this product.