jueves, 30 de noviembre de 2017

iOS: Facebook login

1) Create fb application and install sdk steps

pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'
pod 'FBSDKLoginKit'

2) In your app delegate:

Objective C


import FBSDKCoreKit

...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        
        return true
    }

...

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        
        let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
        
        return handled
    }

Swift

import FBSDKCoreKit

...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
       
        return true
    }

...

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        
        let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
        
        return handled
    }

3) Add login button

Add default facebook button

Swift


let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
loginButton.delegate = self
loginButton.center = view.center

...


extension ViewController: FBSDKLoginButtonDelegate {
    
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if let token = result?.token?.tokenString {
            NSLog("Token: %@", token)
        } else {
            NSLog("Error: %@", error.localizedDescription)
        }
    }
    
    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        NSLog("Logged out from facebook")
    }
}

Add custom button

Swift 

FBSDKLoginManager().logIn(withReadPermissions: ["email","public_profile"], from: self) { (result, error) in

            if let let tokenString = result?.token?.tokenString {

            } else {
                // error
            }
        }

4) In you .plist add

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb{facebook_app_id}</string>
            </array>
        </dict>
    </array>
    <key>FacebookAppID</key>
    <string>{facebook_app_id}</string>
    <key>FacebookDisplayName</key>
    <string>ROMWOD</string>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
    </array>

lunes, 13 de noviembre de 2017

iOS: TextView with placeholder text


class TextViewWithPlaceholder: UITextView {
    
    private var _placeholderTextColor: UIColor = UIColor.lightGray
    @IBInspectable var placeholderTextColor: UIColor {
        set {
            _placeholderTextColor = newValue
            textColor = newValue
        }
        get {
            return _placeholderTextColor
        }
    }
    @IBInspectable var placeholderText: String? {
        didSet {
            if text.isEmpty {
                text = placeholderText
            }
        }
    }
    
    private var initialTextColor: UIColor?
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        commonInit()
    }
    
    private func commonInit() {
        
        initialTextColor = textColor
        
        text = placeholderText
        
        NotificationCenter.default.addObserver(self, selector: #selector(didBeginEditing(_:)), name: UITextView.textDidBeginEditingNotification, object: self)
        NotificationCenter.default.addObserver(self, selector: #selector(didEndEditing(_:)), name: UITextView.textDidEndEditingNotification, object: self)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc func didBeginEditing(_ notification: NSNotification) {
        
        if let textView = notification.object as? TextViewWithPlaceholder, textView == self, text == placeholderText {
            
            text = ""
            textColor = initialTextColor
        }
    }
    
    @objc func didEndEditing(_ notification: NSNotification) {
        
        if let textView = notification.object as? TextViewWithPlaceholder, textView == self, text.isEmpty {
            
            text = placeholderText
            textColor = placeholderTextColor
        }
    }
}