Skip to content

Instantly share code, notes, and snippets.

@designablebits
Last active November 19, 2018 08:47
Show Gist options
  • Save designablebits/0d4c0e9604d3510dfaafa6ac3712a017 to your computer and use it in GitHub Desktop.
Save designablebits/0d4c0e9604d3510dfaafa6ac3712a017 to your computer and use it in GitHub Desktop.
A Collection of code snippets for my reference for iOS project, (UPDATED on regular basis).
//MARK: 01
extension UIView {
// Using a function since `var image` might conflict with an existing variable
// (like on `UIImageView`)
func asImage() -> UIImage {
if #available(iOS 10.0, *) {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
} else {
UIGraphicsBeginImageContext(self.frame.size)
self.layer.render(in:UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return UIImage(cgImage: image!.cgImage!)
}
}
}
/*
//TODO: Usage
let myView = UIView()
let myImageFromMyView = graphImage.asImage()
*/
//MARK: 01
//TODO: Hide Keyboard when clicked around
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
//MARK: 03
//TODO: check if the app launched for the first time
extension UIApplication {
class func isFirstLaunch() -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("HasAtLeastLaunchedOnce") {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "HasAtLeastLaunchedOnce")
NSUserDefaults.standardUserDefaults().synchronize()
return true
}
return false
}
}
//TODO: Call it inside appDelegate > didFinishLaunchingWithOptions
if UIApplication.isFirstLaunch() {
// do something on first launch
} else {
// go ahead with regular launch
}
//MARK: 04 / Also check repo iOS App Security for obj-c complete implementation
//TODO: Add this inside launchScreenViewController.swift > viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
if UIDevice.current.isJailBroken() {
let jailbreakVC = JailBrokenViewController()
self.navigationController?.present(jailbreakVC, animated: true, completion:nil)
} else {
//Continue to execute further
}
}
//TODO: Add below as extension
extension UIDevice {
func isJailBroken() -> Bool {
var jailBroken = false
let cydiaPath = "/Applications/Cydia.app"
let aptPath = "/private/var/lib/apt/"
if FileManager.default.fileExists(atPath: cydiaPath) {
jailBroken = true
}
if FileManager.default.fileExists(atPath: aptPath) {
jailBroken = true
}
return jailBroken
}
}
//MARK: 05
//TODO: Add this outside / as a global method to be called
func performUIUpdatesOnMain(_ updates: @escaping () -> Void) {
DispatchQueue.main.async {
updates()
}
}
//TODO: Use it anywhere you want to run things on main thread
performUIUpdatesOnMain {
//MARK: for example, get use image recieved in data form
//self.storyImage.image = UIImage(data: image)
}
//MARK: 06
//TODO: Resize Image
func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
let scale = newWidth / image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0,width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
func resizeToScreenSize(image: UIImage)->UIImage{
let screenSize = self.view.bounds.size
return resizeImage(image: image, newWidth: screenSize.width)
}
//MARK: 07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment