KEMBAR78
Cocoapods and Most common used library in Swift | PDF
Dependency
Management
Cocoapods and the 

most common used library in Swift
Objective
• We are going to re-create our Weather application
using the most popular library that is widely used
by iOS developer.
• We will see the difference in term of performance
between the app created during Mini Project and
app created during Library.
Cocoapods
• Cocoapods is iOS Dependency Management tool.
• Dependency Management tools help to manage
your library , you don’t need to worry about the
dependency and versioning.
• Example of dependency management tool in other
language: Composer and Gradle.
Installing Cocoapods
• Update Ruby (Ruby is pre-installed for MAC 10.7
and above) : sudo gem update --system
• Download CocoaPods using gem: sudo gem install
cocoapods
• Install Cocoapods: pod setup
$ mkdir -p $HOME/Software/ruby
$ export GEM_HOME=$HOME/Software/ruby
$ gem install cocoapods
[...]
1 gem installed
$ export PATH=$PATH:$HOME/Software/ruby/bin
$ pod --version
Latest installation code in El
Capitan
Install Library using Pod
• Go to the path of our Project from terminal
• Create our Podfile using this command:
-pod init
• Open Podfile with Xcode using following
command:
• open -a Xcode Podfile
• Add the library needed between ‘do’ and ‘end’
• Go to your project directory and download
the library : pod install
Library repository
• We navigate to cocoapods.org or cocoacontrols.com to get the Library that can be
integrated into our app.
• We are going to add the following Library into the
• Alamofire
• SwiftyJson
• Kingfisher
• SCLAlertView
• Once the app is installed we are going to work with the workspace of the project.
Alamofire
• Alamofire is the most popular networking library for
Networking using Swift (AFNetworking for Objective
C)
• AFNetworking is built on top of Foundation URL
loading system.
• Refer to documentation for more info.
AFNetworking Sample Code
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}
Alamofire.request(.POST, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}
AFNetworking POST request
AFNetworking GET request
SwiftyJSON
• SwiftyJSON is one of the most helpful library to
facilitate the parsing of JSON.
• Due to Swift safe-type and optional binding
principal, we need to verify the type and nullity
when retrieving the JSON.
• SwiftyJSON will help us reduce the complexity of
doing all that.
Example
if let topApps = parsedObject as? NSDictionary {
if let feed = topApps["feed"] as? NSDictionary {
if let apps = feed["entry"] as? NSArray {
if let firstApp = apps[0] as? NSDictionary {
if let imname = firstApp["im:name"] as? NSDictionary {
if let appName = imname["label"] as? NSString {
//3
println("Optional Binding: (appName)")
}
}
}
}
}
if let appName = json["feed"]["entry"][0]["im:name"]["label"].string {
println("SwiftyJSON: (appName)")
}
This code in Swift:
Can be simplified to this:
MBProgressHUD
• MBProgressHUD is an iOS
library that displays a translucent
HUD with an indicator.
• We use it to inform user that a
background thread is being
executed.
• MBProgressHUD is code written
in Objective C. Cocoapods help
us to bridge the code from
Objective C to Swift.
MBProgressHUD Sample
Code
MBProgressHUD.showHUDAddedTo(self.view, animated: true)
MBProgressHUD.hideHUDForView(self.view, animated: true)
MBProgressHUD show a HUD.
MBProgressHUD hide a HUD.
Kingfisher
• Kingfisher is a lightweight and pure Swift
implemented library for downloading and caching
images from the web
• It is a UIImageView category. New method will be
added to UIImageView and you can call the
methods to load image from URL
import Kingfisher
imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)
SCLAlertView
• SCLAlertView is one of
the library that can be
found from
CocoaControls.
• You can create a nicer
UIAlertView for your
application.
SCLAlertView().showWarning("Hello
Warning", subTitle: "This is a more
descriptive warning text.") // Warning
SCLAlertView().showInfo("Hello Info",
subTitle: "This is a more descriptive info
text.") // Info

Cocoapods and Most common used library in Swift

  • 1.
    Dependency Management Cocoapods and the
 most common used library in Swift
  • 2.
    Objective • We aregoing to re-create our Weather application using the most popular library that is widely used by iOS developer. • We will see the difference in term of performance between the app created during Mini Project and app created during Library.
  • 3.
    Cocoapods • Cocoapods isiOS Dependency Management tool. • Dependency Management tools help to manage your library , you don’t need to worry about the dependency and versioning. • Example of dependency management tool in other language: Composer and Gradle.
  • 4.
    Installing Cocoapods • UpdateRuby (Ruby is pre-installed for MAC 10.7 and above) : sudo gem update --system • Download CocoaPods using gem: sudo gem install cocoapods • Install Cocoapods: pod setup
  • 5.
    $ mkdir -p$HOME/Software/ruby $ export GEM_HOME=$HOME/Software/ruby $ gem install cocoapods [...] 1 gem installed $ export PATH=$PATH:$HOME/Software/ruby/bin $ pod --version Latest installation code in El Capitan
  • 6.
    Install Library usingPod • Go to the path of our Project from terminal • Create our Podfile using this command: -pod init • Open Podfile with Xcode using following command: • open -a Xcode Podfile • Add the library needed between ‘do’ and ‘end’ • Go to your project directory and download the library : pod install
  • 7.
    Library repository • Wenavigate to cocoapods.org or cocoacontrols.com to get the Library that can be integrated into our app. • We are going to add the following Library into the • Alamofire • SwiftyJson • Kingfisher • SCLAlertView • Once the app is installed we are going to work with the workspace of the project.
  • 8.
    Alamofire • Alamofire isthe most popular networking library for Networking using Swift (AFNetworking for Objective C) • AFNetworking is built on top of Foundation URL loading system. • Refer to documentation for more info.
  • 9.
    AFNetworking Sample Code Alamofire.request(.GET,"https://httpbin.org/get", parameters: ["foo": "bar"]) .responseJSON { response in print(response.request) // original URL request print(response.response) // URL response print(response.data) // server data print(response.result) // result of response serialization if let JSON = response.result.value { print("JSON: (JSON)") } } Alamofire.request(.POST, "https://httpbin.org/get", parameters: ["foo": "bar"]) .responseJSON { response in print(response.request) // original URL request print(response.response) // URL response print(response.data) // server data print(response.result) // result of response serialization if let JSON = response.result.value { print("JSON: (JSON)") } } AFNetworking POST request AFNetworking GET request
  • 10.
    SwiftyJSON • SwiftyJSON isone of the most helpful library to facilitate the parsing of JSON. • Due to Swift safe-type and optional binding principal, we need to verify the type and nullity when retrieving the JSON. • SwiftyJSON will help us reduce the complexity of doing all that.
  • 11.
    Example if let topApps= parsedObject as? NSDictionary { if let feed = topApps["feed"] as? NSDictionary { if let apps = feed["entry"] as? NSArray { if let firstApp = apps[0] as? NSDictionary { if let imname = firstApp["im:name"] as? NSDictionary { if let appName = imname["label"] as? NSString { //3 println("Optional Binding: (appName)") } } } } } if let appName = json["feed"]["entry"][0]["im:name"]["label"].string { println("SwiftyJSON: (appName)") } This code in Swift: Can be simplified to this:
  • 12.
    MBProgressHUD • MBProgressHUD isan iOS library that displays a translucent HUD with an indicator. • We use it to inform user that a background thread is being executed. • MBProgressHUD is code written in Objective C. Cocoapods help us to bridge the code from Objective C to Swift.
  • 13.
    MBProgressHUD Sample Code MBProgressHUD.showHUDAddedTo(self.view, animated:true) MBProgressHUD.hideHUDForView(self.view, animated: true) MBProgressHUD show a HUD. MBProgressHUD hide a HUD.
  • 14.
    Kingfisher • Kingfisher isa lightweight and pure Swift implemented library for downloading and caching images from the web • It is a UIImageView category. New method will be added to UIImageView and you can call the methods to load image from URL import Kingfisher imageView.kf_setImageWithURL(NSURL(string: "http://your_image_url.png")!)
  • 15.
    SCLAlertView • SCLAlertView isone of the library that can be found from CocoaControls. • You can create a nicer UIAlertView for your application. SCLAlertView().showWarning("Hello Warning", subTitle: "This is a more descriptive warning text.") // Warning SCLAlertView().showInfo("Hello Info", subTitle: "This is a more descriptive info text.") // Info