Skip to content

Instantly share code, notes, and snippets.

View samuelbeek's full-sized avatar

Samuel Beek samuelbeek

View GitHub Profile
@samuelbeek
samuelbeek / addContact.swift
Created December 14, 2014 05:01
Add contact to address book in swift
func addUserToAddressBook(contact: User){
let stat = ABAddressBookGetAuthorizationStatus()
switch stat {
case .Denied, .Restricted:
println("no access to addressbook")
case .Authorized, .NotDetermined:
var err : Unmanaged<CFError>? = nil
var adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
if adbk == nil {
println(err)
@samuelbeek
samuelbeek / playground.swift
Created November 16, 2016 16:21
Enums with Associated Values that conform to NSCoding
// Inspiration: https://gist.github.com/NinoScript/47819cf6fe36d6845aee32d19b423e9b
//: Playground - noun: a place where people can play
import UIKit
enum SavableEnum {
case simpleCase
case associatedValue(String)
case anotherAssociated(Int)
}
extension SavableEnum {
@samuelbeek
samuelbeek / sendText.swift
Last active January 30, 2020 11:58
Send Text with MessageBird
import Foundation
import Alamofire // installation instructions for Alamofire can be found here: https://github.com/Alamofire/Alamofire
// Sends text message to phone number
func sendText(phoneNumber: String, message: String) {
let apiKey = "AccessKey YOUR_ACCES_KEY"
let originator = "YOUR_NAME"
// create a new manager instance (so you don't overwrite any other API's authorization)
var manager = Manager.sharedInstance
@samuelbeek
samuelbeek / RandomBackgroundcolor.js
Created June 18, 2015 09:56
Random Background Color Directive for Angularjs
// just add random-backgroundcolor to the element you want to give a random background color
app.directive("randomBackgroundcolor", function () {
return {
restrict: 'EA',
replace: false,
link: function (scope, element, attr) {
//generate random color
var color = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);
@samuelbeek
samuelbeek / StringContainsNumber.swift
Created November 17, 2015 15:30
Check if string contains number or whitespace
import Foundation
extension String {
func containsNumbers() -> Bool {
// check if there's a range for a number
let range = self.rangeOfCharacterFromSet(.decimalDigitCharacterSet())
@samuelbeek
samuelbeek / VerticalCenteredTextView.swift
Last active January 6, 2019 23:37
Vertically aligned text in textview
class VerticalCenteredTextView: UITextView {
override init(frame: CGRect) {
super.init(frame: frame)
addContentSizeObserver()
}
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
}
@samuelbeek
samuelbeek / CartographyExtensions.swift
Created June 14, 2016 12:54
Making Cartography compile faster.
//
// CartographyExtensions.swift
// portkey
//
// Created by Samuel Beek on 14/06/16.
//
// Note: still WIP. I only dealt with the equality operator (==), haven't worked on <= and ~ yet.
// But adding this won't be bad for you project, it saves me 70% compile time per block on avarage.
import Foundation
@samuelbeek
samuelbeek / Platform.swift
Created November 24, 2015 12:23
Detect if Simulator is used in Swift
struct Platform
{
static let isSimulator: Bool = {
var isSim = false
#if arch(i386) || arch(x86_64)
isSim = true
#endif
return isSim
}()
}
@samuelbeek
samuelbeek / ImageTitleButton.swift
Created May 1, 2017 09:43
Button that contains image and title. Their relative position can be modified.
//
// ImageTitleButton.swift
// Coyote
//
// Created by Samuel Beek on 01/05/2017.
// Copyright © 2017 Samuel Beek. All rights reserved.
//
import UIKit
@samuelbeek
samuelbeek / ForceTouchGestureRecognizer.swift
Created January 4, 2017 10:40
Swift 3 implementation of Kraken Devs' force touch recognizer
// Source: https://krakendev.io/force-touch-recognizers/
// Without this import line, you'll get compiler errors when implementing your touch methods since they aren't part of the UIGestureRecognizer superclass
import UIKit.UIGestureRecognizerSubclass
//Since 3D Touch isn't available before iOS 9, we can use the availability APIs to ensure no one uses this class for earlier versions of the OS.
@available(iOS 9.0, *)
public class ForceTouchGestureRecognizer: UIGestureRecognizer {
//Since it also doesn't make sense to have our force variable as a settable property, I'm using a private instance variable to make our public force property read-only
private var _force: CGFloat = 0.0