我对 UIPasteboard 有一个奇怪的问题。
我正在从 Safari 复制文本,然后在我的应用程序中查看 UIPasteboard 以查看它是否包含使用此代码的任何数据:
[[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObject:@"public.utf8-plain-text"]]
它可以在模拟器上正常工作,但不能在 iPad 上工作。这是因为任何字符集问题吗?
我对 UIPasteboard 有一个奇怪的问题。
我正在从 Safari 复制文本,然后在我的应用程序中查看 UIPasteboard 以查看它是否包含使用此代码的任何数据:
[[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObject:@"public.utf8-plain-text"]]
它可以在模拟器上正常工作,但不能在 iPad 上工作。这是因为任何字符集问题吗?
由于字符集,我遇到了这个问题,但我不确定它是否仅适用于 IOS 5.0 或所有版本。但我已经用一个简单的解决方案解决了我的问题,
UIPasteboardTypeListString
[[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
if (pasteboard.string != nil) { [self insertText:pasteboard.string]; }
我希望这个答案可以帮助任何人。
从我自己的经验看来,在 iOS 5 中纯文本不再以 . 结尾public.utf8-plain-text
,而是以public.text
. 使用UIPasteboardTypeListString
而不是显式指定字符串也可以。
所以我现在在我的代码中使用以下内容来检测粘贴板中的纯文本:
[[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:@"public.utf8-plain-text", @"public.text", nil]]
或者
[[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString]
您是在模拟器中针对 iOS 4.x 进行测试并且您的 iPad 安装了 iOS 5 的情况吗?