@@ -222,11 +222,11 @@ - (NSArray*)readDefaultsAsArray {
222222 } else {
223223 dServerIndex = [NSNumber numberWithInteger: -1 ];
224224 }
225- // NSLog(@"local port:%@, udp:%@, profiles:%@",dLocalPort, dUdpSupport, dProfiles);
226225 return @[dProxyState,dMode,dLocalPort,dUdpSupport,dProfiles,dServerIndex];
227226}
228227
229228- (void )readDefaults {
229+ /*
230230 NSArray *defaultsArray = [self readDefaultsAsArray];
231231 proxyIsOn = [defaultsArray[0] boolValue];
232232 isAuto = [defaultsArray[1] boolValue];
@@ -235,9 +235,116 @@ - (void)readDefaults {
235235 [profiles removeAllObjects]; //Maybe unnecessary
236236 profiles = defaultsArray[4];
237237 selectedServerIndex = [defaultsArray[5] integerValue];
238+
239+
240+ return @{@"proxyState": dProxyState,
241+ @"mode": dMode,
242+ @"localPort": dLocalPort,
243+ @"udpSupport": dUdpSupport,
244+ @"profiles": dProfiles,
245+ @"serverIndex": dServerIndex };
246+
247+
248+ */
249+ NSDictionary *defaultsDic = [self readDefaultsAsDictionary ];
250+ proxyIsOn = [defaultsDic[@" proxyState" ] boolValue ];
251+ isAuto = [defaultsDic[@" mode" ] boolValue ];
252+ localPort = [defaultsDic[@" localPort" ] integerValue ];
253+ udpSupport = [defaultsDic[@" udpSupport" ] integerValue ];
254+ [profiles removeAllObjects ];
255+ profiles = defaultsDic[@" profiles" ];
256+ selectedServerIndex = [defaultsDic[@" serverIndex" ] integerValue ];
238257 NSLog (@" read %ld profiles, selected No.%ld " , [profiles count ] , selectedServerIndex);
239258}
240259
260+ - (NSDictionary *)readDefaultsAsDictionary {
261+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults ];
262+ NSNumber *dProxyState = [defaults objectForKey: @" proxyIsOn" ];
263+ if (dProxyState == nil ) {
264+ dProxyState = [NSNumber numberWithBool: YES ];// turn on proxy as default
265+ }
266+ NSNumber *dMode = [defaults objectForKey: @" isAuto" ];
267+ if (dMode == nil ) {
268+ dMode = [NSNumber numberWithBool: YES ];// use auto mode as default
269+ }
270+ NSNumber * dLocalPort = [defaults objectForKey: @" localPort" ];
271+ if (dLocalPort == nil ) {
272+ dLocalPort = [NSNumber numberWithInteger: 1081 ];// use 1081 as default local port
273+ }
274+ NSNumber * dUdpSupport = [defaults objectForKey: @" udpSupport" ];
275+ if (dUdpSupport == nil ) {
276+ dUdpSupport = [NSNumber numberWithBool: NO ];// do not support udp as default
277+ }
278+ NSMutableArray *dProfilesInPlist = [defaults objectForKey: @" profiles" ];
279+ NSMutableArray *dProfiles = [[NSMutableArray alloc ] init ];
280+ NSNumber *dServerIndex;
281+ if ([dProfilesInPlist isKindOfClass: [NSArray class ]] && [dProfilesInPlist count ] > 0 ) {
282+ for (NSDictionary *aProfile in dProfilesInPlist) {
283+
284+ ServerProfile *newProfile = [[ServerProfile alloc ] init ];
285+ [newProfile setAddress: aProfile[@" address" ]];
286+ [newProfile setPort: [aProfile[@" port" ] integerValue ]];
287+ [newProfile setUserId: aProfile[@" userId" ]];
288+ [newProfile setAlterId: [aProfile[@" alterId" ] integerValue ]];
289+ [newProfile setRemark: aProfile[@" remark" ]];
290+ [dProfiles addObject: newProfile];
291+ }
292+ dServerIndex = [defaults objectForKey: @" selectedServerIndex" ];
293+ if ([dServerIndex integerValue ] <= 0 || [dServerIndex integerValue ] >= [dProfiles count ]) {
294+ // "<= 0" also includes the case where dServerIndex is nil
295+ dServerIndex = [NSNumber numberWithInteger: 0 ]; // treate illeagle selectedServerIndex value
296+ }
297+ } else {
298+ dServerIndex = [NSNumber numberWithInteger: -1 ];
299+ }
300+ // return @[dProxyState,dMode,dLocalPort,dUdpSupport,dProfiles,dServerIndex];
301+ return @{@" proxyState" : dProxyState,
302+ @" mode" : dMode,
303+ @" localPort" : dLocalPort,
304+ @" udpSupport" : dUdpSupport,
305+ @" profiles" : dProfiles,
306+ @" serverIndex" : dServerIndex };
307+ }
308+ /*
309+ - (void)readDefaultsAsDictionary {
310+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
311+
312+ NSNumber *dProxyState = [defaults objectForKey:@"proxyIsOn"];
313+ proxyIsOn = dProxyState == nil ? YES : [dProxyState boolValue];//turn on proxy as default
314+
315+ NSNumber *dMode = [defaults objectForKey:@"isAuto"];
316+ isAuto = dMode == nil ? YES : [dMode boolValue];//use auto mode as default
317+
318+ NSNumber* dLocalPort = [defaults objectForKey:@"localPort"];
319+ localPort = dLocalPort == nil ? 1081 : [dLocalPort integerValue];//use 1081 as default local port
320+
321+ NSNumber* dUdpSupport = [defaults objectForKey:@"udpSupport"];
322+ udpSupport = dUdpSupport == nil ? NO : [dUdpSupport boolValue];// do not support udp as default
323+
324+
325+ [profiles removeAllObjects]; //Maybe unnecessary
326+ NSMutableArray *dProfilesInPlist = [defaults objectForKey:@"profiles"];
327+ if ([dProfilesInPlist isKindOfClass:[NSArray class]] && [dProfilesInPlist count] > 0) {
328+ for (NSDictionary *aProfile in dProfilesInPlist) {
329+ ServerProfile *newProfile = [[ServerProfile alloc] init];
330+ [newProfile setAddress:aProfile[@"address"]];
331+ [newProfile setPort:[aProfile[@"port"] integerValue]];
332+ [newProfile setUserId:aProfile[@"userId"]];
333+ [newProfile setAlterId:[aProfile[@"alterId"] integerValue]];
334+ [newProfile setRemark:aProfile[@"remark"]];
335+ [profiles addObject:newProfile];
336+ }
337+ selectedServerIndex = [[defaults objectForKey:@"selectedServerIndex"] integerValue];
338+ if (selectedServerIndex <= 0 || selectedServerIndex >= [profiles count]) {
339+ // "<= 0" also includes the case where dServerIndex is nil
340+ selectedServerIndex = 0; // treate illeagle selectedServerIndex value
341+ }
342+ } else {
343+ selectedServerIndex = -1;
344+ }
345+ }*/
346+
347+
241348-(BOOL )reloadV2ray {
242349 runCommandLine (@" /bin/launchctl" , @[@" unload" , plistPath]);
243350 NSLog (@" V2Ray core unloaded." );
@@ -374,7 +481,7 @@ - (BOOL)isSysconfVersionOK {
374481
375482-(void )configurationDidChange {
376483 [self readDefaults ];
377- // NSLog(@"profiles = %ld, just after read.",[profiles count]);
484+ NSLog (@" profiles = %ld , just after read." ,[profiles count ]);
378485 if (![self reloadV2ray ]) {
379486 proxyIsOn = NO ;
380487 [[NSUserDefaults standardUserDefaults ] setValue: [NSNumber numberWithBool: NO ] forKey: @" proxyIsOn" ];
0 commit comments