6

I am trying to add xib file or any other view controller file within static library but I can't do so. Can you please help me?

If possible please add the whole source code

their is button on first view. And when clicking on that button, new view controllers comes up with something (lets say changes in background color). How to create static library for this? so that I may use it in another project?

Thanks in advance

  • http://stackoverflow.com/questions/6859143/whether-nib-xib-files-can-be-added-to-a-static-library have a look of this it may help you. – Divyam shukla Jul 10 '13 at 13:10
  • When we create static library, it gives the .h and .m files in which we need to write the code. Yes, we can write fibonacci function easily . What if I want table view in the very first view. As I don't have any xib file. I can create a function and can create tableview programatically. and if we select any cell, new view controller needs to comes up. Please can you guide with it. –  Jul 10 '13 at 13:32

3 Answers3

7

If you want to build your interfaces using interface builder when building a static library you will need to make a bundle and distribute it with your library.

In Xcode:

  1. File> New> Target
  2. Select "Bundle" from the OS X Framework and Library section
  3. Fill in the details. The bundle framework should be core foundation.

Then you need to get your bundle compiled at the same time as your framework. Add the bundle to the "Target Dependencies" build phase of your framework.

When you make your xibs you make their target this new bundle you have created.

Then when you compile your framework in the derived data directory, next to your framework binary you will find your compiled bundle. You give this to your third parties along with the framework binary.

So how do you reference this bundle in your code? In iOS bundles cant be loaded and your bundle will actually be inside the third party's iOS application main bundle. You can create a category on NSBundle for conveniently accessing your bundle from your code:

@interface NSBundle (mybundle)
+(NSBundle *)myBundle;
@end

@implementation NSBundle (mybundle)

static NSBundle * _myBundle;

+(NSBundle *)myBundle{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        NSBundle * mainBundle = [NSBundle mainBundle];
        NSString * pathToMyBundle = [mainBundle pathForResource:@"myBundle" ofType:@"bundle"];

        NSAssert(pathToMyBundle, @"bundle not found", nil);

        _myBundle = [NSBundle bundleWithPath:pathToMyBundle];
    });

    return _myBundle;
}

You can then access your bundle in code to load xibs like this:

UIViewController * controller = [[UIViewController alloc] initWithNibName:nil bundle:[NSBundle myBundle]];

Remember that if you use categories in your framework code you will need to make sure that your framework consumers add the -ObjC (or -all_load if not using a recent Xcode) "other linker flag" to their projects

jackslash
  • 8,550
  • 45
  • 56
  • 1
    OMG - this was the winner. It worked for me even in Xcode 7. Many of the older tutorials miss the step of adding the bundle as a target dependency. Thanks! – drudru Jun 03 '16 at 00:55
  • This doesn't work anymore (in Xcode 9), I'm getting an "iOS xibs do not support target device type "mac"" error when building the library. – johnnyMac Mar 15 '18 at 04:07
4

Strictly speaking, it's correct that you cannot add a Xib to a static library. However, there are workarounds. Matt Galloway's iOS Library With Resources tutorial will show you how to do just that.

See iOS Library With Resources.

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
  • It definitely works although there may have been additional steps necessary to get the bundle into the parent project. I'd have to check... – Max MacLeod Jul 10 '13 at 14:25
  • if possible can you send me the steps to create library add xib file in .h and .m file in it –  Jul 10 '13 at 14:30
  • so you have followed all the steps in http://www.galloway.me.uk/tutorials/ios-library-with-resources/ ? Have you double checked everything? – Max MacLeod Jul 10 '13 at 14:42
  • ok, is it compulsory to add whole library project to every projects where we wants to access library project. or just .a file needs to be added? –  Jul 10 '13 at 15:21
  • the library project will produce two products: a static library .a file, and a resources bundle .bundle file. You need to add both of those to the build phases of every project that wants to use them – Max MacLeod Jul 11 '13 at 07:41
0

XIB is like resource and what static library is , a compiled file that contains ur classes. You can say it is your compiled code ready to be used. You can not add xib's to static library.

BhushanVU
  • 3,455
  • 1
  • 24
  • 33
  • Hi, we cannot add xib file. but yes we can add buttons, or any other objects from uikit library in uikit framework. But my question is how to navigate from one view controller to another in static library –  Jul 10 '13 at 13:36
  • i don't get you, navigation code for static library would be same what we use generally.. one advice if you want to use UI in your library then use Subviews rather than using Viewcontrollers... – BhushanVU Jul 10 '13 at 13:40
  • Ok now how can i add subview –  Jul 10 '13 at 13:46
  • this will clear everything http://www.blog.montgomerie.net/easy-xcode-static-library-subprojects-and-submodules – BhushanVU Jul 10 '13 at 13:50
  • Once static library created, is it always necessary to add whole code (project) ob library or just .a file? –  Jul 10 '13 at 14:02
  • Can you send me the code as how to add subview which will have tableview in it. please –  Jul 10 '13 at 14:07