Cannot load xib from framework - ios

I can't seem to load a nib file in the framework I have created. The thing is I worked on it for more than a month and it was working fine. I opened it again today after 2 weeks and it's just crashing. I couldn't believe it.
I have created a tiny sample project where I am able to reproduce the issue.
To get the Bundle I am using
let bundle = Bundle(identifier: "com.nibFramework.NibFramework")
This is returning nil btw.
So I tried
let bundle = Bundle(for: MyNib.self)
Using the nib
let nib = UINib(nibName: "\(MyNib.self)", bundle: bundle)
let array = nib.instantiate(withOwner: self, options: nil)
And this is crashing on instantiate.
Error
2021-11-23 23:55:52.381944+0530 NibApp[7432:2147418] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </private/var/containers/Bundle/Application/7817FD25-54BC-4920-AC67-49D246D28794/NibApp.app> (loaded)' with name 'MyNib''
*** First throw call stack:
(0x1827f904c 0x19ae6df54 0x182850180 0x18513f338 0x1040b0d9c 0x1040af164 0x1040af1c0 0x184db415c 0x184db6c10 0x184d896a4 0x184ecc4d8 0x184e21da0 0x184eb8f84 0x184ed3598 0x184c594a0 0x184f5f584 0x185260400 0x184f61c80 0x184ea10b4 0x1942f0e20 0x194316cdc 0x1942d16b4 0x1942d2cf4 0x10441e3b4 0x104421e70 0x1942d2f94 0x1942d23d4 0x1942d69e4 0x18281b020 0x18282bce0 0x182766054 0x18276b7f4 0x18277f3b8 0x19e10f38c 0x18511f6a8 0x184e9e7f4 0x199e98184 0x1040afcb8 0x1040afc40 0x1040afd38 0x104325a24)
libc++abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </private/var/containers/Bundle/Application/7817FD25-54BC-4920-AC67-49D246D28794/NibApp.app> (loaded)' with name 'MyNib''
terminating with uncaught exception of type NSException
I am using Xcode 13.1. The Mach-O Type of this framework is set to Static Library.
Pretty sure I am missing some simple thing. Just can't figure out what. Thanks for your help.

