IOS take Screenshot of full page and share in swift 4

1. Crate a swift file and paste these two as extension

import UIKit

extension UIApplication {

    var screenShot: UIImage?  {
        return keyWindow?.layer.screenShot
    }
}

extension CALayer {

    var screenShot: UIImage?  {
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(frame.size, false, scale)
        if let context = UIGraphicsGetCurrentContext() {
            render(in: context)
            let screenshot = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return screenshot
        }
        return nil
    }
}

2 . How to use :

We can use as UIApplication.shared.screenShot . If we want to set screenshot at a imageview then –

imageView.image = UIApplication.shared.screenShot

3 .Share screenshot

 let imageToShare = [UIApplication.shared.screenShot]
                    let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
                    activityViewController.popoverPresentationController?.sourceView = self.view
                    
                   
                    activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]
                    self.present(activityViewController, animated: true, completion: nil)

IOS UI Search bar with keyboard actions in swift 4

1 .Fist Create a ViewController and extends UISearchbarDelegate

SearchViewController: UIViewController, UISearchBarDelegate{}

2 . Register search bar as like in ViewDidLoad

self.searchBar.delegate = self

3. Now call keyboard search action func

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
         print(searchBar.text)
 }

4 . Cancel button function

 func searchBarCancelButtonClicked(_ searchBar: UISearchBar){
        searchBar.resignFirstResponder()
  
}

Complete Code


import UIKit

class SearchViewController: UIViewController ,UISearchBarDelegate{

    lazy var searchBar = UISearchBar(frame: CGRect.zero)

   override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.placeholder = "Search"
        navigationItem.titleView = searchBar
        searchBar.text = "this"
        
 
        self.searchBar.delegate = self
        
   
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
        
    }
    

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar){
        searchBar.resignFirstResponder()
        print("cancel")
    }
   
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
         print(searchBar.text)
    }
   
}