Monday, April 18, 2016

Carthage Tutorial - Reachability.swift

Carthage is an alternative dependency manager to CocoaPods. Unlike the Objective-C based CocoaPods, Carthage is developed in Swift. The tutorial below shows how to use Carthage to install reachability.swift and receive network reachability notifications.

1. Install Homebrew with this command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

If Homebrew has already been installed, you'll see this:




2. Type this command in the terminal:

brew update

===== If you have no error, go to step 3. =====

You might see error like this:


warning: unable to unlink CONTRIBUTING.md: Permission denied
warning: unable to unlink SUPPORTERS.md: Permission denied
fatal: cannot create directory at '.github': Permission denied

Error: Failure while executing: git pull --ff --no-rebase --quiet origin refs/heads/master:refs/remotes/origin/master

If you see the above message, type this command:


sudo chown -R username:admin $(brew --prefix)



Then type:

git fetch --all

If this error happens:


fatal: Not a git repository (or any of the parent directories): .git

Type this:

git init

Then try git fetch --all again and type this:

git reset --hard && git clean -df

Then enter these commands:

brew doctor
brew update

Reference: homebrew not working on OSX

===== End of Step 2 Error Section =====

3. Type this command in the terminal:


brew install carthage

4. Check the version of Carthage with:

carthage version

Note: To update the carthage version, use this command:

brew upgrade carthage

5. Create a new Xcode project.

6. Go to the Xcode directory in the terminal and create an empty Cartfile with this command:

touch Cartfile


7. Open the Cartfile with Xcode using this command:


open Cartfile -a Xcode

8. Add this line to the Cartfile:

github "ashleymills/Reachability.swift"

Press [command] + [S] keys to save the Cartfile.

9. Go back to the terminal. Type:

carthage update

You should see message like this:


10. Open the Carthage folder in Finder with this terminal command:

open Carthage

Move to Carthage/Build/iOS/ in Finder.

11.  Select ProjectName -> Targets -> General -> Linked Frameworks and Libraries in Xcode. 




12. Drag Reachability.framework from Finder to Xcode.



13. Select ProjectName -> Targets -> Build Phases -> + sign -> New Run Script Phase.




14. Select Run Script. Add this command:


/usr/local/bin/carthage copy-frameworks

Select Input Files and add this line:

$(SRCROOT)/Carthage/Build/iOS/Reachability.framework


Build the code now and it should work.

15. Modify ViewController.swift in Swift 2.2 with Xcode 7.3 as below:


import UIKit
import Reachability

class ViewController: UIViewController {
    
    var reachability: Reachability?
    
    override func viewWillAppear(animated: Bool) {
        do {
            reachability = try Reachability.reachabilityForInternetConnection()
        } catch {
            print("Unable to create Reachability")
            return
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
        
        do {
            try reachability?.startNotifier()
        } catch {
            print("could not start reachability notifier")
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func reachabilityChanged(notification: NSNotification) {
        
        let reachable = notification.object as! Reachability
        
        if reachable.isReachable() {
            if reachable.isReachableViaWiFi() {
                print("WiFi")
            } else {
                print("Cellular")
            }
        } else {
            print("No Network")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Result:

WiFi
Cellular
No Network
Cellular
WiFi

Related Information:

No comments:

Post a Comment