Converting an existing project into customizable framework - ios

I am a android developer and I needed to create and IOS app. So I did and app is ready now. But now I want to make it a Library/Framework. I mean a same code base will be used in different projects per client and they will be able to change the text strings and colors of app. The rest of the business logic will be remain same.
So Simply I have two major question:
How to convert the Project into Framework? Do I need to create a separate framework elsewhere and then import the project into it as Coca touch Framework? Am I right??
How to make it customizable? What I meant from Customizable is simple two things.
1) Colors of Views. I have two many views. I want A separate file of colors so that I can change color in it and it reflects changes in whole application
2) String. I want to change the text string so that it can be changed in all over the projects.
Please tell me how can I achieve these things. Specially point number two is my main concern.
Note: The project that I want to convert in framework has two many viewControllers and all are built in one storyboard.

With regard to how to create a framework, here's how I'd do it:
Create a workspace:
Bring the project you want to turn into a library into that workspace by dragging it in from the Finder (make sure it's not open in XCode when you do this). Let's call that project FrameworkProj:
Create a target for FrameworkProj of type Cocoa Touch Framework:
Create a second project, which we'll call SampleProjWhichUsesFramework, and import the framework into it.
As for your question of IB elements being settable via the framework, simply make sure you have IBOutlets for those elements that are marked public, and they will be available to SampleProjWhichUsesFramework as properties of your framework.

I think you do not have good idea of the workspace in IOS Xcode. Since in Android the workspace is dealt in another way. But there is a slight difference.
Lets me just tell you about Android and how do you made the Library and add several projects in Android studio. let say you have a Project in Android App, say App1, now you can go to menu and you can add another module as Library. That is fine and easy for you.
Now you can create another project like this, GO to new menu, here you can select New Module and then from templates you can select Phone and Table Module. let's call it App2.
Similarly you can add as many projects/app in the same project (in IOS you can think of a Workspace), and in the end, All you need to do is adding Your library project as a module dependency in All App1, App2 projects you created.
So in IOS, workspace you create at first. And then You create App1, App2 in it. You add Library (Private Framework in IOS) in same project level (same directory) Then You can add the dependency of Library into Other App1, App2 and so on other projects you created.
In this way, your codebase will remain the same for others.
You can find the whole tutorial here. It is the best I have found just give it a try.

Related

Can I refactor my xcode swift app into a lib?

I have an Xcode swift app that uses "import CoreBluetooth" to access a wearable.
Now, my teammate wants to use my functionality, and thus wants a lib (.a) that he can incorporate into his xcode GUI app.
Can I refactor my app into a lib for him to add to his GUI app?
Or, is the only way to create a new xcode lib project?
Short answer is yes, you can use the same project. You need to separate 2 things:
Project is a design-time / code organization entity. One project can contain framework, app, tests, etc - all at the same time
What actually dictates what is going to be built are Targets. Some targets will build an app, some will build the library etc. Each target may contain any subset of files from your project (as long as they compile).
So that's one way to go about it: create a second target for Framework, add relevant files to Build Phases > Compile Sources and voila.
The advantage is simplicity. The disadvantages are many: how do you know you didn't break compatibility with wearable? how do you know which file belongs to app, and which belongs to app and framework (sure, you can see when you click on file, or folder structure can help, but it's not going to stop the mistake from happening)? or what if compatibility your iOS app needs is different with what wearable app needs? how do you maintain versions of such framework? etc etc
So a much better cleaner way is to create a separate repo for your framework, and maintain it as an internal product you are sharing between various components. That way you can maintain clear versioning, compatibility, and address bugs without the fear of breaking either app or wearable. And you can include that framework in both projects using (like suggested above) Swift Package Manager, or CocoaPods, or even manually.

iOS Reusable components

