Skip to content

Instantly share code, notes, and snippets.

View anirudhamahale's full-sized avatar

Anirudha Mahale anirudhamahale

View GitHub Profile
@anirudhamahale
anirudhamahale / Validations.swift
Last active January 30, 2018 06:00
Simple validation code.
extension String {
var isAllowedPhoneNumber: Bool {
guard self.count > 0 else { return false }
let nums: Set<Character> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+"]
return Set(self).isSubset(of: nums)
}
}
@anirudhamahale
anirudhamahale / CollectionViewController.swift
Last active August 20, 2017 16:12
Simple code to get the touch event in the UITableView's or UICollectionView's cell.
let tap = UITapGestureRecognizer(target: self, action: #selector(self.openWebView(sender:)))
cell.advtImage.isUserInteractionEnabled = true
cell.advtImage.addGestureRecognizer(tap)
func openWebView(sender: UITapGestureRecognizer) {
let tapLocation = sender.location(in: self.collectionView) // Returns the CGPoints of the touched location
if let indexPath = self.collectionView.indexPathForItem(at: tapLocation) // Returns the IndexPath at the above location {
if let cell = self.collectionView.cellForItem(at: indexPath) as? Advertisment2 {
if let url = URL(string: cell.url) {
UIApplication.shared.openURL(url)
@anirudhamahale
anirudhamahale / Permission.swift
Created July 31, 2017 11:13
List of the permission that we might want to run in swift.
private func isNotificationGranted() -> Bool {
if UIApplication.shared.currentUserNotificationSettings!.types == [] {
return false
} else {
return true
}
}
@anirudhamahale
anirudhamahale / TableViewController.swift
Last active July 27, 2017 04:57
Change the cell's height and attribution depending upon tapping.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductOutletsTableViewCell", for: indexPath) as! ProductOutletsTableViewCell
cell.showMoreButton.imageView?.transform = .identity
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == selectedRow {
return 80
}
@anirudhamahale
anirudhamahale / APICalls.swift
Last active October 1, 2018 04:35
JSON Encoding rest api
enum HttpMethods: String {
case get = "get"
case post = "post"
case put = "put"
case delete = "delete"
case patch = "patch"
}
private func getJSON(data: Data) -> [String: Any]? {
do {
@anirudhamahale
anirudhamahale / Date.swift
Last active July 19, 2017 05:23
Better way to manage the date.
func dateOutOf(Iso8601 date: String) -> Date {
let dateFormatter = DateFormatter()
// dateFormatter.calendar = Calendar(identifier: .iso8601)
// dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSz" //Your date format
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) //Current time zone
let date = dateFormatter.date(from: date) //according to date format your date string
return date! //Convert String to Date
}
@anirudhamahale
anirudhamahale / ViewController.swift
Created July 19, 2017 04:21
Trick when some of the content of the UITableViewCell's get chopped.
/*
When we give dynamic height for the UITableViewCell, sometimes the below line gets truncated, by doing this it can be prevented.
*/
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NotifyTableViewCell") as! NotifyTableViewCell
cell.bounds = CGRect(x: 0, y: 0, width: tableView.bounds.width * 0.9, height: 99999)
cell.contentView.bounds = cell.bounds
cell.layoutIfNeeded()
@anirudhamahale
anirudhamahale / DatePickerViewController.swift
Created July 19, 2017 04:12
Inserting Date Picker in UITextField
func insertDatePicker() {
let inputView = UIView(frame: CGRect(x: 0,y: 0, width: self.view.frame.width, height: 240))
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 240))
datePicker.datePickerMode = .date
let now: Date = Date()
var plusOneDay: DateComponents = DateComponents()
plusOneDay.day = +1
let oneDayAfter: Date = (Calendar.current as NSCalendar).date(byAdding: plusOneDay, to: now, options: NSCalendar.Options.init(rawValue: 0))!
datePicker.minimumDate = oneDayAfter
@anirudhamahale
anirudhamahale / CustomeView.swift
Last active July 19, 2017 04:03
Create custome view.
class CustomeView: UIView {
// This view is the xib's view.
@IBOutlet var view: UIView!
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
@anirudhamahale
anirudhamahale / extensions.swift
Last active July 19, 2017 04:32
Useful extensions
import CoreGraphics
extension CGFloat {
func toRadians() -> CGFloat {
return self * CGFloat.pi / 180
}
func toDegress() -> CGFloat {
return self * 180 / CGFloat.pi