Run the Project and ensure it succeeds. Right-click on your .framework file created under the Products folder and click on 'Show in Finder'. The Finder will show you the framework that has been created.
Note
In the below image, you can see the framework is created in
the Debug-iphonesimulator folder. This is because I have run my project
using the simulator option. If you have run the project using a real
iPhone device or by selecting the option as GenericOS Device, the
framework will be created in the Debug-iphoneos folder.
Congrats! We have successfully created the framework!
Now let's use the newly-created framework in an application. As you can see, I have created a framework which will add two numbers, so let's create an application for the addition of two numbers.
Create a new project using Single View App. Design a UI on the Main.storyboard. In the below image, you can see I have created a UI with two text boxes, a button which will perform the addition function, and a text box to display the result.
Connect all the tools used (i.e TextBoxes, Button,Labels) to the ViewController.swift file using the assistant editor.
Now, go back to your previous Framework project and copy the framework that has been created in the 'Debug-iphonesimulator/Debug-iphoneos' folder. Follow the below steps shown in the images.
Open your current project. Right-click on your Project with the blue icon present on the left-hand side and click 'Show in Finder'. Paste the copied framework inside your current project directory. Also, add the framework in 'Framework,Libraries and Embedded Content'.
Copy and paste the code for addition operation inside the Add button in the ViewController.swift file. Check if the functions you used inside the framework appear here. Refer to the code below.
Note
Do not forget to add 'import AddTwoNumbers(name of the framework)' inside the files you work with.
- import UIKit
- import AddTwoNumbers
- class ViewController: UIViewController {
- @IBOutlet weak
- var lblNum1: UILabel!@IBOutlet weak
- var lblResult: UILabel!@IBOutlet weak
- var lblNum2: UILabel!@IBOutlet weak
- var txtResult: UITextField!@IBOutlet weak
- var txtNum2: UITextField!@IBOutlet weak
- var txtNum1: UITextField!@IBAction func btnAdd(_ sender: Any) {
- let no1 = Int(txtNum1.text!)
- let no2 = Int(txtNum2.text!)
- let result = Int(SumOfTwoNumbers(num1: no1!, num2: no2!))
- txtResult.text = "\(result)"
- }
- override func viewDidLoad() {
- super.viewDidLoad()
-
- }
- }
Run the application and ensure it succeeds.
Run and check if the application executes properly in the simulator/device.
Some important tips
- Check if you have added 'public' or 'open' keywords to the functions and classes you have created inside your framework.
- Ensure you have run the framework project and it succeeds before importing it into another project.
- Ensure you have imported the framework properly inside your application according to the steps explained above.
- Check if you have added the line 'import (FrameworkName)' in all the files where you need to use the framework.