diff --git a/EmbedFramework/AdaWebHostViewController.swift b/EmbedFramework/AdaWebHostViewController.swift index 460f7f0..cf806c3 100644 --- a/EmbedFramework/AdaWebHostViewController.swift +++ b/EmbedFramework/AdaWebHostViewController.swift @@ -11,20 +11,8 @@ import WebKit class AdaWebHostViewController: UIViewController { static func createWebController(with webView: WKWebView) -> AdaWebHostViewController { - let bundle = Bundle(for: AdaWebHostViewController.self) - - var storyboard:UIStoryboard - - // Loads the resource_bundle if available (Cocoapod) - if (bundle.path(forResource: "AdaEmbedFramework", ofType: "bundle") != nil){ - let frameworkBundlePath = bundle.path(forResource: "AdaEmbedFramework", ofType: "bundle")! - let frameworkBundle = Bundle(path: frameworkBundlePath) - storyboard = UIStoryboard(name: "AdaWebHostViewController", bundle: frameworkBundle) - } else { - // Used for if SDK was manually imported - storyboard = UIStoryboard(name: "AdaWebHostViewController", bundle: bundle) - } - + let storyboard = UIStoryboard(name: "AdaWebHostViewController", bundle: .ada) + guard let viewController = storyboard.instantiateInitialViewController() as? AdaWebHostViewController else { fatalError("This should never, ever happen.") } viewController.webView = webView return viewController diff --git a/EmbedFramework/Bundle+Ada.swift b/EmbedFramework/Bundle+Ada.swift new file mode 100644 index 0000000..ff1a14d --- /dev/null +++ b/EmbedFramework/Bundle+Ada.swift @@ -0,0 +1,39 @@ +// +// Bundle+Ada.swift +// EmbedFramework +// +// Resolves the bundle that holds AdaEmbedFramework's resources +// (storyboard, asset catalog) across every integration method: +// Swift Package Manager, CocoaPods, Carthage, and manual import. +// + +import Foundation + +extension Bundle { + /// The bundle containing AdaEmbedFramework's resources. + /// + /// - Under SPM, resources live in the generated `Bundle.module`. + /// - Under CocoaPods/Carthage, they live in a nested + /// `AdaEmbedFramework.bundle` inside the framework bundle. + /// - When manually imported, they sit directly in the framework bundle. + static let ada: Bundle = { + #if SWIFT_PACKAGE + return Bundle.module + #else + let moduleName = String(reflecting: BundleToken.self) + .components(separatedBy: ".") + .first + let bundle = Bundle(for: BundleToken.self) + guard + let moduleName = moduleName, + let path = bundle.path(forResource: moduleName, ofType: "bundle"), + let subbundle = Bundle(path: path) + else { return bundle } + return subbundle + #endif + }() +} + +// MARK: - + +private final class BundleToken {} diff --git a/EmbedFramework/OfflineViewController.swift b/EmbedFramework/OfflineViewController.swift index 6e59565..1b19b20 100644 --- a/EmbedFramework/OfflineViewController.swift +++ b/EmbedFramework/OfflineViewController.swift @@ -16,19 +16,7 @@ class OfflineViewController: UIViewController { var retryBlock: (() -> Void)? static func create() -> OfflineViewController? { - let bundle = Bundle(for: OfflineViewController.self) - - var storyboard:UIStoryboard - - // Loads the resource_bundle if available (Cocoapod) - if (bundle.path(forResource: "AdaEmbedFramework", ofType: "bundle") != nil){ - let frameworkBundlePath = bundle.path(forResource: "AdaEmbedFramework", ofType: "bundle")! - let frameworkBundle = Bundle(path: frameworkBundlePath) - storyboard = UIStoryboard(name: "AdaWebHostViewController", bundle: frameworkBundle) - } else { - // Used for if SDK was manually imported - storyboard = UIStoryboard(name: "AdaWebHostViewController", bundle: bundle) - } + let storyboard = UIStoryboard(name: "AdaWebHostViewController", bundle: .ada) return storyboard.instantiateViewController(withIdentifier: "OfflineViewController") as? OfflineViewController } diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..86bfa84 --- /dev/null +++ b/Package.swift @@ -0,0 +1,44 @@ +// swift-tools-version:5.5 +import PackageDescription + +// Generated from AdaEmbedFramework.podspec (version 1.8.0). +// The target is named "AdaEmbedFramework" so it matches the +// customModule="AdaEmbedFramework" references inside the storyboard. +let package = Package( + name: "AdaEmbedFramework", + // podspec declares ios 10.0; SPM resource bundles are only reliable on iOS 11+. + platforms: [ + .iOS(.v11) + ], + products: [ + .library( + name: "AdaEmbedFramework", + targets: ["AdaEmbedFramework"] + ) + ], + targets: [ + .target( + name: "AdaEmbedFramework", + // Mirrors podspec source_files = "EmbedFramework/**/*.swift" + path: "EmbedFramework", + // Non-Swift files that are not part of the shipped resources. + exclude: [ + "Info.plist", + "EmbedFramework.h", + "AdaEmbed.html", + "EmbedTest.html", + "EmbedView.xib", + "OfflineView.xib", + "View.xib", + "en.lproj" + ], + // Mirrors podspec resource_bundles (xcassets + storyboard). + resources: [ + .process("Assets.xcassets"), + .process("AdaWebHostViewController.storyboard") + ] + // Swift 5 language mode (podspec swift_version = '5.0') is the + // default for swift-tools-version:5.5, so no extra setting is needed. + ) + ] +)