Skip to content

Instantly share code, notes, and snippets.

@Achyut-Sagar
Created February 17, 2020 07:07
Show Gist options
  • Select an option

  • Save Achyut-Sagar/ead6b7bf881b0ce185e58475bce3761c to your computer and use it in GitHub Desktop.

Select an option

Save Achyut-Sagar/ead6b7bf881b0ce185e58475bce3761c to your computer and use it in GitHub Desktop.
import MobileCoreServices
import Foundation
import UIKit
struct GeneralUtility{
public static var appDelegate : AppDelegate{
get{
return UIApplication.shared.delegate as! AppDelegate
}
}
public static func getVCFromSBId(sb_id : String) -> UIViewController{
let sb = UIStoryboard.init(name: "Main", bundle: nil)
return sb.instantiateViewController(withIdentifier: sb_id)
}
public static func showToast(title : String, msg : String,vc : UIViewController){
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
vc.present(alert, animated: true)
// duration in seconds
let duration: Double = 1
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
alert.dismiss(animated: true)
}
}
public static func showAlert(title : String, msg : String,vc : UIViewController){
let alert = UIAlertController.init(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Okay", style: .cancel, handler: nil))
vc.present(alert, animated: true, completion: nil)
}
public static func getVCFromView(view : UIView) -> UIViewController{
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindow.Level.alert + 1;
alertWindow.makeKeyAndVisible()
return alertWindow.rootViewController!
}
public static func addDropShadowToView(view : UIView,shadowColor : UIColor, opacity : Float,offset : CGSize, radius : CGFloat){
view.layer.shadowColor = shadowColor.cgColor
view.layer.shadowOpacity = opacity
view.layer.shadowOffset = offset
view.layer.shadowRadius = radius
}
public static func dateToString(date : Date, format : String) -> String{
let formatter = DateFormatter()
formatter.dateFormat = format
let dateString = formatter.string(from: date)
return dateString
}
public static func stringToDate(dateString : String, format : String) -> Date?{
let formatter = DateFormatter()
formatter.dateFormat = format
let date = formatter.date(from: dateString)
return date
}
public static func heightForLabelView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
let label:UILabel = UILabel(frame: rect)
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
public static func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: testStr)
}
static func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment