Преглед изворни кода

iOS: move old AppDelegate startup code to SwiftUI

We forgot to setup the default settings and check for network access in the
new UI.
osy пре 4 година
родитељ
комит
e74e9e6bde
4 измењених фајлова са 33 додато и 26 уклоњено
  1. 25 0
      Platform/Main.swift
  2. 7 0
      Platform/UTMData.swift
  3. 0 26
      Platform/iOS/AppDelegate.m
  4. 1 0
      Platform/iOS/ContentView.swift

+ 25 - 0
Platform/Main.swift

@@ -24,6 +24,7 @@ class Main {
     
     static func main() {
         setupLogging()
+        registerDefaultsFromSettingsBundle()
         // check if we have jailbreak
         if jb_has_jit_entitlement() {
             logger.info("JIT: found entitlement")
@@ -56,4 +57,28 @@ class Main {
             ])
         }
     }
+    
+    // https://stackoverflow.com/a/44675628
+    static private func registerDefaultsFromSettingsBundle() {
+        let userDefaults = UserDefaults.standard
+
+        if let settingsURL = Bundle.main.url(forResource: "Root", withExtension: "plist", subdirectory: "Settings.bundle"),
+            let settings = NSDictionary(contentsOf: settingsURL),
+            let preferences = settings["PreferenceSpecifiers"] as? [NSDictionary] {
+
+            var defaultsToRegister = [String: AnyObject]()
+            for prefSpecification in preferences {
+                if let key = prefSpecification["Key"] as? String,
+                    let value = prefSpecification["DefaultValue"] {
+
+                    defaultsToRegister[key] = value as AnyObject
+                    logger.debug("registerDefaultsFromSettingsBundle: (\(key), \(value)) \(type(of: value))")
+                }
+            }
+
+            userDefaults.register(defaults: defaultsToRegister)
+        } else {
+            logger.debug("registerDefaultsFromSettingsBundle: Could not find Settings.bundle")
+        }
+    }
 }

+ 7 - 0
Platform/UTMData.swift

@@ -402,6 +402,13 @@ class UTMData: ObservableObject {
         }
     }
     
+    // MARK: - Networking
+    
+    func enableNetworking() {
+        let task = URLSession.shared.dataTask(with: URL(string: "http://captive.apple.com")!)
+        task.resume()
+    }
+    
     // MARK: - Helper functions
     
     private func recreate(vm: UTMVirtualMachine) {

+ 0 - 26
Platform/iOS/AppDelegate.m

@@ -29,7 +29,6 @@ const NSNotificationName UTMImportNotification = @"UTMImportNotification";
     // Override point for customization after application launch.
     // trigger "allow network usage" popup in some regions
     [[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:@"http://captive.apple.com"]] resume];
-    [self registerDefaultsFromSettingsBundle];
     if (launchOptions[UIApplicationLaunchOptionsURLKey]) {
         self.openURL = launchOptions[UIApplicationLaunchOptionsURLKey];
     }
@@ -48,30 +47,5 @@ const NSNotificationName UTMImportNotification = @"UTMImportNotification";
     return YES;
 }
 
-#pragma - mark NSUserDefaults
-
-- (void)registerDefaultsFromSettingsBundle {
-    // this function writes default settings as settings
-    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
-    if(!settingsBundle) {
-        NSLog(@"Could not find Settings.bundle");
-        return;
-    }
-
-    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
-    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
-
-    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
-    for(NSDictionary *prefSpecification in preferences) {
-        NSString *key = [prefSpecification objectForKey:@"Key"];
-        if(key) {
-            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
-            NSLog(@"writing as default %@ to the key %@",[prefSpecification objectForKey:@"DefaultValue"],key);
-        }
-    }
-
-    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
-}
-
 
 @end

+ 1 - 0
Platform/iOS/ContentView.swift

@@ -62,6 +62,7 @@ struct ContentView: View {
         .onOpenURL(perform: importUTM)
         .onAppear {
             data.refresh()
+            data.enableNetworking()
             IQKeyboardManager.shared.enable = true
             if !Main.jitAvailable {
                 jitAlertPresented.toggle()