If you're like me, you've probably already started writing a Swift application to start learning the new language and to get familiar with what it provides. As you are writing the Swift app, you will quickly find that you will need to use an Objective-C framework, but where do you start? This blog post aims to show you how to use the Telerik UI for iOS in your next Swift app in just a few minutes.
Create Your Swift Application
Begin by creating your Swift Application or using an existing one. In the screenshot provided below, I’ve created a project and gave it a name, as well as switched the language to Swift.
Adding our Framework to be Accessed in Swift Files
Once the project is created, select “Linked Frameworks and Libraries” and hit the + button as shown below then, add in TelerikUI.framework. Note: This assumes that you have already downloaded and installed UI for iOS.
Create the Bridging Header and Configure our Project
Now that we have the TelerikUI.framework added to our project, we need to simply add an “Empty Objective-C File” to our project. Go to File -> New -> File and select Objective-C File as shown below:
It will prompt you for a filename, and you can give it any name you want. Leave the File type as Empty File and press next.
It will ask you where to save it, so leave the default location as is in the next dialog. Finally, you will see this prompt appear: “Would you like to configure an Objective-C bridging header?”
Select “Yes” on this screen, and you will notice your Swift project now has two files:
- The .h file: This is the header file.
- The .m file: This is the implementation file
Inside the .m file, you will see that it already added some comments and a reference to the Foundation framework. You can leave this file as is.
Inside the .h file, they’ve left a comment that says the following: “Use this file to import your target's public headers that you would like to expose to Swift.” Here we are going to add two lines:
The first is to a required framework for Telerik UI for iOS, and the second import statement is for the UI for iOS Framework.
Using UI for iOS in Swift
We can now switch over to our ViewController.swift or whatever file we would like to use the Telerik framework in and add some code. In this sample, I’m going to create a simple chart to make sure my project has recognized that I’ve added UI for iOS.
I'll test it with the source code provided here.
Success! My app is using UI for iOS successfully in a Swift Application.
You are now ready to begin using our controls in your next Swift app!
FYI: If you happen to delete the bridging header, you will need to create an .h file and go to the plist project file and set the appropriate bridging header name.
I hope this post gave you a look at how easy it is to useTelerik UI for iOS in your next Swift Application. While I only covered the chart, this can be applied to all of our controls such as ListView, DataForm, Alert, Calendar, SideDrawer and more! Feel free to download a trial of Telerik UI for iOS and checkout our new DataForm and Alert controls. I should also mention, that our documentation has complete samples for all of our controls in Swift as well as Objective-C, so use what language is best for you and your team.
Thanks for reading and if you have any questions, then feel free to leave a comment below.