During my work with development of iOS applications, i noticed that almost every application has some parts that are repeated. For example every application has user management logic, Login, Sign Up, Forgot password.
And every time, i find my self trying to manually import already developed logic (View controllers, nibs, storyboards).
My question is how can i implement these common features in separate component, so i can simply reuse them every time i start new project. Also notice that there should be possibility for small customisations, for example all apps have login screen, but UI design varies for every app.
Long story short, what i need is:
How to encapsulate commonly repeated features in separate component.
How to inject the component in the newly started project.
How to make customizations at the component, without changing the component core.
I guess that here should be made some combination of Framework (Or static library) and cocoa pods, but i wanted to hear if somebody have already developed some concept about this.
Yes, exactly as you supposed, the way I opted for to reuse components is through a static library or sometimes a framework of reusable components, implementing common logic or well structured classes to inherit from in the new projects, which I make available to the new projects as a CocoaPods development pod sitting on my development machine or in a shared git repository. This way should answer your questions 1 and 2. For your question 3, you can either opt to perform customisations to the core dismissing pod updates, or to adapt the core methods to a possible override in the destination project. Hope it helps.
How to encapsulate commonly repeated features in separate component.
Whatever you choose you are going to have to factor out the code your separate component requires from your code base. This is the first step in the entire process - so think long and hard about if it makes sense to turn it into a separate component.
So now you have some code you would like to reuse...
There are a number of ways of doing this, such as Xcode's workspaces, stand alone source files, static libraries and frameworks. Cocoa pods is a package manager and will help you maintain your framework - not write it :(
Xcode's workspaces
A workspace is an Xcode document that groups projects and other
documents so you can work on them together. A workspace can contain
any number of Xcode projects, plus any other files you want to
include. In addition to organizing all the files in each Xcode
project, a workspace provides implicit and explicit relationships
among the included projects and their targets.
Static Libraries
Introduction to Using Static Libraries in iOS
Static libraries provide a convenient mechanism for sharing code among
multiple applications. On iOS, static libraries are the only supported
library type. This document explains how to extract code from your
application into a new static library and how to use that static
library in multiple applications.
Frameworks
In OS X, shared resources are packaged using standard frameworks and
umbrella frameworks. Both types of framework feature the same basic
structure and can contain resources such as a shared library, nib
files, image files, strings files, information property lists,
documentation, header files, and so on. Umbrella frameworks add minor
refinements to the standard framework structure, such as the ability
to encompass other frameworks.
Frameworks are packaged in a bundle structure. The framework bundle
directory ends with the .framework extension, and unlike most other
bundle types, a framework bundle is presented to the user as a
directory and not as a file. This openness makes it easy for
developers to browse any header files and documentation included with
the framework.
Source Files
These are the classes you have factored out of your code base. You could just include them in each project you use them - for instance a separate repo, that contains all of your shared/common code that you add to your Xcode project's workspace. Very simple, not the best to maintain.
How to inject the component in the newly started project.
Depending on how you choose to implement your common code will effect this step. For source files you just need add them to the project and set the target. For frameworks or static libraries you will have to embed them in your project
For workspaces you will add the projects containing the shared code to the main projects workspace.
How to make customizations at the component, without changing the component core.
Again you may find yourself refactoring code so that you expose the UI controls or logic functions that you want to be able to customize. As a general rule the more you expose the more complex the code gets.

How to import/link shared xCode project (and be able to edit shared project from any client)?

I don't want to compile shared project to any kind of library. I just want to use existing classes like they would integral part of client project.
I want to edit/develop/improve the shared project from any client project that use it.
I've ran trough google, but couldn't find the simpliest/more convenient approach.
More simple:
I want drag and drop shared code project, and be able to edit it anywhere. How?
There are two options: Use a static library, or include the source files directly in your project.
If you don't want to use a static library (which IMHO is the preferred method for iOS and integrates well with Xcode), you could just add references to the shared classes either as file references or as a folder reference.
Either way, the build toolchain will link everything together into one Mach-O binary, which is the only way to deliver code for the iOS.

Xcode: Handling intra project dependencies

