Instantiate Storyboard from local Swift Package - ios

I try to instatiat a ViewController vom a local SPM Package but I always get the error "Could not find a storyboard".
let myViewController = UIStoryboard(
name: "MyStoryboard",
bundle: Bundle(
url: Bundle.main.url(
forResource: "myModule_myModule",
withExtension: "bundle"
)!
)
).instantiateViewController(withIdentifier: "MyViewController")
This code works when using it as an remote package, but not when dragging it to Xcode and use it local.
Thanks Bernhard

Unlike Remote Packages, When adding a Local one Xcode doesn't prompt you to add it to a target. Hence you need to add it manually in your Target's General settings, under Frameworks, Libraries, and Embedded Content

Related

Image from bundle loads ok in SwiftUI but not in UIKit

I have developed a framework that consists of some colors in the asset catalogue. I packaged it in the cocoapod and integrated in another project where I try to access the assets from the framework. Everything works correctly as long as I'm in the SwiftUI domain, if I try to do exactly the same thing in UIKit, suddenly the images are no longer available (or at least this is what the UIImage constructor claims).
func testAssetSDKBundleAccess() {
let bundle = Assets.bundle
XCTAssertNotNil(bundle, "The Assets.bundle should not be nil")
XCTAssertNotNil(Image("Switzerland", bundle: bundle))
XCTAssertNotNil(UIImage(named: "Switzerland", in: bundle, compatibleWith: nil)) <- XCTAssertNotNil failed
}
When I run the same test case in my asset framework - it passes fine. Looks like something UIKit specific, but it got me puzzled for a couple of days now and I can't figure it out.
Xcode 13.2.1, Swift version 5
This turned out to be a problem connected with cocoapods. My assets framework exposes it's bundle with Assets.bundle and inside there you have to check what is available to you - when building from source you access the bundle in a different way than when using xcframework. So, our bundle access code tried one way, when it failed it tried the other. We changed the order and suddenly it started working - I don't understand why it works now but I take it. The correct order below:
public static let bundle: Bundle = {
let bundleName = "MyFramework"
/* Bundle should be present here when linked as pod dependency. */
let podBundle = Bundle(for: MyFramework.self)
if let path = podBundle.path(forResource: bundleName, ofType: "bundle"), let bundle = Bundle(path: path) {
return bundle
}
/* Bundle should be present here when running local tests or linked as xcframework. */
if let bundle = Bundle(identifier: "com.app.example.\(bundleName)") {
return bundle
}
fatalError("Unable to load bundle named \(bundleName)")
}()

Not able to access pod swift file, after installing pod

I have created a pod using the raywenderlich tutroial link, everything works fine
creating
installing and
importing the pod too
but when I am trying to access the view controller its says
use of unresolved identifier 'file name'
I tried cmd+click to jump to defination of the file and it navigating me to the file also
code implemented
let bundle = Bundle(for: SDKSplashVC.self) // error here - Use of unresolved identifier 'SDKSplashVC'
let storyboard = UIStoryboard(name: "SDKSplash", bundle: myBundle)
let controller = storyboard.instantiateViewController(withIdentifier: "SDKSplashVC")
self.present(controller, animated: true, completion: nil)
I have imported the pod, crossed check in managed scheme and build phases, my custom pod is listed their but still error persist
Any help would be really helpful.
Thank you
In the tutorial author is using class PickFlavorViewController which is defined in RWPickFlavor cocoa pod.
let bundle = Bundle(for: PickFlavorViewController.self)
You can take a look and see all the files in this pod here.
Docs say that you can use the init(for:) initializer of Bundle to locate the framework bundle based on a class defined in that framework.
You are getting the error because SDKSplashVC is not defined. You need to write something like this:
import UIKit
public class SDKSplashVC: UIViewController {}
Make sure the class is defined as public.

Add Playground to a Storyboard project: Could not find a storyboard ... in bundle NSBundle

