1

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.

Borce Ivanovski
  • 561
  • 1
  • 4
  • 23
  • 1
    You can build your own framework, which users can import – The Brofessor Aug 05 '15 at 01:10
  • yes, that is what i am trying to do, i started with a static library, and i have managed to integrate the static library, but as i said in the question, i don't know which files should i put as a public headers, so i can start the view controller from the app that uses the library. And also i am not very familiar with iOS, starting to learn it, but at this moment i am trying to solve this as well. Any guidelines in how to access the view controller, or the app delegate inside the new app? – Borce Ivanovski Aug 05 '15 at 01:15
  • once i manage to fix the static library, i have found tutorial in how to make a framework which can be imported. but first i want to test if possible to do it through static library and afterwards i will continue in doing the framework. What i want to achieve is, i have App 1 created as library 1, and i have app 2, that will have library 1 imported or linked, and on a button 1 press to activate the UI from library 1, and then on button 2 press, to activate its own ui or functionality. – Borce Ivanovski Aug 05 '15 at 01:17
  • have a look at my answer on another post [here](http://stackoverflow.com/a/19957367/1219956) its not the same question as yours, but you will have this question down the line. I have implemented something similar to what you are asking and this question / answer helped me – Fonix Aug 05 '15 at 02:30
  • yes i will look into it, maybe that will solve my issue. actually i think that one of the comments in that question, is actually what i am looking for, function to get the storyboard from the library inside the app, and display it – Borce Ivanovski Aug 05 '15 at 02:46
  • shouldn't this be much simpler to be implemented? when we are providing the whole app as a library, shouldn't be simple just to get the new app to display the storyboard? i mean this is part of the things why we need libraries, so we can distribute functionalities much easier, and if the client doesn't want to change anything from the functionality of my app, it should be able just simple to add button to call my app view, and done – Borce Ivanovski Aug 05 '15 at 02:48
  • @BorceIvanovski, its not quite so simple because storyboards and xibs cant be inside frameworks, so you have to do a bit of juggling to get your UI to show up. but once you have done that its easy from there on out, then it is as simple as calling a function to display your ui – Fonix Aug 05 '15 at 04:34
  • I managed to make it work somehow, but i am having another issue now, that i will create another question. Thanks all for the help – Borce Ivanovski Aug 05 '15 at 19:59
  • @Fonix ok, i managed to make the framework, so should i use the xib of the view controller, inside a bundle, in order to add it to the framework? and also what do i do afterwards? Right now when i try to use the ViewController class, it shows me lot of Match o link errors, but don't know what to do with it. – Borce Ivanovski Aug 07 '15 at 01:15

2 Answers2

1

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.

Community
  • 1
  • 1
FlySoFast
  • 1,854
  • 6
  • 26
  • 47
  • yes i followed the 2nd post how to create framework, and i did the library part, but thats where i am stuck, not that i can not continue, but how to start the viewcontroler, from the library? – Borce Ivanovski Aug 05 '15 at 01:58
  • and i was planning to continue the framework path, and create the framework, but if i can not start the viewcontroler of the library inside the new app, than what is the point of continuing to create framework, when i am stuck at this point – Borce Ivanovski Aug 05 '15 at 01:59
  • @BorceIvanovski I've just updated some code. Hope it helps. – FlySoFast Aug 05 '15 at 08:11
  • thanks for the code, and i am trying to understand it, but i don't think i can figure it out. Is there possibility to help me with my specific case? or at least to see the library file that you have, and how is all connected? from a clear specific example, i think i can understand it, but like this i can't. I created the library.h and library.m files, and i put this what you wrote in them. When i tried to create the object in the demo app, i get error undeclared identifier. also i have the library.h as public header in the library. Is this the right way or? – Borce Ivanovski Aug 05 '15 at 16:18
  • ok so i did that, and it compiles and no issues, when i press the button doesn't do anything or shows anything. So what would be the next step? FlySoFast, is it possible to connect on email maybe, because i can see you have knowledge in IOS, and i really need some help with it. – Borce Ivanovski Aug 06 '15 at 20:18
  • so i followed your comment, but i am getting an error unrecognized selector sent to instance , when i press the button action, this is the code it makes the error Alleview *vc=[[Alleview alloc] initWithString:temp]; in the framework, i setup the public header lib.h, to be ViewController, but don't know where the issue is. Thanks – Borce Ivanovski Aug 06 '15 at 23:33
0

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

Alan
  • 197
  • 1
  • 11
  • but do they need to have our app installed on the phone or not? my app might not be installed on the user device, so this is why i wanted to include the static library inside the client app, so it can open the UI of app1 inside app2. – Borce Ivanovski Aug 05 '15 at 01:26
  • ```[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@""]];``` This know if your app installed or not.If your want to include the static library in the client app,your can't just replace the `rootViewController` or just `Present a new viewContorller` – Alan Aug 05 '15 at 01:29
  • ok so how do you suggest to make this work? How to use the UI of the app1, inside app2? how to add the functionality of my app, inside clients app? – Borce Ivanovski Aug 05 '15 at 01:34
  • ok any help? link where i can find some example or tutorial? example code you have or anything? I found that very easy done on android, but in iOS i find it little bit more difficult. i can not find real example with explanation, on how to do the library and use it in the new app. Thanks in advance for the help – Borce Ivanovski Aug 05 '15 at 01:38
  • ```app1.window.rootViewController = app1.viewController;``` button method ```[UIApplication sharedApplication]keyWindow.rootViewController = app2.rootViewController;``` [creating-a-static-library-in-ios-tutorial](http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial) – Alan Aug 05 '15 at 02:04
  • and this is going where? – Borce Ivanovski Aug 05 '15 at 02:11
  • do i still use library of app 1 inside app2 or? – Borce Ivanovski Aug 05 '15 at 02:17
  • how to access the rootviewcontroller of app1, or the view controller? that is the part i am asking about, what should i do in the library of app 1, so i can use the view controller of app1 inside app 2. And not to give out the code behind the whole functionality of the app, just to give them a way for clients to access the view controller of my app (app1) inside their own application. – Borce Ivanovski Aug 05 '15 at 02:22
  • unless im missing something, i dont think this is what the OP wants, this will just open the app as a separate app, not open it "inside" the other app (im guessing he wants like a modal popup that has his own custom stuff, sort of like facebook api) – Fonix Aug 05 '15 at 02:27
  • I think He wants to make app2 to SDK.Your can search many [SDK](https://github.com/search?l=Objective-C&q=SDK&type=Repositories&utf8=✓) info on `github`. – Alan Aug 05 '15 at 02:33
  • yes Fonix that is exactly what i want, and why i was trying to build a static library. Basically, to open up the storyboard or the main view of app 1(library 1) when a button in app 2 is pressed. So this way i can distribute the static library or framework to clients who wants to include the functionality in their own app without any changes and visibility of my app – Borce Ivanovski Aug 05 '15 at 02:35
  • ok, fonix and Alan, are you familiar with android development? I managed to do this in android where i managed to convert the app into a library, and just using an intent i started the main class of the app. i want to do the same in iOS, so i don't know if this is exactly possible and apple allows it or not. Hope i make sense, and give enough information. It should be straight forward to do this, at least in my mind it is, but i guess Apple maybe doesn't allow this kind of activity – Borce Ivanovski Aug 05 '15 at 02:38
  • maybe if i give little bit more details, it will make more sense. I developed the app, has only 2 views, main view and settings view, thats all. Clients want to include both views inside their own app, they don't want to change anything, just want to add 1 button inside their app, that will initiate my app and shows the main view, from there, there will be back button, which will lead back to the parent, or 1 button which will open the settings view of my app (again inside the client app), and thats it. – Borce Ivanovski Aug 05 '15 at 02:41
  • check my comment on your main question, should help – Fonix Aug 05 '15 at 02:43