Skip to content
DeveloperMemos

Checking When an App Was Installed With the Creation Date of .documentDirectory

iOS, App Installation, Creation Date1 min read

As an iOS developer, you may encounter situations where you need to determine when a user installed your app. This information can be useful for various purposes, such as tracking user engagement or offering personalized experiences. In this article, we will discuss how you can retrieve the installation date of an iOS app using the creation date of the .documentDirectory - which is a somewhat reliable method. By leveraging this technique, you can obtain an approximation of the app's installation date without relying on external services or additional storage.

Retrieving the Creation Date of .documentDirectory

To get the creation date of the .documentDirectory in iOS, we can use the FileManager class provided by the Foundation framework. The following Swift code snippet demonstrates how you can obtain the creation date:

1if let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
2 do {
3 let attributes = try FileManager.default.attributesOfItem(atPath: documentsURL.path)
4
5 if let creationDate = attributes[.creationDate] as? Date {
6 // Use the `creationDate` here
7 print("App installation date: \(creationDate)")
8 }
9 } catch {
10 print("Failed to retrieve creation date: \(error)")
11 }
12}

In the code above, we retrieve the URL of the .documentDirectory using FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first. Then, we obtain the attributes of the directory using FileManager.default.attributesOfItem(atPath: documentsURL.path). Finally, we extract the creation date from the retrieved attributes dictionary and store it in the creationDate variable.

Example Usage

Let's put this knowledge into practice with an example scenario. Suppose you want to display a welcome message to users who have installed your app within the last 7 days. You can use the installation date obtained from the .documentDirectory creation date to determine this. Here's how you can accomplish that:

1func isRecentlyInstalled() -> Bool {
2 if let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
3 do {
4 let attributes = try FileManager.default.attributesOfItem(atPath: documentsURL.path)
5
6 if let creationDate = attributes[.creationDate] as? Date {
7 let calendar = Calendar.current
8 let sevenDaysAgo = calendar.date(byAdding: .day, value: -7, to: Date())
9
10 if let sevenDaysAgo = sevenDaysAgo {
11 return creationDate > sevenDaysAgo
12 }
13 }
14 } catch {
15 print("Failed to retrieve creation date: \(error)")
16 }
17 }
18
19 return false
20}
21
22if isRecentlyInstalled() {
23 print("Welcome, new user!")
24} else {
25 print("Welcome back!")
26}

In the code snippet above, we define the isRecentlyInstalled() function, which checks if the app was installed within the last 7 days. We compare the retrieved installation date with a calculated date that is 7 days ago using Calendar.current.date(byAdding: .day, value: -7, to: Date()). If the installation date is greater than the calculated date, we consider it a recent installation and display the corresponding welcome message.