C. Bess Wonders

UIPasteboard Permissions

Saturday, December 7, 2024
visibility 9

UIPasteboard Permissions

There are no explicit UIPasteboard permission operations. But, how would you know if your app has permission to paste? Its a two step process. First, check the desired has* property, like hasStrings, which does not require permission to return the value. Then attempt to access the corresponding value property, if you get nil, then you don’t have permission.

Example

func pasteButtonPressed(_ sender: Any) {
      guard UIPasteboard.general.hasStrings else {
          // No text to paste
          return
      }

      guard let string = UIPasteboard.general.string else {
         // App not authorized to paste
          return
      }

      guard string.count > 0 else {
          // No text to paste
          return
      }

      // use string…
}

I hope iOS 19 comes with a better way to check for UIPasteboard permissions.