Framework
Sometimes we need to write the many repetitive functions which are used in many places as well as in many projects, for this we need to define the common class and where we declare and define all function . for doing this we can make common framework where we can only include in our app and access all its functionality.
There are two ways to use:
1: Include the whole framework like include sample.xcodeproj
2: Using Pod (where we can get time to time update).
Now we are describing how to start:
1: Go to file and select new project.
2: Here select “Cocoa Touch Framework” and named as “Sample”
3: It will default add .h file in root directory.
4: Now you can take the swift file or (objective c file) by simply on add.
5: Example:
We have taken HelperClass.swift file then it will show like that:
1 2 |
class HelperClass: NSObject { } |
For accessing in your project then we have declared as “Public” then it will be accessible.
1 2 |
public class HelperClass: NSObject { } |
6: Now start writing the helper function :
Here we have taken the
example of Email check :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class HelperClass: NSObject { public func getData()->String{ return "mmmm"; } public func checkValidEmail(data:String) -> Bool{ let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex) return (emailTest.evaluate(with: data)) } } |
7: Now you have yo use this :
a: Build this project and close;
8: Copy the Sample project ( Your framework) in your usable project folder.
9: double tap on Sample project and it will show “SampleFramework.xcodeproj” now drag and drop to the root of your project.
10: Now select your usable project and go to target and include “Sample.framework”;
11: Now you able to import your framework.
12:
1 |
var dd = HelperClass(); |
13: and now you are able to use the function which you have declared in your HelperClass.
Note:
In Next update, we will provide the information regarding “how to use this using “POD” “