XCode - Workspaces - ios

i'm developing a framework and i would like to know if there is a method for share the code across multiple project. Example : i have my workspace with my framework project and a test application project for the fw; then, i have another project of an app and i need to use the framework, but i want to link the framework so that when i modify the original code in workspace even the code in the project is updated without import every time the .framework file.
Is it possible?
If not, how can i do to share code across projects?
Thanks

Yes, this can easily be done by importing files from the other project. You could create a group called external, right click and choose "add files". Make sure the option "copy items into destination group`s folder" is unchecked.
If you now change the files in the other project, the changes will be reflected in the dependent one.

Related

Export files from Xcode project

I currently have an Xcode workspace with an app project and the pod project (as commonly happens). I would like to "export" some of the files containing classes and structs that would be preparatory for a new project that should begin soon. These files are totally separate from each other and should not be exported as a library or framework because that should require to keep everything updated. Instead, I would rather bring these files out of the app project, but keep them into the workspace. Is it possibile? If so, how can I achieve this goal?
When you want to keep your common code and be able to maintain it as one code on both projects then I suggest you still use a framework. It is hard to tell from your question what you mean by "because that should require to keep everything updated" but using a framework within your workspace works awesomely in Xcode and any change you do within framework is instantly reflected in your code. So no extra deployment work is needed at all. To do so you simply do the following:
Create a new project selecting Framework
When creating the project select your workspace from dropdown menu (it is at step where you select project folder location)
In Xcode file navigator in newly created framework project open "Products" directory which should contain (in red) FrameworkName.framework
Open your main project target settings and open General, scroll down to Frameworks, Libraries... and drag that .framework file in this list
That is all. Now you can use your framework in your code. Simply import it in any Swift file you want to.
If at this point you are worried that you need to add import MyFramework into each and every Swift file where you use it then there is a shortcut. You can simply add a new Swift file and use type aliases to avoid this problem:
import MyFramework
typealias Class1 = MyFramework.Class1
typealias Class2 = MyFramework.Class2
typealias Class3 = MyFramework.Class3
Not the cleanest solution but it does the job.
On the other hand if you wish to extract files and use them in a new project but not share the code. So then each of the projects may eventually end up with different implementations of these files then you need to duplicate the files. You can do that easiest with file system by copying source files themselves.
You can then put them into workspace under whatever you want. If you wish that currently none of the projects contain these files you may simply select files in Xcode and uncheck Target Membership for all projects on file settings.

Do I need to add my framework to project again and again after editing ios?

I have my own framework which I've made. I use this framework in another project I made. That is, after building my framework, Dragged the Product, .framework to Embedded Binaries of project.
However, I've to continuously make changes to the framework. So the process of dragging and dropping again and again is a lot of work. How to conquer this ?
Can someone tell the exact steps even if it means including the project as well ?
Copy the framework Project to your project folder.
Open your Project in xcode.
Right click on the Project File on left side of xcode and click on option "Add file to Project Name" and then select the framework project here.
Delete the Product .framework available in the embedded libraries and add the framework product properly.
So your goal is to 'deploy' changes made to .framework to all the projects you've already embedded it in previously. Here is one approach to it.
Create [myFramework]Targets.txt file where you'll be putting all the locations (directories) of your framework in different projects, line by line. It will have something like this:
~/Path/to/project/A/Frameworks
~/Path/to/project/B/Frameworks
~/Path/to/project/C/Frameworks
Create deploy_framework.sh file with script that will replace your framework everywhere you need. I'm definitely not the one you can call a Bash expert, by here is what it probably looks like:
for destination in $(<[myFramework]Targets.txt); do /bin/cp -rf /Path/to/updated/[myFramework].framework "$destination"; done
Now, whenever you're done with your framework changes, just run deploy_framework.sh from Terminal. Assuming your Xcode projects have existing reference to .framework and you didn't change its name, it should work.
NOTE: You might still need to do Clean+Build for your projects to compile with updated framework. I believe you can also tweak project settings to 'cache' builds less aggressively.
Let me know if it works for you, we might need to adjust script a bit, since I never tested it.

Not able to access public swift class through framework