Let's suppose that I want to create an Xcode static library.
I use the Xcode provided template for this kind of project "Cocoa Touch Static Library", i want this library in a separate project because:
I want be able to keep this project under git version control
I want to be able to import this static library under other project as a git submodule
Now let's suppose that this library depend on third-part library such as ASIHTTPRequest.
Maybe in my main project (the one that import my static library) i will also depend on the same library. What's the best way to handle this multi-dependecy?
PLUS: Now suppose that i want to create a second static library (this one is more specific for some kind of task and i don't want to mix this code with the code of the previous mentioned static library) but this second static library depend on the after mentioned static library. Again what the best way to handle this
Your question is not super clear, Luca, but let me see if I can help you out a bit.
In one of my own projects, I have a number of embedded (or "sub") projects which get built as libraries included in the main project. So if one file in the sub project changes, both the sub project and the main project pick up the changes.
With Xcode 3, it was a snap to simply drag and import one project into another project and if you have Xcode 3 installed, I highly recommend using Xcode 3 to embed one project into another.
Xcode 4 does handle projects already embedded within another project (like what you might have created using Xcode 3), but the ability to actually do the work on it's own is not fully implemented or baked yet (in other words: it does not work well or at all). Here's another question somebody else asked with more information that might help you.
Also, I noticed this other related question.
Does this information help you out?

Reference code in a separate project in XCode 4.2

I'm working on an iOS app where I use third party libraries. I want to migrate my project to use ARC, but the third party libraries are still using the old memory management. So I want to separate third party code and put it in a separate project without ARC, and then somehow link that project into my iOS-app project, so that they will be built together using the same configuration.
Is this possible to do in a very simple way, or am i better of just turning off ARC for the individual files? (seems very tedious..)
Can I use a workspace? Where one project is my iOS app and the other just contains third party code?
I've played around a bit and googled a lot, but there just doesn't seem to be any simple soultion, or am I wrong?
So I figured it out myself, with a lot of help from different blogs. Something this basic should be more trivial and well documented... But here we go, this is what I did to get a library for AsiHttpRequest:
Create a new iOS project. Select the 'Cocoa Touch Static Library' template. Call it whatever you like. You don't want to tick 'Use automatic reference counting', since AsiHttpRequest does not support it.
Select a location for your library project (will matter later on).
Delete the default .h- and .m-file created by Xcode.
Drag and drop the AsiHttpRequest files into the project
You can add the frameworks that AsiHttpRequest is dependent of, but you will have to add them to your main project anyway, so it is not necessary.
Try to build the project, it should do so without errors.
Open your main project
From finder, drag your library .proj-file into your main project (in Xcode, so that it 'lands' onto the main project file)
The library project should now appear under your main project (still in XCode). It should be expandable and you should be able to see the library project files as well. If it doesn't, try closing all open projects and reopen the main project.
Select the main project, and select target. Under Build Phases - Link Binary With Libraries, click the +-sign.
In the list of frameworks you should see your library project (called something like libname.a). Select that file
The newly added file might appear red in the list of frameworks, don't worry, it works anyway. Guess it's a bug.
Still under target, go to Build Settings
Under Header Search Paths add the relative search path to where the library .h-files are. This is relative to your main projects .proj-file. (For example ../some folder/libproject/)
Hopefully your main project will build without errors and the library project will be built at the same time, using the same configuration as the main project.
I have no idea if this is a good approach or if there is some easier way to do it. However, I like this, since I can use the library project in several projects. And if I want to update the library project, I only have to do it in one place, and the other projects will be updated as well, since they all reference the same project.
Edit1:
I had some problems with library projects using objective c categories. I received unrecognized selector sent to instance errors in runtime when trying to call those methods. This problem was solved by following the answer given here.
Go to build settings of the target in the main project and add -ObjC to the entry called Other Linker Flags
Edit2:
I found this template for creating Universal frameworks. I haven't tried it, but I guess something like this would work as well.

Resources