1、前言
iOS在某些特定时刻需要把内容重一个app拷贝到另一个app 这时候我们就可以使用剪切板UIPasteboard
2、iOS自带剪切板操作的原生UI控件
在iOS中下面三个控件,自身就有复制-粘贴的功能: (1)、UITextView (2)、UITextField (3)、UIWebView
3、系统的剪切板UIPasteboard
UIPasteboard类有3个初始化方法:
+ (UIPasteboard *)generalPasteboard;
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;
+ (UIPasteboard *)pasteboardWithUniqueName;
上面3个初始化方法,分别获取或创建3个级别不同的剪切板,系统级别的剪切板在整个设备中共享,即是应用程序被删掉,其向系统级的剪切板中写入的数据依然在。自定义的剪切板通过一个特定的名称字符串进行创建,它在应用程序内或者同一开发者开发的其他应用程序中可以进行数据共享。第3个方法创建的剪切板等价为使用第2个方法创建的剪切板,只是其名称字符串为nil,它通常用于当前应用内部。
注意:使用第3个方法创建的剪切板默认是不进行数据持久化的,及当应用程序退出后,剪切板中内容将别抹去。若要实现持久化,需要设置persistent属性为YES。
UIPasteboard中常用方法及属性如下:
@property(readonly,nonatomic) NSString *name;
+ (void)removePasteboardWithName:(NSString *)pasteboardName;
@property(getter=isPersistent,nonatomic) BOOL persistent;
@property(readonly,nonatomic) NSInteger changeCount;
下面这些方法用于设置与获取剪切板中的数据:
最新一组数据对象的存取:
- (NSArray<NSString> *)pasteboardTypes;
- (BOOL)containsPasteboardTypes:(NSArray<NSString> *)pasteboardTypes;
- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
- (nullable id)valueForPasteboardType:(NSString *)pasteboardType;
- (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType;
- (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;
多组数据对象的存取:
@property(readonly,nonatomic) NSInteger numberOfItems;
- (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet*)itemSet;
- (BOOL)containsPasteboardTypes:(NSArray<NSString> *)pasteboardTypes inItemSet:(nullable NSIndexSet *)itemSet;
- (nullable NSIndexSet *)itemSetWithPasteboardTypes:(NSArray *)pasteboardTypes;
- (nullable NSArray *)valuesForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
- (nullable NSArray *)dataForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
@property(nonatomic,copy) NSArray *items;
- (void)addItems:(NSArray<NSDictionary> *> *)items;
上面方法中很多需要传入数据类型参数,这些参数是系统定义好的一些字符窜,如下:
UIKIT_EXTERN NSArray<NSString> *UIPasteboardTypeListString;
UIKIT_EXTERN NSArray<NSString> *UIPasteboardTypeListURL;
UIKIT_EXTERN NSArray<NSString> *UIPasteboardTypeListImage;
UIKIT_EXTERN NSArray<NSString> *UIPasteboardTypeListColor;
相比于上面两组方法,下面这些方法更加面向对象,在开发中使用更加方便与快捷:
@property(nullable,nonatomic,copy) NSString *string;
@property(nullable,nonatomic,copy) NSArray<NSString> *strings;
@property(nullable,nonatomic,copy) NSURL *URL;
@property(nullable,nonatomic,copy) NSArray<NSURL> *URLs;
@property(nullable,nonatomic,copy) UIImage *image;
@property(nullable,nonatomic,copy) NSArray<UIImage> *images;
@property(nullable,nonatomic,copy) UIColor *color;
@property(nullable,nonatomic,copy) NSArray<UIColor> *colors;
对剪切板的某些操作会触发如下通知:
UIKIT_EXTERN NSString *const UIPasteboardChangedNotification;
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey;
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey;
UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;
未完待续...