Not able to access the public class from framework iOS Swift - ios

Hi i have created a framework with a swift file having a public class. When i try to use this public class in the app project that i have linked this framework to , the xcode shows that there are no such class.
public class TestFrame {
public func hello() {
print("Hello World")
}
}
This is the class from my framework.
First i build the framework for my device, then link that file to the embedded binaries in my app project. Then i include a import statement of the framework name and then try to use this class(which is not possible at this stage).The Only file that i am able to access through framework is the .h(header file) of the framework. So what i need to do to get this class accessed in my app project.

The problem is that you built the framework for the device (arm) and now you are trying to link it to the simulator platform (i386).
You can either build two frameworks (one for the device, one for simulator), build a 'fat' framework including both architectures or you drag the framework project to your workspace so it is always built with for the selected target platform.

For anyone with the same problem:
Try building your framework for Generic iOS Device and then import it again.

For me it was because I moved the framework files from another project, grouped inside a folder and it lost the target. So I had to add a target to the classes before I was able to see them in the main project.

For me it was because I used the same name of the framework for a class name. 🤦

Related

Swift3 Framework Module is empty

Framework Project: https://github.com/twodayslate/SwiftGMP
I built this this project and dragged the SwiftGMP.framework into my iOS application. I can import SwiftGMP just fine but I cannot use any of the functions or structs even though they are set to public in the framework. What am I missing? I'm hoping it is just a flag or build setting. The SwiftGMP-Swift.h is being generated.
edit:// I got it working by dragging project into my application and then settings the library path in my main project. Doesn't seem right but it works.
Take a look at the settings of this project which uses SwiftGMP without adding it as a separate sub-project.

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).

Is it possible to create a standalone distributed Swift Framework?

I have a scenario where I need to package some common code into a Swift only framework. I have found many tutorials on how to do this but all involve creating a framework within an iOS app. My goal is to:
Create a seperate Framework project
Build and produce a .framework file
Copy that single file(without all the source files) into some iOS app project, add it under "Linked Libraries" and use it.
Question: Is this possible or do I have to include all of the Classes that are part of the framework?
I have added a framework project to an iOS app and had success using it because all the source code was in the Framework project. I tried to copy that .framework file alone to a new project and use it but when trying to access the classes or instantiate the contents of the framework I couldn't even-though whatever needed to be declared as public was.
Appreciate any help...

Swift: using private framework

I have built an iOS Swift framework with Xcode.
After writing the code I have build the project and took the .framework file inside the Products folder to test it.
To test the framework have open a new application and drag and drop the .framework file previously built and I added it into the embedded binaries list of my application project.
To import it into my ViewController.swift class I wrote:
import frameworkName
No problem until here, this means that the project sees the framework.
When I try to use a public class inside the framework with:
var x : className?
I get the following error:
'className' is unavailable: cannot find Swift declaration for this class
What does it mean? What is the problem?
When you're referencing a framework in the products directory from your workspace, ensure that the location is "Built Products" and the filename is just the framework name, without any additional path components.
If you're referencing a framework that isn't in your workspace, I would recommend using Carthage instead of copying it directly into your repository. This will make versioning much easier, and will ensure that it is built correctly for both simulator and device.
To actual a self defined framework, u really have to do a lot of things.
Firstly, make sure ur framework is used on right device. It's to say framework can only be used on corresponding device(either one of Simulator, Device and Mac). In other words, if framework A built on simulator, project import framework A can only pass compile and successfully built on simulator.
P.S. if universal version desired, -lipo command is what u need to further explore.
Secondly, during implementing ur framework , make sure all the classes, methods and variables u want use outside start with Public.
Thirdly, check ur project setting Embedded Binaries and linked Frameworks and Libraries do contain ur framework.

iOS import swift class from swift using framework

How do I import a Swift class from a framework?
I have Xcode Workspace
I create a Cocoa touch framework with using Swift
I create a Swift class named TestClass in that Cocoa touch framework
I create a Tabbed Base iOS application
I import my framework in that tabbed iOS application using import FrameWorkTest
The application project compiler can't find TestClass.
How can I import TestClass for use in the main application?
It says that TestClass is not defined. I also want to add an extension in FrameWork an then use it.
https://github.com/ArtemKyslicyn/-SwiftImportFrameworkTest
I'm using this Tutorial
http://www.swift-studies.com/blog/2014/6/30/creating-a-pure-swift-framework-for-both-ios-and-mac
I'm on Xcode version 6.0 seed
Thanks
Make the class in framework public. Also any variables must be public if you want them to use outside the framework.
public class MyClass... {
...
}
Here's one solution I found:
Create new iOS project
Press on project
Press + to add target
In the resulting window, choose cocoa touch framework
Press on project and press add dependency and add framework
Success!
I used the solution here: https://github.com/ArtemKyslicyn/-SwiftImportFrameworkTest in the project named AddingFrameWork
#Artem Kislitsvn: I did see your github repository,
In which you have TestClass.Swift file outside the targets 'test' and 'FrameWorkTest'. Which is out of the scope of the namespaces.
In Swift the namespaces are specified per targets. In your case 'test' and 'FrameWorkTest' are two targets. If you keep your file out of the targets, then your class(.swift file will not visible to at any time).
The solution is very simple. You just move your TestClass.Swift file inside any of the target groups (Drag and drop through Project explorer).
Following this your project works...!
To import a Swift class from a framework:
Go to your Project -> Build phases -> Select test target -> Link Binary With Libraries -> + Button -> Add other (on the dialog) -> Select your framework and congrats!
I leave the explanation in images below:

Resources