Not able to access pod swift file, after installing pod - ios

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.

Related

Instantiate Storyboard from local Swift Package

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

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)")
}()

UnitTest in swift | IOS Storyboard | Failed to cast viewcontroller class defined in objective c

So I am trying to write down some test cases for one of my controller class,
My viewcontroller is defined in objective c.
I am writing test cases in swift.
I have included my viewcontroller class in both my main target and my test target.
I have also added the storybaord in both test and main target.
In my test I am trying to load the view controller from story board, below is the code (this code is working fine in my normal project from swift class)
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.init(for: self.classForCoder))
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController")
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
controller = viewController as! LoginViewController
print output
LoginView Controller
__ABT_LoginViewController
Last line of my code is getting failedi.e
controller = viewController as! LoginViewController
error I am getting below
Could not cast value of type '__ABT_LoginViewController' (0x600000053b30) to 'LoginViewController' (0x11e88de30).
Need your help guys
Updated
So above issue resolved after following the answer by #dashandrest, Now I am facing new issue, I have created a TestAppdelegate file for test cases, in my TestAppDelegate class I have this statement import "App-swift.h now after compiling the whole code it is giving an error inside App-swift.h file mentioning cannot import module AppName, My main target module name is AppName whereas test target module name is AppNameTest,
#import "App-swift.h"
Now I am getting compiling error inside App-swift.h file.
#if defined(__has_feature) && __has_feature(modules)
#import ObjectiveC;
#import AppName; ///this line generate compile errror, module AppName not found
#import XCTest;
#endif
Updated
So there were multiple issues in my app which I resolved, definitely #DashAndRest input help me in identifying those, below are the things I correct in order to make things work.
Remove all code files from test target except the test cases.
Make sure that I have separate Module name for my main target and test target.
Make sure that My Pods file does have installing the libraries for my test target also.
Write test cases in Swift for the safe side so that I don't need to include Appname-swift.h in test cases.
Use #testable import AppModuleName (as suggested by #DashAndRest)
This is not required:
3 I have included my viewcontroller class in both my main target and my test target.
4 I have also added the storybaord in both test and main target.
Try unchecking Test target for both view controller and storyboard.
And then in your test file just include main target as:
#testable import Your-Main-Target-Name
Update
Still App-swift.h is confusing.
But if you want to test LoginViewController, here is the solution:
Add Bridging Header to your main AppName target and import all the objective-c classes to it, like:
#ifndef AppName_Bridging_Header_h
#define AppName_Bridging_Header_h
#import "LoginViewController.h"
#endif
After this, your LoginViewController class will be available for all other swift classes to use, including test target.
Add new file like LoginVCTests.swift in test target and add following code to it-
import XCTest
#testable import AppName
class LoginVCTests: XCTestCase {
func testSomeLogic() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as! LoginViewController
print(viewController.classForCoder)
print(type(of: viewController))
let _ = viewController.view
//test on viewController
}
}
Hope this will solve your problem!

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.

Use of undeclared type 'cocoapods library'

I made a pod Here and pushed cocoapods succesfully. When I try to test my library I added
pod 'TDTextSlider'
to my podfile of another test project.It is installed and imported without error but when I try to use the library with this code
let td : TDTextSlider = TDTextSlider(frame: UIScreen.main.bounds)
I got "Use of undeclared type TDTextSlider" error but when I copy the swift.class manually its working.What am i doing wrong?
Make sure the classes you are trying to access are public or Open.

Resources