iOS静态库

时间:2015-08-05 01:07:26

标签: ios static-libraries

我是iOS开发的新手。 我需要从当前应用程序创建一个静态库,我们希望它们提供给客户端以将其集成到自己的应用程序中。现在我不知道Apple是否允许这样或类似的东西。

我设法创建静态库,但我不知道我需要将哪些文件作为公共头文件而不是。基本上最简单的库实现,客户端将添加一个按钮,它将在我们的应用程序内午餐。正如我所说,我不知道这是否可能,但从我到目前为止阅读的内容来看,它应该是可能的,但我无法找到任何实例或帮助如何实现它。

这是来自AppDelegate.m的代码

#import "AppDelegate.h"

#import "GlobalViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.viewController = [[GlobalViewController alloc] initWithNibName:@"GlobalViewController" bundle:nil];


    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;

}

现在问题是我应该把什么放在library.h和library.m中? 还应该如何调用使用库的演示应用程序中的视图控制器?

感谢大家的帮助。

2 个答案:

答案 0 :(得分:1)

这是一个真正的helpful post适合您的情况:如何创建和使用静态库,使其通用(支持32位和64位架构),在哪里放置头文件...

但是,为了简单起见,在我看来,创建框架或包而不是静态库会更好。如果你想知道如何做,这是另一个useful post

修改

好的,这是一个针对您的案例的简单演示。不要再担心那些静态库,头文件了......事情。只需编写一些类并将其添加到项目中,假装您将为您的客户提供您的lib的完整源代码。如果它们有效,则继续将其打包到框架或静态库中。首先,创建一个Viewcontroller类,它将显示初始化时给出的任何字符串。

标题文件:

#import <UIKit/UIKit.h>
@interface YourViewController : UIViewController
-(instancetype)initWithString:(NSString*)text;
-(void)presentFromViewController:(UIViewController*)viewController;
@end

实施文件,添加以下方法:

-(instancetype)initWithString:(NSString*)text{ //Init your viewcontroller
self=[super init];
if (self) {
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
    label.text=text;
    [label sizeToFit];
    label.textColor=[UIColor whiteColor];
    [self.view addSubview:label];

}
return self;
}

//I will not delete the code with navigation controller for your later use.
-(void)presentFromViewController:(UIViewController*)viewController{

    //if (viewController.navigationController) {
        //[viewController.navigationController pushViewController:self animated:YES];
    //}
    //else{
        //UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self];
        //[viewController presentViewController:navigationController animated:YES completion:nil];
    //}

    [viewController presentViewController:self animated:YES completion:nil];

}

presentFromViewController可以解决问题!

它将在您的客户端项目中使用如下所示。在您的问题中,当用户单击按钮时将显示viewcontroller:

导入头文件

 #import "YourViewController.h"  

然后,在您的按钮代码中添加以下行:

- (IBAction)yourButtonOnClick:(id)sender {
    YourViewController *vc=[[YourViewController alloc] initWithString:@"It works!"];
    [vc presentFromViewController:self];
}

希望这有帮助。

答案 1 :(得分:0)

the client will add a button which will lunch our app inside theirs.

如果你想要这个功能,就不需要制作静态库了。

你可以使用

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"xxx:xxx"]];

请勿忘记Xcode URL Schems

中的配置TARGETS->Info->URL Types