Skip to content

Instantly share code, notes, and snippets.

@janodev
janodev / requestAuthorization.swift
Created August 16, 2016 12:57
Request authorization
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.sound, .alert, .badge]) { (granted, error) in
// ...
}
@janodev
janodev / registerToken.swift
Created August 16, 2016 12:57
Register the token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
let token = deviceToken.description.components(separatedBy: CharacterSet(charactersIn: "<> ")).joined(separator: ""
dlog(token, .Info)
}
@janodev
janodev / handleNotifications.swift
Created August 16, 2016 12:59
Handle the notifications
@import UserNotifications;
extension NotificationManager: UNUserNotificationCenterDelegate
{
// Called because the user interacted with a notification. Actions may be dismiss, open, or choosing an action.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)
{
print(response)
}
@janodev
janodev / BaseConverter.swift
Created August 20, 2016 20:29
A base converter in Swift 3. For instance, BaseConverter.representNumber(number: 3405691582, inBase: 16) is "cafebabe"
import Foundation
public struct BaseConverter
{
static let alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 62 digits
static func representNumber(n: Int, asciiAlphabet: String) -> String
{
let base = asciiAlphabet.lengthOfBytes(using: String.Encoding.utf8)
if (n < base){
@janodev
janodev / gist:76588cc804bc34766328dc5962b10f15
Created January 4, 2017 18:30
Kill dupes in Open With context menu
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user;killall Finder
@janodev
janodev / diff.mdown
Created January 13, 2017 15:41 — forked from ndarville/diff.mdown
Paul Heckel's Diff Algorithm

[Isolating Differences Between Files][paper]

Advantage over Other Algorithms

The diff output is more specific:

[I]f a whole block of text is moved, then all of it, rather than just the beginning and end, is detected as changed.

>The algorithm described here avoids these difficulties. It detects differences that correspond very closely to our intuitive notion of difference.

@janodev
janodev / Optional+Extension.swift
Created February 18, 2017 10:32
Chainable closures for optional types.
import Foundation
extension Optional
{
@discardableResult
func ifSome(_ handler: (Wrapped) -> Void) -> Optional {
switch self {
case .some(let wrapped): handler(wrapped); return self
case .none: return self
}
@janodev
janodev / resign.sh
Created March 28, 2017 15:12 — forked from mcxiaoke/resign.sh
A simple tool for resigning an iOS app ipa with a new certificate/mobileprovision
#!/usr/bin/env bash
if [[ ! ( # any of the following are not true
# 1st arg is an existing regular file
-f "$1" &&
# ...and it has a .ipa extension
"${1##*.}" == "ipa" &&
# 2nd arg is an existing regular file
-f "$2" &&
# ...and it has an .mobileprovision extension
import UIKit
import PluggableApplicationDelegate
@UIApplicationMain
class AppDelegate: PluggableApplicationDelegate
{
override var services: [ApplicationService] {
return [
LoggerApplicationService()
]
enum Colors: Int64
{
case black = 0x000000ff
case white = 0xffffffff
func color() -> UIColor
{
let red = CGFloat((self.rawValue >> 24) & 0xff) / 255.0
let green = CGFloat((self.rawValue >> 16) & 0xff) / 255.0
let blue = CGFloat((self.rawValue >> 8) & 0xff) / 255.0