Skip to content

Commit

Permalink
Fix crash opening URL
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed May 8, 2021
1 parent cf6e22e commit f41ac3a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
13 changes: 8 additions & 5 deletions MobileCelestia/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import AppCenterAnalytics
import AppCenterCrashes

let newURLOpenedNotificationName = Notification.Name("NewURLOpenedNotificationName")
let newURLOpenedNotificationIsExternalKey = Notification.Name("NewURLOpenedNotificationURLIsExternalKey")
let newURLOpenedNotificationURLKey = "NewURLOpenedNotificationURLKey"
let showHelpNotificationName = Notification.Name("ShowHelpNotificationName")
let showPreferencesNotificationName = Notification.Name("ShowPreferencesNotificationName")
Expand Down Expand Up @@ -75,7 +74,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
if #available(iOS 13.0, *) { return true }

window = UIWindow()
let vc = MainViewController(initialURL: launchOptions?[.url] as? URL)

var launchURL: UniformedURL?
if let url = launchOptions?[.url] as? URL {
launchURL = UniformedURL(url: url, securityScoped: url.isFileURL)
}
let vc = MainViewController(initialURL: launchURL)

window?.rootViewController = vc
window?.makeKeyAndVisible()
Expand All @@ -85,8 +89,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
NotificationCenter.default.post(name: newURLOpenedNotificationName, object: nil, userInfo: [
newURLOpenedNotificationURLKey: url,
newURLOpenedNotificationIsExternalKey: options[.openInPlace] as? Bool == true
newURLOpenedNotificationURLKey: UniformedURL(url: url, securityScoped: url.isFileURL && options[.openInPlace] as? Bool == true),
])
return true
}
Expand Down Expand Up @@ -136,7 +139,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Make request to the server to resolve the URL
let requestURL = apiPrefix + "/resolve"
_ = RequestHandler.get(url: requestURL, parameters: ["path" : path, "id" : id], success: { (response: Response) in
NotificationCenter.default.post(name: newURLOpenedNotificationName, object: nil, userInfo: [newURLOpenedNotificationURLKey : response.resolvedURL])
NotificationCenter.default.post(name: newURLOpenedNotificationName, object: nil, userInfo: [newURLOpenedNotificationURLKey: UniformedURL(url: response.resolvedURL, securityScoped: false)])
})
return true
}
Expand Down
9 changes: 4 additions & 5 deletions MobileCelestia/Celestia/CelestiaInteractionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,11 @@ extension CelestiaInteractionController {
core.keyUp(with: input, modifiers: modifiers)
}

func openURL(_ url: URL, external: Bool) {
if url.isFileURL {
let uniformed = UniformedURL(url: url, securityScoped: external)
core.runScript(at: uniformed.url.path)
func openURL(_ url: UniformedURL) {
if url.url.isFileURL {
core.runScript(at: url.url.path)
} else {
core.go(to: url.absoluteString)
core.go(to: url.url.absoluteString)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions MobileCelestia/Celestia/CelestiaViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ extension CelestiaViewController {
}
}

func openURL(_ url: URL, external: Bool) {
interactionController?.openURL(url, external: external)
func openURL(_ url: UniformedURL) {
interactionController?.openURL(url)
}

func moveToNewScreen(_ newScreen: UIScreen) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion MobileCelestia/Favorite/FavoriteItemViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ extension CelestiaScript: FavoriteItem {
typealias Representation = URL

var associatedObject: URL? {
return URL(fileURLWithPath: FileManager.default.currentDirectoryPath + "/" + filename)
return URL(fileURLWithPath: filename)
}

var isLeaf: Bool {
Expand Down
9 changes: 6 additions & 3 deletions MobileCelestia/MainSceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate {
#endif
let window = UIWindow(windowScene: windowScene)
window.overrideUserInterfaceStyle = .dark
let vc = MainViewController(initialURL: connectionOptions.urlContexts.first?.url)
var launchURL: UniformedURL?
if let url = connectionOptions.urlContexts.first {
launchURL = UniformedURL(url: url.url, securityScoped: url.url.isFileURL && url.options.openInPlace)
}
let vc = MainViewController(initialURL: launchURL)
if let userActivity = connectionOptions.userActivities.first {
AppDelegate.handleUserActivity(userActivity)
}
Expand All @@ -42,8 +46,7 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let urlContext = URLContexts.first else { return }
NotificationCenter.default.post(name: newURLOpenedNotificationName, object: nil, userInfo: [
newURLOpenedNotificationURLKey: urlContext.url,
newURLOpenedNotificationIsExternalKey: urlContext.options.openInPlace
newURLOpenedNotificationURLKey: UniformedURL(url: urlContext.url, securityScoped: urlContext.url.isFileURL && urlContext.options.openInPlace)
])
}

Expand Down
19 changes: 7 additions & 12 deletions MobileCelestia/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ class MainViewController: UIViewController {

private var currentExternalScreen: UIScreen?

private var urlToRun: URL?
private var urlToRunIsExternal: Bool = false
private var urlToRun: UniformedURL?

init(initialURL: URL?) {
init(initialURL: UniformedURL?) {
self.urlToRun = initialURL
super.init(nibName: nil, bundle: nil)
}
Expand Down Expand Up @@ -143,23 +142,21 @@ extension MainViewController {
}

@objc private func newURLOpened(_ notification: Notification) {
guard let url = notification.userInfo?[newURLOpenedNotificationURLKey] as? URL else { return }
urlToRunIsExternal = notification.userInfo?[newURLOpenedNotificationIsExternalKey] as? Bool == true
guard let url = notification.userInfo?[newURLOpenedNotificationURLKey] as? UniformedURL else { return }
urlToRun = url
guard status == .loaded else { return }
checkNeedOpeningURL()
}

private func checkNeedOpeningURL() {
guard let url = urlToRun else { return }
let external = urlToRunIsExternal

urlToRun = nil

let title = url.isFileURL ? CelestiaString("Run script?", comment: "") : CelestiaString("Open URL?", comment: "")
let title = url.url.isFileURL ? CelestiaString("Run script?", comment: "") : CelestiaString("Open URL?", comment: "")
front?.showOption(title) { [unowned self] (confirmed) in
guard confirmed else { return }
self.celestiaController.openURL(url, external: external)
self.celestiaController.openURL(url)
}
}
}
Expand Down Expand Up @@ -214,8 +211,7 @@ extension MainViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }

urlToRun = url
urlToRunIsExternal = true
urlToRun = UniformedURL(url: url, securityScoped: true)
checkNeedOpeningURL()
}
}
Expand Down Expand Up @@ -319,8 +315,7 @@ extension MainViewController: CelestiaControllerDelegate {
private func presentFavorite(_ root: FavoriteRoot) {
let controller = FavoriteCoordinatorController(root: root) { [unowned self] object in
if let url = object as? URL {
urlToRun = url
urlToRunIsExternal = false
urlToRun = UniformedURL(url: url, securityScoped: false)
self.checkNeedOpeningURL()
} else if let destination = object as? CelestiaDestination {
self.core.simulation.goToDestination(destination)
Expand Down

0 comments on commit f41ac3a

Please sign in to comment.