Monday 25 January 2016

NSURLConnection And NSURLSession

Apple has released NSURLSession APIs for iOS 7.0 onwards and it has made life a lot easier for us:

NSURLConnection Code
let queue: NSOperationQueue = …
let url = NSURL(string: “https://www.example.com”)!
let request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: queue) 
{
(response: NSURLResponse?, data: NSData?, error: NSError?) in
}



NSURLSession Code

let url = NSURL(string: “https://www.example.com”)!
let request = NSURLRequest(URL: url)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data: NSData?, response: NSURLResponse?, error: NSError?) in

}
task.resume()



Advantages of NSURLSession Over NSURLConnection:

– Fetching of data via network is easy
– Upload and download of data in the background
– Rich delegate methods
– Get the progress of loading, download, upload.
– Option to Cancel, Pause, Resume, Restart the download/upload network operations.
– Multiple session configurations to suit the application requirement
– Can provide different settings for different sessions.
– Private sessions, Cookies, Storage
– Support for HTTP1.1 and HTTP 2.0 automatically. No source code changes required.
– Uses best available connectivity (iPhone or WiFi)
– Switching to NSURLSession is easy
– Watch OS 2 only supports NSURLSession, It encourages reusability code in between iOS and Watch OS apps.
– Performance is better and well supported by Apple


Disadvantages of NSURLConnection:

– Watch OS2 does not support NSURLConnection
– Lot of coding is required
– Get the progress of loading, download, upload.




Friday 22 January 2016

Install CocoaPods

POD Install:

1) Open terminal 

2) $ sudo gem install cocoapods (gem will get installed in Ruby inside System library)

3) Enter this command in Terminal to complete the setup:
    $ pod setup


4) Install Command line tool of Xcode.
    $ xcode-select --install

5) Verify that you’ve successfully installed Xcode Command Line Tools:
$ xcode-select -p /Library/Developer/CommandLineTools

6) Create a xcode project. 

7) cd "path to your project root directory"

8) Create Podfile in your project.
    $ pod init 

9) open -a Xcode Podfile 

Podfile will get open in text mode. 
You shouldn’t use TextEdit to edit the Podfile because it likes to replace standard quotes with more graphically appealing typeset quotes. This can cause CocoaPods to become confused and results in errors being thrown, so it’s best to use Xcode or another programming text editor to edit your Podfile.


The default Podfile looks like this:

# Uncomment this line to define a global platform for your project
#platform :ios, '8.0'
# Uncomment this line if you're using Swift
#use_frameworks!

target ‘TestPod’ do

end




Replace the commented lines with the following:

# Uncomment this line to define a global platform for your project
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
# Uncomment this line if you're using Swift
use_frameworks!

This tells CocoaPods that your project is targeting iOS 8.0 and will be using frameworks instead of static libraries.



10) pod 'AFNetworking', '~> 3.0'

  It’s finally time to add your first dependency using CocoaPods! Copy and paste the following into your pod file, right after target “TestPod” do:

11) Now close pod file and execute below command on terminal
      $ pod install

Now close your xcode project and open .xcworkspace xcode project file.