I created a project and imported it inside this framework. When i load this framework into a demo app, i am able to access the public class MyTest inside MyTest.swift file but not able to access the public class inside MessagesViewController.swift. Am i missing something? Can't we access files inside a project through a framework this way?
Restructure your project space so that you do not have your application inside your framework, then tackle linking files between the app and the framework.
Create a xcworkspace file to contain all of your sub-projects. For more information check out the Xcode Workspace section in the Xcode Concepts document
Open the workspace file and add your framework and app projects by dragging the xcodeproj files to the "Project Navigation" panel. Be careful when adding the files as Xcode as a tendency to place one project inside another, you don't want this.
Build your framework and then link it to the the app project. This will also make all you framework files available to your app project as long as they are public(see below for more details). Navigate to the FireChat-Swift app project target in the editor view then from the frameworks project open the product directory and drag the framework file to the Embedded Binaries section of the general tab.
Now your framework is linked with your app project. You will need to import your framework in your app's source code.
Add new files to your framework and remember they need to be public should you require them to be used outside of the framework - i.e. by your app.
Did you add MessagesViewController.swift to the Target inside the original Project?
To check it just open the original project, select MessagesViewController.swift and take a look at the right sidebar (Target Membership section).
I had this problem, and I fixed mine by going to my framework and adding target membership to Tests and UITests:
Strange fix, but I guess it works! 🤷‍♂️
This can also occur as part of a bug in Xcode, though I'm not sure what causes it - possibly dragging files from another project into your library. I just fixed this by closing down and restarting Xcode, then cleaning the build folder (hold down Alt then select the option from the Product menu).

How to create an Xcode project that is not an app but is visible in other projects?

I want to create a project with a handful of categories that add useful functionality to UIKit.
I want to keep this as a separate project so that it can be source-controlled separately (and eventually hosted on github, and downloadable as a dependency via cocoa pods...but let's skip this for now.)
But all the Xcode 6.1 project templates imply an app inside. When I select an empty project it complains about missing base SDK. But this cannot be set without a target present. It is quite confusing. I don't need a target. Do I?
The project should be just a shell for a number of categories + a accompanying summary header file.
I want to be able to drag this project as a subproject to any other proper app project, import the summary header file and expose those categories to the main project.
Any advice?
You need a target. Every source file that should be compiled must be a part of a target.
You could reference each source file from the target(s) in your app's Xcode projects, but that'd be tedious as you'd have to add to every project manually when you add source to your shared repository.
Either create an embedded framework project or a static library. Unless you are really going to share code between an app and an app's extension, go with the static library. It is a lot easier to manage.
Create a project that has a static library target, add the files to that static library.
Then create a workspace that has your static library project and your app(s) project(s) in it. Modify the app targets to link against the static library.

Trouble Creating Cross Project Reference for Xcode 4

I'm trying to pick up some better code organization practices. Over time, I've developed a collection of utility-type files that I import into all of my iOS projects. Rather than continuing to copy them in, I want to setup a separate "Library" project which builds these files to a static library. Then, I reference the static library in my app's project.
In Xcode 4.2 I created a new project "JTLibrary" with a static library as the target, added some files, and it builds as expected.
Now, I created a new project "LibraryTest" which should behave like any other app project I might be working on. My understanding was that I should be able to drag the JTLibrary project into this project to create a cross project reference. Once that was done, I would link LibraryTest's binary against the static library from JTLibrary.
However, when I drag the JTLibary project into the LibraryTest project, it does not appear expandable. I expected I should be able to see the files in it, etc. See screenshot below:
Can anyone offer some insight to why the project appears like this rather than expandable? Once it is expandable, I should be able to see the static library in the app's project.
Thanks!
EDIT 11/7
It's worth mentioning that I'm working with a VERY basic project now. From the new project window, select Framework & Library >> Cocoa Touch Static Library. Add one function to the default class and build.
I can copy the header and .a files into another project and use them successfully, but if I try to drag the project itself, it appears like the picture above.
To create a subproject simply drag a project node from Finder into Xcode.
DO NOT open two Xcode instances and drag the project node from one to the other (that's why the subproject is not expandable).

Resources