1

我正在创建一个自定义框架,其中包含许多我将在几个不同项目中使用的类。我将把这个库提供给其他开发人员,所以我需要保护我的代码

我遇到了这个似乎非常有用的教程

http://codefriend.blogspot.co.uk/2011/09/creating-ios-framework-with-xcode4.html

使用它,我设法将我的应用程序变成了一个框架,并且所有类都被复制并且代码受到保护,因为所有用户都会看到 .h 文件。所以我可以将它添加到任何未来的应用程序中,但是我不知道如何访问资源,即图像和 XIB 文件。

图像的一种解决方法是将 .png 文件转换为 .png.h 文件,过程是这样的

1) Open a terminal and "cd" to the directory that holds myImage.png
2) Run the command "xxd -i myImage.png myImage.png.h" -- This will turn the image into a .h header file
3) Include the new myImage.png.h file in the framework before building and make it a public header
4) After adding the framework to the project using it, import myImage.png.h
5) Load the data into a NSData object with [NSData dataWithBytes:myImage_png length:myImage_png_len];

这适用于图像,即使它是一种混乱的方式。但是,这似乎不适用于 XIB 文件。作为测试,在我的框架项目中,我创建了一个名为 TestController 的新 UIViewController,然后,我创建了框架,将其添加到我的新项目中,然后尝试像这样访问它

NSData *data = [NSData dataWithBytes:TestControllerVC_xib length:TestControllerVC_xib_len];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    TestControllerVC *test = [[TestControllerVC alloc]initWithNibName:str bundle:nil];

    [self.navigationController pushViewController:test animated:YES];

应用程序因此错误而崩溃

>     *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in
> bundle: 'NSBundle </Users/adam/Library/Application Support/iPhone
> Simulator/6.1/Applications/E9AEE10A-27A0-4363-B81D-D1DBE5DC7A51/TestAppWithFrameworkLib.app>
> (loaded)' with name <?xml version="1.0" encoding="UTF-8"?>.... (rest of xml printed)'

(它打印 TestController 类的 XML,不想用整个 xml 文件阻塞帖子)

我的问题是,有没有更好的访问资源的方法?如果我访问我目录中的 TestBundle.Framework 文件夹,您可以看到资源文件,但是当您将框架添加到您的应用程序时,您看到的只是标题文件夹(见下图)

将框架添加到应用程序时没有显示资源文件夹是否有原因?无论如何我可以访问资源,因为 xib 文件肯定已添加到资源中,如下图所示。任何帮助将非常感激!!!

数字

在此处输入图像描述

4

1 回答 1

1

http://www.nearinfinity.com/blogs/tyler_vernon/2012/07/17/creating-and-using-static-libraries-for-iphone-using-xcode-4.3.html

对于需要创建库和添加资源的其他任何人,本教程可以为您完成这项工作。如果您要在库中放入大量类,则设置起来很耗时,但可以完成工作。希望这可以帮助其他有问题的人

于 2013-07-05T10:07:34.477 回答