Skip to content

Commit 13a3a3d

Browse files
committed
Improve app loading
1 parent 9165e9f commit 13a3a3d

1 file changed

Lines changed: 19 additions & 33 deletions

File tree

Home Screen Layout/ApplicationsHelper.swift

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import Foundation
33
import UIKit
44

5-
let APP_PATH_SYSTEM = "/Applications"
6-
let APP_PATH = "/var/containers/Bundle/Application"
75
let APP_ICON_CACHE = "/private/var/mobile/Library/Caches/com.apple.HeadBoard/AppIconCache"
86

97
class ApplicationsHelper {
@@ -21,38 +19,26 @@ class ApplicationsHelper {
2119
}
2220

2321
private func loadApplications() {
24-
let applicationPaths = [APP_PATH_SYSTEM, APP_PATH]
25-
26-
for path in applicationPaths {
27-
do {
28-
let contents = try fm.contentsOfDirectory(atPath: path)
29-
for item in contents {
30-
let fullPath = "\(path)/\(item)"
31-
if fm.fileExists(atPath: fullPath, isDirectory: nil) {
32-
if item.hasSuffix(".app") {
33-
let appPath = fullPath
34-
if let (bundleIdentifier, appName) = getBundleIdentifierAndName(from: appPath) {
35-
bundleIdToName[bundleIdentifier] = appName
36-
bundleIdToPath[bundleIdentifier] = appPath
37-
}
38-
} else {
39-
if let appContents = try? fm.contentsOfDirectory(atPath: fullPath) {
40-
for appItem in appContents {
41-
if appItem.hasSuffix(".app") {
42-
let appPath = "\(fullPath)/\(appItem)"
43-
if let (bundleIdentifier, appName) = getBundleIdentifierAndName(from: appPath) {
44-
bundleIdToName[bundleIdentifier] = appName
45-
bundleIdToPath[bundleIdentifier] = appPath
46-
}
47-
}
48-
}
49-
}
50-
}
51-
}
52-
}
53-
} catch {
54-
print("Error reading contents of directory \(path): \(error)")
22+
guard let workspaceClass = NSClassFromString("LSApplicationWorkspace") as AnyObject?,
23+
let defaultWorkspace = workspaceClass.perform(NSSelectorFromString("defaultWorkspace"))?.takeUnretainedValue(),
24+
let apps = defaultWorkspace.perform(NSSelectorFromString("allApplications"))?.takeUnretainedValue() as? [AnyObject] else {
25+
return
26+
}
27+
28+
for app in apps {
29+
guard let bundleIdentifier = app.perform(NSSelectorFromString("bundleIdentifier"))?.takeUnretainedValue() as? String,
30+
let bundleURL = app.perform(NSSelectorFromString("bundleURL"))?.takeUnretainedValue() as? URL else {
31+
continue
5532
}
33+
34+
let name = app.perform(NSSelectorFromString("localizedName"))?.takeUnretainedValue() as? String
35+
?? app.perform(NSSelectorFromString("bundleName"))?.takeUnretainedValue() as? String
36+
37+
if let name = name, !name.isEmpty {
38+
bundleIdToName[bundleIdentifier] = name
39+
}
40+
41+
bundleIdToPath[bundleIdentifier] = bundleURL.path
5642
}
5743
}
5844

0 commit comments

Comments
 (0)