I have a working iOS project with Storyboards. Then I added a playground file to it with following content:
The
import UIKit
import PlaygroundSupport
import SpriteKit
var str = "Hello, playground"
let gameViewController = GameViewController.loadFromStoryboard() // <<<<- Fails
PlaygroundPage.current.liveView = gameViewController
I am getting following error at line, where I loadFromStoryboard():
NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle
What should I fix?
There are tonnes of questions on stackoverflow with the above title/contents however, each has a very specific scenario. I have gone through many of them over past few days and I think that my scenario is distinct due to use of Playground hence I am posting again.
If this is in an existing project, you can instantiate the storyboard by using your full bundle identifier, as demonstrated here:
let myBundle = Bundle(identifier: "com.asad.MyProgram")
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: myBundle)
Please note that this does require that the playground is in the same workspace as the project with that particular bundle identifier.

Cannot find (secondary) storyboard named Test.storyboard

I would like to load a secondary storyboard Test.storyboard (next to Main.storyboard) in my app like so:
let storyboard = UIStoryboard(name: "Test.storyboard", bundle: nil)
Test.storyboard resides inside a group Test, it is marked for localization as Base (hence its Location Relative to Group is reported as Base.lproj/Test.storyboard) and it also marked for target membership in my app target. When I run the app on the simulator, I can confirm that a file Base.lproj/Test.storyboardc exists inside the app's main bundle.
Yet the above code produces this error:
Could not find a storyboard named 'Test.storyboard' in bundle ...
What is the right way to successfully package and load a secondary storyboard like mine?
You don't need to load storyboard with .storyboard extension .use like this
let storyboard = UIStoryboard(name: "Test", bundle: nil)

Cannot resolve identifier from Cocoapod Xcode 7.2.1 Swift 2.2

I've been following this tutorial religiously which is verified for Swift 2.0 and Xcode 7.1 (as per the title I'm using Xcode 7.2.1 Swift 2.2), so I get stuck on the article "Using Your New CocoaPod" where you've implement it. I followed it along precisely, and my app delegate look like this:
import UIKit
import RWPickFlavor
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController: UIViewController!
func application(application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [NSObject : AnyObject]?) -> Bool {
setupRootViewController()
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = rootViewController
window?.makeKeyAndVisible()
return true
}
func setupRootViewController() {
let bundle = NSBundle(forClass: PickFlavorViewController.self)
let storyboard = UIStoryboard(name: "Main", bundle: bundle)
rootViewController = storyboard.instantiateInitialViewController()!
}
}
but I have an error on the following line:
let bundle = NSBundle(forClass: PickFlavorViewController.self)
The error message reads: "Use of unresolved identifier 'PickFlavorViewController'"
Has something happened in between swift 2.0 and 2.2 affecting the CocoaPods and how can I resolve it?
It looks like you didn't copy the PickFlavorViewController into your project, which is mentioned in your tutorial:
Now drag and drop everything — except for AppDelegate.swift and LaunchScreen.xib — from the above groups in IceCreamShop.xcworkspace into the corresponding groups in RWPickFlavor.xcworkspace like this:
Copy the controller and all should be fine. Good luck!
The Beginner's guide to Making your CocoaPods
The issue I had here, despite following the tutorial to the spot was that when I copied the files over, they were moved into the root folder of my Swift cocoa pod project folder in my filesystem. This is why I go the error message that my class couldn't be found.
NOTE!
Just because you made a group in your xcode project it does not mean that Xcode will make any kind of folder architecture in regards to that.
Xcode's project structure is a pointer-based system that points to a file no matter where it is in your computer's file system. Moving it in Xcode does not move it in the folder!
In my case the files were copied into the root folder and per default the pod with look into the folder named like your project that's located in the root folder, but it won't see what's in your root and therefor not find the files when you use the pod.
How do I fix it?
In your root folder (the one where you can find the RWPickFlavor.xcworkspace) of your pod project you have a folder with the same name as your project. Move all your swift files into that folder.
Update your pod to accomondate your changes:
git add .
git commit -m 'Refactored the files (Or whichever commit message you want here)'
git tag 0.2.0
git push origin master --tags
pod repo push RWPodSpec RWPickFlavor.podspec
And in your Ice Cream Shop project root (Once again, where you find the [ProjectName].xcworkspace) run a pod install with your updated pod.
Some of these pod steps may be unnecessary, but this is what I did to solve my problem.

Resources