Skip to content

Instantly share code, notes, and snippets.

View marcos1262's full-sized avatar

Marcos Paulo marcos1262

View GitHub Profile
@marcos1262
marcos1262 / MyViewController.swift
Last active October 27, 2018 14:26
Open URL in Safari Modal
View MyViewController.swift
import SafariServices
func openURL(from urlString: String) {
guard let url = URL(string: urlString) else { return }
let svc = SFSafariViewController(url: url)
self.present(svc, animated: true, completion: nil)
}
@marcos1262
marcos1262 / MyViewController.swift
Created October 27, 2018 14:24
Share some text through the OS
View MyViewController.swift
func share(_ text: String) {
let activityVC = UIActivityViewController(activityItems: [text], applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop,
UIActivity.ActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
}
@marcos1262
marcos1262 / MyModel.swift
Created October 27, 2018 14:11
Abbreviated date interval String formatting
View MyModel.swift
var timeString: String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.month, .day, .hour, .minute]
formatter.unitsStyle = .abbreviated
formatter.zeroFormattingBehavior = .dropAll
if let date = self.date, let interval = formatter.string(from: date, to: Date()) {
return "\(interval) ago"
}
return "unknown"
@marcos1262
marcos1262 / UIImageViewExtension.swift
Last active October 27, 2018 14:06
Asynchronous download of images and saving in caches directory
View UIImageViewExtension.swift
extension UIImageView {
//Use this function with the imageview
func loadImageUsingCacheWithUrlString(_ url: String?) {
guard let url = url else {
self.image = UIImage(named: "default")
return
}
let imageName = url.split(separator: "/").last! as NSString
let fileManager = FileManager.default
@marcos1262
marcos1262 / UIViewControllerExtension.swift
Last active October 27, 2018 14:06
Spinner indicator to put over loading views
View UIViewControllerExtension.swift
extension UIViewController {
func displaySpinner(onView : UIView) -> UIView {
let spinnerView = UIView.init(frame: onView.bounds)
spinnerView.backgroundColor = UIColor.white //UIColor.black.withAlphaComponent(0.5)
let ai = UIActivityIndicatorView(style: .gray)
ai.startAnimating()
ai.center = spinnerView.center
DispatchQueue.main.async {
spinnerView.addSubview(ai)
View UIViewControllerExtension.swift
extension UIViewController {
func showError(_ message: String?) {
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
@marcos1262
marcos1262 / DataManager.swift
Created October 27, 2018 13:43
Wrapped CoreData stack
View DataManager.swift
//
// DataManager.swift
//
// Created by Marcos Santos on 8/30/18.
// Copyright © 2018 Marcos Santos 2018. All rights reserved.
//
import CoreData
public class DataManager {
@marcos1262
marcos1262 / NSManagedObjectExtension.swift
Created October 27, 2018 13:42
Nice functions for dealing with CoreData
View NSManagedObjectExtension.swift
//
// NSManagedObject.swift
//
// Created by Marcos Santos on 8/30/18.
// Copyright © 2018 Marcos Santos 2018. All rights reserved.
//
import Foundation
import CoreData
@marcos1262
marcos1262 / ButtonWithGradient.swift
Last active August 3, 2018 11:55
Example of gradient on iOS
View ButtonWithGradient.swift
override func draw(_ rect: CGRect) {
self.layer.insertSublayer(AppDelegate.mainGradient(self.frame.width, self.frame.height), at: 0)
}
@marcos1262
marcos1262 / UIColorExtension.swift
Last active October 27, 2018 14:07
Get UIColor from RGB or Hex value
View UIColorExtension.swift
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(rgb: Int) {