Search code examples
iosswiftxcodeframeworkscocoapods

Not able to access storyboard and viewcontroller files from private cocoapods framework


I have created my own private framework and using it as a cocoapods in an iOS project and I am able to access all the swift files from a framework but when I tried to show a screen that exists in the framework's storyboard file then I got a crash in an iOS project.

Details are as following:

Error:

2020-06-06 18:02:30.469084+0530 Test[33726:4822657] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x6000024c7300>) doesn't contain a view controller with identifier 'AdditionVC''
*** First throw call stack: 
(   
0   CoreFoundation                      0x0000000106a4c8db __exceptionPreprocess + 331  
1   libobjc.A.dylib    0x0000000104fd7ac5 objc_exception_throw + 48     
2   UIKitCore            0x00000001094206a9 -[UIStoryboard instantiateInitialViewController] + 0    
3   Framework                           0x00000001049dcef6 $s22Framework7ManagerC10additionVCSo16UIViewControllerCyFZ + 262     
4   Test                                0x00000001046d8932 $s17Test14ViewControllerC11viewDidLoadyyF + 178  
5   Test 0x00000001046d8a9b $s17Test14ViewControllerC11viewDidLoadyyFTo + 43    
6 UIKitCore                           0x0000000108c820f7
    -[UIViewController loadViewIfRequired] + 1183   
7   UIKitCore                           0x0000000108be5cf0 -[UINavigationController
    _updateScrollViewFromViewController:toViewController:] + 68     
8   UIKitCore                           0x0000000108be5fe3
    -[UINavigationController _startTransition:fromViewController:toViewController:] + 146   
9   UIKitCore                           0x0000000108be70a1
    -[UINavigationController _startDeferredTransitionIfNeeded:] + 896   
10  UIKitCore                           0x0000000108be8393
    -[UINavigationController __viewWillLayoutSubviews] + 150    
11  UIKitCore                           0x0000000108bc9041
    -[UILayoutContainerView layoutSubviews] + 217   
12  UIKitCore                           0x0000000109763e69 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
    + 1417  
13  QuartzCore                          0x000000010ace6d22 -[CALayer layoutSublayers] + 173     
14  QuartzCore                          0x000000010aceb9fc _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 396  
15  QuartzCore                          0x000000010acf7d58
    _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 72  
16  QuartzCore                          0x000000010ac6724a
    _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 328     
17  QuartzCore                          0x000000010ac9e606
    _ZN2CA11Transaction6commitEv + 610  
18  UIKitCore                           0x000000010929e2c3 __34-[UIApplication
    _firstCommitBlock]_block_invoke_2 + 128     
19  CoreFoundation                      0x00000001069b3cbc __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12     
20  CoreFoundation                      0x00000001069b3480
    __CFRunLoopDoBlocks + 336   
21  CoreFoundation                      0x00000001069add04 __CFRunLoopRun + 1252    
22  CoreFoundation           0x00000001069ad4d2 CFRunLoopRunSpecific + 626  
23  GraphicsServices    0x000000010eab12fe GSEventRunModal + 65     
24  UIKitCore                 0x0000000109284fc2 UIApplicationMain + 140    
25  Test                   0x00000001046d990b main + 75     
26  libdyld.dylib                       0x0000000107de8541 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException

Structure of the framework:

enter image description here

Podspec file

Pod::Spec.new do |spec|

  spec.name         = "Framework"
  spec.version      = "0.0.4"
  spec.summary      = "Framework to give support to main project."
  spec.description  = "Framework is developed to give additional support to any normal user to create their private profile by adding it in main project."
  spec.homepage     = "https://github.com/my/Framework"
  spec.license      = { :type => "MIT", :file => "LICENSE" }
  spec.author       = { "Tushar Amkar" => "[email protected]" }
  spec.platform     = :ios, "12.0"
  spec.source       = { :git => "https://github.com/my/Framework.git", :tag => "#{spec.version}" }
  spec.source_files  = "Framework/Framework/**/*.{swift,otf,storyboard}"
  spec.resource_bundles = {
    'Framework' => ['Framework/Framework/**/*.{storyboard}']
  }
  spec.exclude_files = "Classes/Exclude"
  spec.swift_version = "5.0"

end

Code I have tried to show viewcontroller file which I have got from the different reference websites but not working:

Code 1

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AdditionVC") as! AdditionVC
self.navigationController?.pushViewController(controller, animated: true)

Code 2

let podBundle = Bundle(for: AdditionVC.self)
if let bundleURL = podBundle.url(forResource: "Framework", withExtension: "bundle") {
   if let bundle = Bundle(url: bundleURL) {
      let storyBoard = UIStoryboard.init(name: "Main", bundle: bundle)
      if let vc = storyBoard.instantiateViewController(withIdentifier: "AdditionVC") as? AdditionVC {
          self.navigationController?.pushViewController(vc, animated: true)
      }
   }
}

I have checked in other pods which we are using it currently, so in that it is working fine below is the code (Although it is in objective but I don't think language matters in this case):

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Applozic" bundle:[NSBundle bundleForClass:[self class]]];
ALGroupDetailViewController * groupDetailViewController = (ALGroupDetailViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ALGroupDetailViewController"];
groupDetailViewController.channelKeyID = self.channelKey;
groupDetailViewController.alChatViewController = self;
[self.navigationController pushViewController:groupDetailViewController animated:YES];

Any help will be welcome. Thanks in advance.


Solution

  • After looking at all possible solution, I have tried to change the simulator and it is working fine and I have write the code as simply we are following normally as below:

    let storyboard = UIStoryboard(name: "WodPodMain", bundle: Bundle(for: AdditionVC.self))
    if let vc = storyboard.instantiateViewController(withIdentifier: "AdditionVC") as? AdditionVC{
        self.navigationController?.pushViewController(vc,animated: true)
    }
    

    So the issue totally weird, Simulator has some issue 😐 !!!