Latest1.0.0
Homepagehttps://github.com/arturgrigor/CloudKitGDPR
LicenseMIT
Platformsios 8.0, osx 10.10, tvos 9.0, watchos 3.0, requires ARC
Authors

Carthage compatible
Build Status
CocoaPods Compatible
Platform
Twitter

Swift framework for allowing users to manage data stored in iCloud. This project is based on the sample code provided by Apple.

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 3.0+
  • Xcode 9.0+
  • Swift 4.1+

Installation

Carthage

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 carthage

To integrate CloudKitGDPR into your Xcode project using Carthage, specify it in your Cartfile:

github "arturgrigor/CloudKitGDPR" ~> 1.0

Run carthage update to build the framework and drag the built CloudKitGDPR.framework into your Xcode project.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 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', '~> 1.0'
end

Then, run the following command:

$ pod install

Swift Package Manager

The 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: 1)
]

Usage

Create the instance

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 Data

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.

Delete All Data

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.")
    }
}

Advanced Usage

iOS

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: { position, size -> Data in
            return data
          })
        }

        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)
        }
      }
  }
}

Notes

iOS Demo Prerequisites

  • Change the identifier for the defaultContainer in the GDPR+App.swift file 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-identifiers key in the Demo.entitlements file.

Contact

Let me know if you’re using or enjoying this product.

Latest podspec

{
    "name": "CloudKitGDPR",
    "version": "1.0.0",
    "license": "MIT",
    "summary": "Framework for allowing users to manage data stored in iCloud.",
    "homepage": "https://github.com/arturgrigor/CloudKitGDPR",
    "social_media_url": "http://twitter.com/arturgrigor",
    "authors": {
        "Artur Grigor": "[email protected]"
    },
    "source": {
        "git": "https://github.com/arturgrigor/CloudKitGDPR.git",
        "tag": "1.0.0"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "3.0"
    },
    "source_files": "Sources/*.swift",
    "requires_arc": true
}

Pin It on Pinterest

Share This