iPhone 粘贴板的基本用法
我正在尝试在 iPhone 粘贴板中放入一些纯文本。 以下代码似乎不起作用:
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
NSString *value = @"test";
[pboard setValue: value forPasteboardType: @"public.plain-text"];
我猜问题出在 PasteBoard 类型参数中。 传递 @"public.plain-text"
没有任何反应。 传递 kUTTypePlainText 时,编译器会抱怨指针类型不兼容,但不会崩溃,也不会发生任何事情。 使用 kUTTypePlainText 似乎还需要与 MobileCoreServices 链接,文档中没有提到这一点。
I'm trying to put some plain text in the iPhone Pasteboard. The following code doesn't seem to work:
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
NSString *value = @"test";
[pboard setValue: value forPasteboardType: @"public.plain-text"];
I'm guessing the problem is in the PasteBoard type argument. Passing @"public.plain-text"
nothing happens. Passing kUTTypePlainText
the compiler complains about incompatible pointer type, but doesn't crash, and nothing happens either. Using kUTTypePlainText
also seems to require linking with MobileCoreServices
, which is not mentioned in the docs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用此标头获取 kUTTypeUTF8PlainText 的值;
您需要有可用的 MobileCoreServices 框架。
Use this header to get the value for kUTTypeUTF8PlainText;
You'll need to have the MobileCoreServices framework available.
回应评论和我自己的问题:
pasteboard
字符串属性有效。kUTTypeUTF8PlainText
而不是kUTTypePlainText
作为粘贴板类型,则使用setValue:forPasteboardType:
也有效。我没有注意到字符串属性,因为我直接进入了“获取和设置单个粘贴板项目”任务部分。
我测试的方法是单击文本字段,看看是否会出现粘贴弹出窗口。
我仍然不确定文档中的 UTT 类型在 iPhone 中的解释,包括从哪里获取它们(框架,#include 文件),似乎“统一类型标识符概述”文档是仍然面向Mac OS。 由于常量给了我一个类型不匹配警告,我认为我做错了什么,这就是我第一次尝试使用
NSString
文字的原因。Responding to the comments and my own question:
pasteboard
string property works.setValue:forPasteboardType:
also works if I usekUTTypeUTF8PlainText
instead ofkUTTypePlainText
for the pasteboard type.I had not noticed the string property because I went directly to the "Getting and Setting Single Pasteboard Items" tasks section.
The way I was testing was by clicking in a text field and see if the paste pop-up would appear.
I still am not sure where in the docs the UTT types are explained for the iPhone, including where to get them (Framework,
#include
files), it seems that the "Uniform Type Identifiers Overview" doc is still geared toward Mac OS. Since the constants gave me a type mismatch warning I thought I was doing something wrong, that's why I first tried using anNSString
literal.这是我将文本粘贴到粘贴板上的实验。 我正在使用按钮以编程方式添加文本。
我将内容粘贴到文本字段中进行检查,并每次更改文本内容以确保它不仅仅是重复使用以前的粘贴。
Here's my experiments with pasting text onto the pasteboard. I'm using a button to add the text programatically.
I pasted the contents into a text field to check, and changed the text contents each time to make sure it wasn't just re-using the previous paste.