iOS系统剪贴板有两种:公共剪贴板和私有剪贴板。
- 公共剪贴板:用于不同APP之间传递数据
UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard]; //快捷创建方法
UIPasteboard *generalPasteboard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:TRUE];
第一个参数UIPasteboardNameGeneral
代表取的是公共剪贴板,也可以传任意字符串,则创建的是私有剪贴板;
第二个参数传YES or TRUE,表示如果当前没有该Name的剪贴板,就创建一个剪贴板。
- 私有剪贴板:用于同一个Team的APP之间传递数据
UIPasteboard *uPasteboard = [UIPasteboard pasteboardWithUniqueName]; //快捷方式
UIPasteboard *pPasteboard = [UIPasteboard pasteboardWithName:@"privatePasteBorad" create:TRUE];
- 删除剪贴板
+ (void)removePasteboardWithName:(UIPasteboardName)pasteboardName;
系统公共剪贴板通过该接口是无法删除的。
- 剪贴板数据类型
//单数据
@property(nullable,nonatomic,copy) NSString *string;
@property(nullable,nonatomic,copy) NSURL *URL;
@property(nullable,nonatomic,copy) UIImage *image;
@property(nullable,nonatomic,copy) UIColor *color;
//多数据
@property(nullable,nonatomic,copy) NSArray<NSString *> *strings;
@property(nullable,nonatomic,copy) NSArray<NSURL *> *URLs;
@property(nullable,nonatomic,copy) NSArray<UIImage *> *images;
@property(nullable,nonatomic,copy) NSArray<UIColor *> *colors;
//是否存在该类型的数据(iOS10以上)
@property (nonatomic, readonly) BOOL hasStrings;
@property (nonatomic, readonly) BOOL hasURLs;
@property (nonatomic, readonly) BOOL hasImages;
@property (nonatomic, readonly) BOOL hasColors;
示例:复制文本到剪贴板
UIPasteboard *generalPasteboard = [UIPasteboard generalPasteboard];
generalPasteboard.string = "hello world";