Monday, 20 November 2017

Custom Activity Indicator



//
//  ViewController.swift
//  MyFirstApp
//
//  Created by Divakar on 19/10/17.
//  Copyright © 2017 Divakar. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        customActivityIndicatory(self.view, startAnimate: true)
        
        //hide activityIndicatorView
     //   customActivityIndicatory(self.view, startAnimate: false)
    }

    
    
    @discardableResult
    func customActivityIndicatory(_ viewContainer: UIView, startAnimate:Bool? = true) -> UIActivityIndicatorView {
        let mainContainer: UIView = UIView(frame: viewContainer.frame)
        mainContainer.center = viewContainer.center
        mainContainer.backgroundColor = hexStringToUIColor(hex: "#FFFFFF")
        mainContainer.alpha = 0.5
        mainContainer.tag = 789456123
        mainContainer.isUserInteractionEnabled = false
        
        let viewBackgroundLoading: UIView = UIView(frame: CGRect(x:0,y: 0,width: 80,height: 80))
        viewBackgroundLoading.center = viewContainer.center
        viewBackgroundLoading.backgroundColor = hexStringToUIColor(hex: "#444444")
        viewBackgroundLoading.alpha = 0.5
        viewBackgroundLoading.clipsToBounds = true
        viewBackgroundLoading.layer.cornerRadius = 15
        
        let activityIndicatorView: UIActivityIndicatorView = UIActivityIndicatorView()
        activityIndicatorView.frame = CGRect(x:0.0,y: 0.0,width: 40.0, height: 40.0)
        activityIndicatorView.activityIndicatorViewStyle =
            UIActivityIndicatorViewStyle.whiteLarge
        activityIndicatorView.center = CGPoint(x: viewBackgroundLoading.frame.size.width / 2, y: viewBackgroundLoading.frame.size.height / 2)
        if startAnimate!{
            viewBackgroundLoading.addSubview(activityIndicatorView)
            mainContainer.addSubview(viewBackgroundLoading)
            viewContainer.addSubview(mainContainer)
            activityIndicatorView.startAnimating()
        }else{
            for subview in viewContainer.subviews{
                if subview.tag == 789456123{
                    subview.removeFromSuperview()
                }
            }
        }
        return activityIndicatorView
    }

        func hexStringToUIColor (hex:String) -> UIColor {
            var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
    
            if (cString.hasPrefix("#")) {
                cString.remove(at: cString.startIndex)
            }
    
            if ((cString.characters.count) != 6) {
                return UIColor.gray
            }
    
            var rgbValue:UInt32 = 0
            Scanner(string: cString).scanHexInt32(&rgbValue)
    
            return UIColor(
                red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
                green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
                blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
                alpha: CGFloat(1.0)
            )
        }
}


Saturday, 18 November 2017

Homebrew and Carthage Intallation




Homebrew and Carthage Intallation 


To install Carthage and create Cartfile, Follow below step - 

A. Install Homebrew

Website :  https://brew.sh
  1. Open terminal
  2. Paste below code and enter - Command  :  /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  3. Enter, wait for while

B. Install Carthage
  1. Download the latest “Carthage.pkg” from this website : https://github.com/Carthage/Carthage/releases, ( In my case - 0.26.2 )
  2. Double-click Carthage.pkg to run the installer
  3. Click Continue, select a location to install to, click Continue again, and finally click Install

Note : When you attempt to run the installer, you may see a message stating “Carthage.pkg can’t be opened because it is from an unidentified developer.” If so, Control-click the installer and choose Open from the context menu.

C. Check Carthage Version
  1. Open Terminal & Hit this command - carthage version

D. Creating First Cartfile

A Cartfile is a simple text file that describes your project’s dependencies to Carthage, so it can determine what to install. Each line in a Cartfile states where to fetch a dependency from, the name of the dependency, and optionally, which version of the dependency to use. A Cartfile is the equivalent of a CocoaPods Podfile.
Navigate to the root directory of your project in Terminal (the directory that contains your .xcodeproj file) using the cd command:

cd ~/Path/To/Starter/Project

Create an empty Cartfile with the touch command:

touch Cartfile

And then open the file up in Xcode for editing:

open -a Xcode Cartfile

If you’re familiar with another text editor, like Vim, then feel free to use that instead. Don’t, however, use TextEdit to edit the file; with TextEdit it’s too easy to accidentally use so-called “smart quotes” instead of straight quotes, and they will confuse Carthage.
Add the following lines to the Cartfile and save it:
github "Alamofire/Alamofire" == 4.5
github "Alamofire/AlamofireImage" ~> 3.2
These two lines tell Carthage that your project requires Alamofire version 4.5, and the latest version of AlamofireImage that’s compatible with version 3.2.





/////
 FOR SOCKET
Carthage
Add this line to your Cartfile:
github "socketio/socket.io-client-swift" ~> 13.0.0
Run carthage update --platform ios,macosx.
Add the Starscream and SocketIO frameworks to your projects and follow the usual Carthage process.
CocoaPods 1.0.0 or later
Create Podfile and add pod 'Socket.IO-Client-Swift':
use_frameworks!

target 'YourApp' do
    pod 'Socket.IO-Client-Swift', '~> 13.0.0'
end
Install pods:
$ pod install
Import the module:
Swift:
import SocketIO
Objective-C:
@import SocketIO;


Summary

1. Copy path pf your project

cd ~/Path/To/Starter/Project

2. Create an empty Cartfile with the touch command:

touch Cartfile

3. And then open the file up in Xcode for editing:

open -a Xcode Cartfile

4. Add the following lines to the Cartfile and save it:


add cart file like - github "Alamofire/Alamofire" == 4.5

5. Now, Install Carthage

carthage update --platform ios,macosx


Wednesday, 15 November 2017

AlertDialog with Textfield








*****

let alertController = UIAlertController(title: "Your Title", message: "Your Subtitle here", preferredStyle: UIAlertControllerStyle.alert)
        
        alertController.addTextField(configurationHandler: nil)
        
        let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action) -> Void in
            
        }
        
        alertController.addAction(OKAction)

        present(alertController, animated: true, completion: nil)

******

       // IF DATA NULL, CALL YOUR METHOD


//        let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action) -> Void in
//            let textfield = alertController.textFields![0]
//            if textfield.text?.characters.count == 0 {
//
//              //  self.askForNickname()
//            }
//            else {
//

//            }

Monday, 13 November 2017

Passing Data using Struct in between controller or method








We can pass value using "struct" in swift 3
  1. Create a swift file in your project
  2. Create a class in the swift file created like bellow
        class StructOperation {
          struct glovalVariable {
          static var userName = String();
         }
     }
  3. Assing value to "static var userName" variable of " struct glovalVariable" in "StructOperation" class from First View Controller like
     @IBAction func btnSend(_ sender: Any) {
        StructOperation.glovalVariable.userName = "Enamul Haque";
       //Code Move to Sencon View Controller
     }
  4. In destination view controller get value like bellow
     var username = StructOperation.glovalVariable.userName;