diff --git a/xcode/Userscripts.xcodeproj/project.pbxproj b/xcode/Userscripts.xcodeproj/project.pbxproj index 1f7f2371..3a703519 100644 --- a/xcode/Userscripts.xcodeproj/project.pbxproj +++ b/xcode/Userscripts.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 030C64F62AC62CC900548FBD /* Settings.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 030C64F52AC62CC900548FBD /* Settings.bundle */; }; + 030C64FC2AD0B24C00548FBD /* Initialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030C64FB2AD0B24C00548FBD /* Initialization.swift */; }; 0336619F294DF7C900CFE179 /* Functions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0336619E294DF7C900CFE179 /* Functions.swift */; }; 033661A529510B7900CFE179 /* View.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 033661A329510B7900CFE179 /* View.storyboard */; }; 0376905B29808EEA00F3474D /* background.js in Resources */ = {isa = PBXBuildFile; fileRef = 0376905A29808EEA00F3474D /* background.js */; }; @@ -106,6 +107,7 @@ /* Begin PBXFileReference section */ 030C64F52AC62CC900548FBD /* Settings.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Settings.bundle; sourceTree = ""; }; + 030C64FB2AD0B24C00548FBD /* Initialization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Initialization.swift; sourceTree = ""; }; 0336619E294DF7C900CFE179 /* Functions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Functions.swift; sourceTree = ""; }; 033661A429510B7900CFE179 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/View.storyboard; sourceTree = ""; }; 0376905A29808EEA00F3474D /* background.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = background.js; sourceTree = ""; }; @@ -210,6 +212,7 @@ 4A4CF6DD270A38BD00111584 /* AppDelegate.swift */, 4A4CF6E7270A38BD00111584 /* SceneDelegate.swift */, 4A4CF6E9270A38BD00111584 /* ViewController.swift */, + 030C64FB2AD0B24C00548FBD /* Initialization.swift */, 4A4CF6EB270A38BD00111584 /* LaunchScreen.storyboard */, 4A4CF6EE270A38BD00111584 /* Main.storyboard */, ); @@ -566,6 +569,7 @@ 4A4CF6EA270A38BD00111584 /* ViewController.swift in Sources */, 4A4CF6DE270A38BD00111584 /* AppDelegate.swift in Sources */, 03C24F992ABD2CBB00F130F9 /* Preferences.swift in Sources */, + 030C64FC2AD0B24C00548FBD /* Initialization.swift in Sources */, 78B25585299D4C27000B2E9B /* Shared.swift in Sources */, 4A4CF6E8270A38BD00111584 /* SceneDelegate.swift in Sources */, ); diff --git a/xcode/iOS-App/AppDelegate.swift b/xcode/iOS-App/AppDelegate.swift index 8bb92da4..1857846c 100644 --- a/xcode/iOS-App/AppDelegate.swift +++ b/xcode/iOS-App/AppDelegate.swift @@ -16,12 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. - let logger = USLogger(#fileID) - // set the scripts directory to the app document on first use - if Preferences.scriptsDirectoryUrl == getDefaultScriptsDirectoryUrl() { - logger?.info("\(#function, privacy: .public) - Initialize default directory") - Preferences.scriptsDirectoryUrl = getDocumentsDirectory() - } + initializeFirstStart() return true } diff --git a/xcode/iOS-App/Initialization.swift b/xcode/iOS-App/Initialization.swift new file mode 100644 index 00000000..394e054e --- /dev/null +++ b/xcode/iOS-App/Initialization.swift @@ -0,0 +1,47 @@ +import Foundation + +private let logger = USLogger(#fileID) + +private func createDemoScript() { + let demoScript = """ +// ==UserScript== +// @name Demo user script +// @description I am a demo user script that you can safely delete (add any files to this folder and I will no longer automatically generate) +// @author Userscripts +// @version 0.0.1 +// @match *://*/* +// @grant none +// @inject-into content +// ==/UserScript== + +(function () { + 'use strict'; + // here is your code +})(); +""" + let url = getDocumentsDirectory() + // get a list of non-hidden files + guard let contents = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: [], options: .skipsHiddenFiles) else { + logger?.error("\(#function, privacy: .public) - failed get contentsOfDirectory") + return + } + if contents.isEmpty { + let fileURL = url.appendingPathComponent("Demo_user_script.user.js") + do { + logger?.info("\(#function, privacy: .public) - try to write demo user script") + try demoScript.write(to: fileURL, atomically: true, encoding: .utf8) + } catch { + logger?.error("\(#function, privacy: .public) - failed to write demo user script") + } + } +} + +func initializeFirstStart() { + // set the scripts directory to the app document on first use + if Preferences.scriptsDirectoryUrl == getDefaultScriptsDirectoryUrl() { + logger?.info("\(#function, privacy: .public) - Initialize default directory") + Preferences.scriptsDirectoryUrl = getDocumentsDirectory() + } + // put a visible file to display the documents directory in files app + createDemoScript() +} diff --git a/xcode/iOS-App/ViewController.swift b/xcode/iOS-App/ViewController.swift index d7734b76..e21c5cbe 100644 --- a/xcode/iOS-App/ViewController.swift +++ b/xcode/iOS-App/ViewController.swift @@ -62,7 +62,7 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan logger?.info("\(#function, privacy: .public) - Userscripts iOS has requested to set the readLocation") let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.folder]) documentPicker.delegate = self - documentPicker.directoryURL = Preferences.scriptsDirectoryUrl + documentPicker.directoryURL = getDocumentsDirectory() present(documentPicker, animated: true, completion: nil) } }