Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4cd08ed

Browse filesBrowse files
committed
fix(ios): ApplicationSettings.getAllJSON now uses native method
1 parent a91cd15 commit 4cd08ed
Copy full SHA for 4cd08ed

File tree

Expand file treeCollapse file tree

2 files changed

+89
-32
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+89
-32
lines changed

‎packages/core/application-settings/index.ios.ts

Copy file name to clipboardExpand all lines: packages/core/application-settings/index.ios.ts
+2-32Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,38 +96,8 @@ export function getAllKeys(): Array<string> {
9696
return utils.ios.collections.nsArrayToJSArray(userDefaults.dictionaryRepresentation().allKeys);
9797
}
9898
export function getAllJSON(ignoreRegexp?: string | RegExp): string {
99-
const nsDictionary = userDefaults.dictionaryRepresentation();
100-
const jsonDictionary = NSMutableDictionary.new();
101-
nsDictionary.enumerateKeysAndObjectsUsingBlock((key, value, stop) => {
102-
// we try to filter Apple internal settings. Though some might still be there like AddingEmojiKeybordHandled
103-
if (key.startsWith('AK') || key.startsWith('Apple') || key.startsWith('NS') || key.startsWith('PK') || (ignoreRegexp && key.match(ignoreRegexp))) {
104-
return;
105-
}
106-
107-
let valueClassString = value.classForCoder || value.class ? NSStringFromClass(value.classForCoder?.() ?? value.class?.()) : undefined;
108-
if (valueClassString) {
109-
if (valueClassString.startsWith('__')) {
110-
valueClassString = valueClassString.slice(2);
111-
}
112-
switch (valueClassString) {
113-
case 'NSDate':
114-
jsonDictionary.setObjectForKey(NSISO8601DateFormatter.alloc().init().stringFromDate(value), key);
115-
break;
116-
case 'NSURL':
117-
jsonDictionary.setObjectForKey((value as NSURL).absoluteString, key);
118-
break;
119-
default:
120-
jsonDictionary.setObjectForKey(value, key);
121-
}
122-
} else {
123-
jsonDictionary.setObjectForKey(value, key);
124-
}
125-
});
126-
const jsonData = NSJSONSerialization.dataWithJSONObjectOptionsError(jsonDictionary, 0 as any);
127-
if (jsonData) {
128-
return NSString.alloc().initWithDataEncoding(jsonData, NSUTF8StringEncoding).toString();
129-
}
130-
return null;
99+
//@ts-ignore
100+
return NSApplicationSettings.userDefaultsToJSONStringWithIgnoreRegexp(typeof ignoreRegexp === 'string' ? ignoreRegexp : ignoreRegexp?.toString());
131101
}
132102

133103
export function getNative() {
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
import Foundation
3+
import UIKit
4+
5+
@objcMembers
6+
@objc(NSApplicationSettings)
7+
class NSApplicationSettings : NSObject {
8+
9+
static func userDefaultsToJSONString(ignoreRegexp: String?) -> String? {
10+
let userDefaults = UserDefaults.standard
11+
let nsDictionary = userDefaults.dictionaryRepresentation()
12+
let jsonDictionary = NSMutableDictionary()
13+
14+
var regex: NSRegularExpression?
15+
if let ignoreRegexp = ignoreRegexp {
16+
do {
17+
regex = try NSRegularExpression(pattern: ignoreRegexp)
18+
} catch {
19+
print("Invalid regular expression: \(error.localizedDescription)")
20+
return nil
21+
}
22+
}
23+
24+
nsDictionary.forEach { (key, value) in
25+
guard let key = key as? String else { return }
26+
27+
// Filter out keys that match the ignore criteria
28+
if key.hasPrefix("AK") || key.hasPrefix("Apple") || key.hasPrefix("NS") || key.hasPrefix("PK") {
29+
return
30+
}
31+
32+
// If regex is provided, filter keys that match the regex
33+
if let regex = regex {
34+
let range = NSRange(location: 0, length: key.utf16.count)
35+
if regex.firstMatch(in: key, options: [], range: range) != nil {
36+
return
37+
}
38+
}
39+
40+
var valueClassString: String?
41+
42+
// Obtain the class of the value
43+
if let valueClass = object_getClass(value) {
44+
valueClassString = NSStringFromClass(valueClass)
45+
}
46+
47+
if let valueClassString = valueClassString {
48+
var formattedValueClassString = valueClassString
49+
50+
// Remove '__' prefix if present
51+
if formattedValueClassString.hasPrefix("__") {
52+
formattedValueClassString.removeFirst(2)
53+
}
54+
55+
switch formattedValueClassString {
56+
case "NSDate":
57+
if let dateValue = value as? Date {
58+
let dateFormatter = ISO8601DateFormatter()
59+
jsonDictionary.setObject(dateFormatter.string(from: dateValue), forKey: key as NSString)
60+
}
61+
case "NSURL":
62+
if let urlValue = value as? URL {
63+
jsonDictionary.setObject(urlValue.absoluteString, forKey: key as NSString)
64+
}
65+
default:
66+
jsonDictionary.setObject(value, forKey: key as NSString)
67+
}
68+
} else {
69+
jsonDictionary.setObject(value, forKey: key as NSString)
70+
}
71+
}
72+
73+
// Convert the dictionary to JSON data
74+
do {
75+
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: .prettyPrinted)
76+
77+
// Convert JSON data to string
78+
if let jsonString = String(data: jsonData, encoding: .utf8) {
79+
return jsonString
80+
}
81+
} catch {
82+
print("Failed to serialize dictionary to JSON: \(error.localizedDescription)")
83+
}
84+
85+
return nil
86+
}
87+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.