4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

iOSのクリップボードについて調べた(Swift)

Posted at

クリップボードを取得する

String (Strings)

// String
if let string = UIPasteboard.general.string {
   print(string)
}
// Strings
if let strings = UIPasteboard.general.strings {
   print(strings)
}

Image (Images)

// Image
if let image = UIPasteboard.general.image {
   print(image)
}
// Images
if let images = UIPasteboard.general.images {
   print(images)
}

URL (URLs)

// URL
if let url = UIPasteboard.general.url {
   print(url)
}
// URLs
if let urls = UIPasteboard.general.urls {
   print(urls)
}

Color (Colors)

// Color
if let color = UIPasteboard.general.color {
   print(color)
}
// Colors
if let color = UIPasteboard.general.color {
   print(color)
}

クリップボードに値を設定する

String (Strings)

// String
UIPasteboard.general.string = "クリップボード"
// Strings
UIPasteboard.general.strings = ["クリップ", "ボード"]

Image (Images)

// Image
UIPasteboard.general.image = UIImage(named: "sample")
// Images
UIPasteboard.general.images = [UIImage(named: "sample1"), UIImage(named: "sample2")]

URL (URLs)

// URL
UIPasteboard.general.url = URL(string: "https://google.com・")
// URLs
UIPasteboard.general.urls = [URL(string: "https://google.com・"), URL(string: "https://www.apple.com/")]

Color (Colors)

// Color
UIPasteboard.general.color = UIColor.black
// Colors
UIPasteboard.general.colors = [UIColor.black, UIColor.white]

クリップボードの値の判定

クリップボードの値がStringかどうか

print(UIPasteboard.general.hasStrings)

クリップボードの値がUIImageかどうか

print(UIPasteboard.general.hasImages)

クリップボードの値がUIColorかどうか

print(UIPasteboard.general.hasColors)

クリップボードの値がURLかどうか

print(UIPasteboard.general.hasURLs)

その他(使えそうなのピックアップ)

クリップボードの個数

// EX) 
// UIPasteboard.general.string = "クリップボード" -> 1
// UIPasteboard.general.strings = ["クリップ", "ボード"] -> 2

print(UIPasteboard.general.numberOfItems)

クリップボードの名前

print(UIPasteboard.general.name.rawValue)

クリップボードの変更回数

print(UIPasteboard.general.changeCount)

クリップボードの値の型

// String -> ["public.utf8-plain-text"]
// Image -> ["com.apple.uikit.image", "public.png", "public.jpeg"]
// URL -> ["public.url", "public.utf8-plain-text"]
// Color -> ["com.apple.uikit.color"]


print(UIPasteboard.general.types)
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?