Any Framework Bundle created with Xcodes Template will have a structure that you can rely on when looking for content packed in it.
Nib/Xib files are Resources, so with compiling they end up being copied into the according subfolder of the created Bundle (Framework in this case).
So you should be able to construct the needed Resources Folder manually.
NSString *privateFWPath = [[NSBundle mainBundle] privateFrameworksPath];
NSString *nibPath = [privateFWPath stringByAppendingString:#"/NibFramework.framework/Versions/A/Resources/MyNib"];
another way is to ask the class for its bundle and construct the frameworks Resources folder to reach the nib.
[NSBundle bundleForClass:MyNib.class];
PS: guessing MyNib.self is not a class, it is an object.
A way to check if the nib/xib ended up in the framework is to climb thru the folder structure with your desktops Finder.app. Your App will have a Frameworks folder, in there a NibFramework.framework and in this some link to Resources deeper into the structure or directly a Versions folder, subfolder A in which a Resources folder should be found and the nib will reside in. To do this you will compile first of course and then select the Product in your Xcode project, right click > show in Finder and then right click > Show Contents.
Hint: Asking Bundle for contents has a side effect when the input given results in (nil) or (null), it usually returns the mainBundle then.
Reminder: nib/xib/storyboard files do not reside in the root folder of a bundle.

Related

.xib file name is changed into ~iphone.nib while creating bundle

I am creating the framework, in that i have xib, images etc.. in frame work i am creating custom tableViewCell called SampleTableviewCell.xib. While this file is build into Bundle the name of the xib file is converted into SampleTableviewCell~iphone.nib. Because of this i am getting crash while loading this xib file in cellForRowAtIndexPath.
SampleTableviewCell *cell = (SampleTableviewCell *)[tableView dequeueReusableCellWithIdentifier:#"SampleTableviewCell"];
My crash is,
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/myname/Library/Developer/CoreSimulator/Devices/ACEB5838-EC42-416F-95AA-4CE5932D5849/data/Containers/Bundle/Application/B20C8F8B-5E09-4D84-960C-614185E955DE/myapp.app/MySDK.bundle> (not yet loaded)' with name 'SampleTableviewCell''
I dont know why it is hapening. Anyone please help me to fix this issue.Actually i dont want to have ~iphone in bundle.
Thanks.
After long struggle i found the solution,
In my xcode 7 beta 4 , changed the Deployment target of bundle from 6.0 to 8.0
This was protect me from creating nib file post fixed with ~iphone .
Try this
UINib *cellNib = [UINib nibWithNibName:#"SampleTableviewCell~iphone" bundle:nil];
[self.tableView registerNib:self.cellNib forCellReuseIdentifier:#"SampleTableviewCell"];
Hope it helps.

Main.storyboard not found

I have been trying to figure out solution to the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle
<../Library/Application Support/iPhone Simulator/7.1/Applications/FA7AFF8D-48A6-4C4D- AE92-B15CF6641C4B/myApp.app> (loaded)'
*** First throw call stack:
Tried all possible solutions given in stack overflow. Nothing worked!!
Thanks for any help!
in your Info.plist file find "Main storyboard file base name" and change value for your storyboard's name example "MainStoryboard"
Go to Build Phases -> project target -> Copy Bundle Resources -> + -> Add storyboard here.If it is there than delete and add.
Clean project and Build and run

Added new target: Could not load NIB in bundle (NSInternalInconsistencyException)

I'm attempting to add a new build target in Xcode, and so far everything has been going fine - or I've at least been able to figure out why stuff was breaking/not compiling.
Now however, I'm stuck. When building and starting the app using the new target, the app fails with this:
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'Valider1''
* First throw call stack:
(0x2f71be83 0x39a7c6c7 0x2f71bdc5 0x321ff391 0x321610fb 0x31fc2b59 0x31ea479d 0x31f4f895 0x31f4f7ab 0x31f4ed89 0x31f4eab1 0x31f4e821 0x31f4e7b9 0x31ea0353 0x31b26943 0x31b22167 0x31b21ff9 0x31b21a0d 0x31b2181f 0x31f1fa3b 0x31f0aedf 0x31ea5a07 0x31ea4cfd 0x31f0a321 0x3438476d 0x34384357 0x2f6e6777 0x2f6e6713 0x2f6e4edf 0x2f64f471 0x2f64f253 0x31f095c3 0x31f04845 0xcd059 0x39f75ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Now, I've searched for 2 hours regarding this, and theres a lot of results with suggestions, but none of them has solved the problem in my case.
The original build target works perfectly, and I'm using the same app delegate file for both targets.
Here's what I've tried so far:
Tried simulator/real device
Double checked that the "Valider1" is in "Compile sources"
Double checked that "Copy bundle resources" contains my "MainWindow.xib" (this is the nib used for "Valider1"
Double checked that "Valider1" is written correctly everywhere
Cleaned project 100 times
Validated that the Valider.m and "MainWindow.xib" has the new target checked in target membership
.plist files, build settings etc. is all identical to the original and working target
Please SO, tell me what I've done wrong or missed!
edit:
I call my nib like this:
Valider1 *validerVC = [[Valider1 alloc] initWithNibName:#"Valider1" bundle:nil mode:ny_mode];
validerVC.delegate = self;
and it works perfectly when using the original target.
EDIT 2:
I tried with another nib from my project, but still same error.
It would appear that the nibs are not being copied to the new target app, but I have no idea why :/
Remove and re-add the .xib file to Copy Bundle Resources
Go to your project settings.
Select your target.
Click on Build Phases.
Scroll down to Copy Bundle Resources and expand that section.
Check if your .xib file is present in this list -> then remove it.
Now re-add your .xib file by clicking the plus-button and find the file.
It helped me, I hope it helps you as well!

Can't load sub-view class and xib from static library and bundle

I have 2 iOS projects. Let's call them A and B. A is library project including 2 products:
a static library
and bundle file has some .xib files
B is an app project using project A's library and bundle files.
Project A has 2 things: a table (customized UIViewController class and a .xib file) and a cell (customized UITableViewCell and .xib file)
The problem is:
Project B can load the table from library and bundle, but can't load cell for that table.
This is an open-source project, I am stuck for a few days, please help me out.
https://github.com/G3Feng/granpost-ios
Error:
2013-09-18 07:28:50.184 TabbedDemo[34408:c07] |cell| load bundle:NSBundle </Users/Feng/Library/Application Support/iPhone Simulator/6.1/Applications/359F9352-F3BA-4478-AA51-A182D8CFA4AA/TabbedDemo.app/GPResource.bundle> (not yet loaded)
2013-09-18 07:28:50.185 TabbedDemo[34408:c07] Unknown class articleListCell in Interface Builder file.
2013-09-18 07:28:50.204 TabbedDemo[34408:c07] Could not load the "commentbubble-01.png" image referenced from a nib in the bundle with identifier "(null)"
2013-09-18 07:28:50.206 TabbedDemo[34408:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x71598b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bottomBar.'
*** First throw call stack:
(0x1c97012 0x10d4e7e 0x1d1ffb1 0xb80e41 0xb025f8 0xb020e7 0x68f23 0xb2cb58 0x236019 0x10e8663 0x1c9245a 0x234b1c 0x2368da 0x39a0 0xd38fb 0xd39cf 0xbc1bb 0xccb4b 0x692dd 0x10e86b0 0x2293fc0 0x228833c 0x2288150 0x22060bc 0x2207227 0x22a9b50 0x2eedf 0x1c5fafe 0x1c5fa3d 0x1c3d7c2 0x1c3cf44 0x1c3ce1b 0x1bf17e3 0x1bf1668 0x18ffc 0x2b1d 0x2a45)
libc++abi.dylib: terminate called throwing an exception
This one solved the problem finally: https://stackoverflow.com/a/6092090/1571946
==>
This doesn't really have anything to do with Interface Builder, what's happening here is the symbols aren't being loaded from your static library by Xcode.
To resolve this problem you need to add the -all_load -ObjC flags to the Other Linker Flags key the Project (and possibly the Target) in the Build Settings.
Check every time you access a resource from another bundle. Try this code.
NSBundle *yourBundleObject = [NSBundle bundleWithIdentifier:#"yourBundleIdentifier"];
if(![yourBundleObject isLoaded]){
[yourBundleObject load];
}

iPad specific resource suffix doesn't work with storyboards

According to the Apple's Resource Programming Guide you should be able to use device modifiers for any kind of resource file
Here's the quote
You can apply device modifiers to any type of resource file.
However while this seems to work for most resources (images, nibs) I'm running into problems when using it with storyboards.
What I've tried doing is having two storyboards with one of them having the ~ipad suffix attached to it's file name, and trying to load it like the following
UIStoryboard *mySB =
[UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
myViewController = [sigRequestSB instantiateInitialViewController];
And I'm getting a NSInternalInconsistencyException exception
The strange thing is that I only get this error if I have a storyboard with the ~ipad modifier, if I have only the single storyboard without the modifier it will just load that on the iPad. Further if I detect the what device is running and load the storyboard using the full file name (that is on an iPad attach the "~ipad" to the filename) it works.
Here's the full error
2013-08-29 15:32:44.294 MyApp[18757:c07] * Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason: 'Could
not load NIB in bundle: 'NSBundle
(loaded)' with name 'UIViewController-Cq1-k2-VhC' and directory
'MyStoryboard.storyboardc''

Resources