Skip to content

Commit

Permalink
Fixed a bug where Navigation would behave strangely when there were m…
Browse files Browse the repository at this point in the history
…ultiple UIWindowScenes.
  • Loading branch information
Kyome22 committed Sep 20, 2023
1 parent 2fe7ef0 commit 60f2bab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
30 changes: 15 additions & 15 deletions LicenseDemo/LicenseDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
18AC730228EBEE2800E64B96 /* LicenseList in Frameworks */ = {isa = PBXBuildFile; productRef = 18AC730128EBEE2800E64B96 /* LicenseList */; };
18780F9B2ABABB16007B286A /* LicenseList in Frameworks */ = {isa = PBXBuildFile; productRef = 18780F9A2ABABB16007B286A /* LicenseList */; };
18780F9D2ABABB1C007B286A /* LicenseList in Frameworks */ = {isa = PBXBuildFile; productRef = 18780F9C2ABABB1C007B286A /* LicenseList */; };
18E61A722872A66800C5A710 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18E61A712872A66800C5A710 /* AppDelegate.swift */; };
18E61A742872A66800C5A710 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18E61A732872A66800C5A710 /* SceneDelegate.swift */; };
18E61A762872A66800C5A710 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18E61A752872A66800C5A710 /* ViewController.swift */; };
Expand All @@ -18,10 +19,10 @@
18E61A962872AC5100C5A710 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18E61A952872AC5100C5A710 /* ContentView.swift */; };
18E61A982872AC5300C5A710 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18E61A972872AC5300C5A710 /* Assets.xcassets */; };
18E61A9B2872AC5300C5A710 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 18E61A9A2872AC5300C5A710 /* Preview Assets.xcassets */; };
18E61AA42872B13500C5A710 /* LicenseList in Frameworks */ = {isa = PBXBuildFile; productRef = 18E61AA32872B13500C5A710 /* LicenseList */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
18780F992ABABB07007B286A /* LicenseList */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = LicenseList; path = ..; sourceTree = "<group>"; };
18E61A6F2872A66800C5A710 /* LicenseDemoForUIKit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LicenseDemoForUIKit.app; sourceTree = BUILT_PRODUCTS_DIR; };
18E61A712872A66800C5A710 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
18E61A732872A66800C5A710 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -30,7 +31,6 @@
18E61A7A2872A66900C5A710 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
18E61A7D2872A66900C5A710 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
18E61A7F2872A66900C5A710 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
18E61A892872AB6100C5A710 /* LicenseList */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = LicenseList; path = ..; sourceTree = "<group>"; };
18E61A912872AC5100C5A710 /* Demo SwiftUI.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Demo SwiftUI.app"; sourceTree = BUILT_PRODUCTS_DIR; };
18E61A932872AC5100C5A710 /* LicenseDemoForSwiftUIApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LicenseDemoForSwiftUIApp.swift; sourceTree = "<group>"; };
18E61A952872AC5100C5A710 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand All @@ -43,15 +43,15 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
18AC730228EBEE2800E64B96 /* LicenseList in Frameworks */,
18780F9B2ABABB16007B286A /* LicenseList in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
18E61A8E2872AC5100C5A710 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
18E61AA42872B13500C5A710 /* LicenseList in Frameworks */,
18780F9D2ABABB1C007B286A /* LicenseList in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -61,7 +61,7 @@
181DFF4C2849FA1800B97329 = {
isa = PBXGroup;
children = (
181F97F7286C0F45003D756F /* Packages */,
18780F982ABABB07007B286A /* Packages */,
18E61A702872A66800C5A710 /* LicenseDemoForUIKit */,
18E61A922872AC5100C5A710 /* LicenseDemoForSwiftUI */,
181DFF562849FA1800B97329 /* Products */,
Expand All @@ -78,10 +78,10 @@
name = Products;
sourceTree = "<group>";
};
181F97F7286C0F45003D756F /* Packages */ = {
18780F982ABABB07007B286A /* Packages */ = {
isa = PBXGroup;
children = (
18E61A892872AB6100C5A710 /* LicenseList */,
18780F992ABABB07007B286A /* LicenseList */,
);
name = Packages;
sourceTree = "<group>";
Expand Down Expand Up @@ -144,7 +144,7 @@
);
name = LicenseDemoForUIKit;
packageProductDependencies = (
18AC730128EBEE2800E64B96 /* LicenseList */,
18780F9A2ABABB16007B286A /* LicenseList */,
);
productName = LicenseDemoForUIKit;
productReference = 18E61A6F2872A66800C5A710 /* LicenseDemoForUIKit.app */;
Expand All @@ -165,7 +165,7 @@
);
name = LicenseDemoForSwiftUI;
packageProductDependencies = (
18E61AA32872B13500C5A710 /* LicenseList */,
18780F9C2ABABB1C007B286A /* LicenseList */,
);
productName = LicenseDemoForSwiftUI;
productReference = 18E61A912872AC5100C5A710 /* Demo SwiftUI.app */;
Expand Down Expand Up @@ -600,7 +600,11 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
18AC730128EBEE2800E64B96 /* LicenseList */ = {
18780F9A2ABABB16007B286A /* LicenseList */ = {
isa = XCSwiftPackageProductDependency;
productName = LicenseList;
};
18780F9C2ABABB1C007B286A /* LicenseList */ = {
isa = XCSwiftPackageProductDependency;
productName = LicenseList;
};
Expand All @@ -612,10 +616,6 @@
isa = XCSwiftPackageProductDependency;
productName = "plugin:PrepareLicenseList";
};
18E61AA32872B13500C5A710 /* LicenseList */ = {
isa = XCSwiftPackageProductDependency;
productName = LicenseList;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 181DFF4D2849FA1800B97329 /* Project object */;
Expand Down
2 changes: 1 addition & 1 deletion LicenseDemo/LicenseDemoForUIKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<true/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
Expand Down
15 changes: 11 additions & 4 deletions Sources/LicenseList/LicenseListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import SwiftUI
public struct LicenseListView: View {
let libraries: [Library]
let useUINavigationController: Bool
let id: UUID?

public init(fileURL: URL, useUINavigationController: Bool = false) {
public init(fileURL: URL, useUINavigationController: Bool = false, id: UUID? = nil) {
self.useUINavigationController = useUINavigationController
self.id = id
guard let data = try? Data(contentsOf: fileURL),
let plist = try? PropertyListSerialization.propertyList(from: data, format: nil),
let dict = plist as? [String: Any] else {
Expand Down Expand Up @@ -49,11 +51,16 @@ public struct LicenseListView: View {
}

var navigationController: UINavigationController? {
guard let scene = UIApplication.shared.connectedScenes.first,
let sceneDelegate = scene as? UIWindowScene,
var controller = sceneDelegate.windows.first?.rootViewController else {
guard let id else { return nil }
let windowScenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
let viewControllers = windowScenes
.flatMap { $0.windows }
.flatMap { $0.rootViewController?.children ?? [] }
.compactMap { $0 as? LicenseListViewController }
guard let viewController = viewControllers.first(where: { $0.id == id }) else {
return nil
}
var controller: UIViewController = viewController
while true {
if let navigationController = controller as? UINavigationController,
let visibleViewController = navigationController.visibleViewController {
Expand Down
4 changes: 3 additions & 1 deletion Sources/LicenseList/LicenseListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import SwiftUI

public class LicenseListViewController: UIViewController {
let fileURL: URL
let id: UUID

public init(fileURL: URL) {
self.fileURL = fileURL
self.id = UUID()
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -30,7 +32,7 @@ public class LicenseListViewController: UIViewController {
public override func viewDidLoad() {
super.viewDidLoad()

let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true)
let licenseListView = LicenseListView(fileURL: fileURL, useUINavigationController: true, id: id)
let vc = UIHostingController(rootView: licenseListView)
self.addChild(vc)
self.view.addSubview(vc.view)
Expand Down

0 comments on commit 60f2bab

Please sign in to comment.