Skip to content

Instantly share code, notes, and snippets.

@jabruder
Forked from kristopherjohnson/pasteboard.swift
Created September 7, 2020 14:11
Show Gist options
  • Save jabruder/2bbe0b4961172c4288201752f02b3f32 to your computer and use it in GitHub Desktop.
Save jabruder/2bbe0b4961172c4288201752f02b3f32 to your computer and use it in GitHub Desktop.
Utility functions to copy/paste text
import Foundation
#if os(iOS)
import UIKit
#else
import AppKit
#endif
/// Return string value currently on clipboard
func getPasteboardContents() -> String? {
#if os(iOS)
let pasteboard = UIPasteboard.generalPasteboard()
return pasteboard.string
#else
let pasteboard = NSPasteboard.generalPasteboard()
return pasteboard.stringForType(NSPasteboardTypeString)
#endif
}
/// Write a string value to the pasteboard
func copyToPasteboard(text: String) {
#if os(iOS)
let pasteboard = UIPasteboard.generalPasteboard()
pasteboard.string = text
#else
let pasteboard = NSPasteboard.generalPasteboard()
pasteboard.clearContents()
pasteboard.setString(text, forType: NSPasteboardTypeString)
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment