Borce Ivanovski
Borce Ivanovski

Reputation: 571

iOS static library

I am fairly new to iOS development. I need to create a static library from a current app, which we want to provide to clients to integrate it in their own app. Now I don't know if Apple allows something like this or not.

I manage to create the static library, but i don't know which files i need to put as public headers which not. Basically the simplest implementation of the library will be, the client will add a button which will lunch our app inside theirs. As i said i don't know if this is even possible, but from what i read so far, it should be possible, but i haven't been able to find any example or help in how to make it happen.

here is the code from 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;

}

Now the question is what should i put in the library.h and library.m? And also how should i call the view controller inside the demo app, that utilizes the library?

Thanks everyone for the help.

Upvotes: 1

Views: 610

Answers (2)

FlySoFast
FlySoFast

Reputation: 1922

This is a really helpful post for your situation: how to create and use a static library, make it universal (support both 32 and 64 bits architectures), where to put the headers file...

However, for the sake of simple use, creating frameworks or bundles instead of static library would be better in my opinion. In case you want to know how to do it, here is another useful post.

edit

OK this is a simple demo for your case. Stop worrying about those static library, header files... thing. Just write some class and add it to you projects, pretending that you're going to give your clients your lib's full source code. If they work, then continue to pack it into a framework or static lib. First you create a Viewcontroller class that will display any string given on initialized.

Header file:

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

Implement file, add these methods:

-(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];

}

The presentFromViewController does the trick!

It will be used like below in your client project. As in your question, the viewcontroller will be presented when user click a button:

Import header file

 #import "YourViewController.h"  

Then, at your button code, add these lines:

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

Hope this helps.

Upvotes: 1

Alan
Alan

Reputation: 307

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

If you want to this function, there is no need to make static library.

Your can use

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

Don't forget config URL Schems in Xcode TARGETS->Info->URL Types

Upvotes: 0

Related Questions