iOS 静态库

iOS static library

我对 iOS 开发还很陌生。 我需要从当前应用程序创建一个静态库,我们希望将其提供给客户以将其集成到他们自己的应用程序中。现在我不知道 Apple 是否允许这样的事情。

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

这是来自 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 中放什么? 还有我应该如何在演示应用程序中调用使用该库的视图控制器?

感谢大家的帮助。

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

如果你想要这个功能,不需要做静态库。

你可以使用

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

不要忘记在 Xcode TARGETS->Info->URL Types

中配置 URL Schems

这确实helpful post适合您的情况:如何创建和使用静态库,使其通用(同时支持 32 位和 64 位架构),将头文件放在哪里...

但是,为了简单使用,我认为创建框架或包而不是静态库会更好。如果您想知道如何操作,这里是另一个 useful post.

编辑

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

头文件:

#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];
}

希望对您有所帮助。