当前位置: 代码网 > it编程>App开发>苹果IOS > IOS开发UIPasteboard类的粘贴板全面详解

IOS开发UIPasteboard类的粘贴板全面详解

2024年05月15日 苹果IOS 我要评论
uipasteboard 特点和用法uipasteboard 是 swift 中用于存储和检索应用程序中剪贴板中的数据的一个类。剪贴板是应用程序之间共享数据的一种机制,uipasteboard 提供了

uipasteboard 特点和用法

uipasteboard 是 swift 中用于存储和检索应用程序中剪贴板中的数据的一个类。剪贴板是应用程序之间共享数据的一种机制,uipasteboard 提供了一种简单的方式来存储和检索应用程序中的剪贴板数据。

下面是 uipasteboard 的一些特点和用法:

  • uipasteboard 是一个公共类别,因此可以从所有应用程序中访问。
  • uipasteboard 包含两个方法:setstring:和 stringfortype:,用于将字符串添加到剪贴板中或从剪贴板中检索字符串。
  • uipasteboard 还包含一些类型和方法,如 url 类型、data 类型、image 类型和 text 类型,用于存储和检索不同类型的数据。
  • 可以使用 uipasteboard 将数据添加到剪贴板中,例如将一个 url 添加到剪贴板中,以便在其他应用程序中打开该 url。
  • 可以使用 uipasteboard 检索剪贴板中的数据,例如检索剪贴板中的 url,或检索剪贴板中的文本。
  • uipasteboard 还提供了一些方法,用于检查剪贴板中是否有特定类型的数据,例如检查剪贴板中是否有文本或 url。
    下面是一个简单的示例,演示如何使用 uipasteboard 存储和检索剪贴板中的数据:
let string = "hello, world!"  
let pasteboard = uipasteboard.general
pasteboard.setstring(string, fortype: .text)  
print("剪贴板中的内容是:\(string)")
if let string = pasteboard.string {  
    print("剪贴板中的内容是:\(string)")  
}

在ios中,uitextfield、uitextview和uiwebview等都有复制粘贴等功能。而其她控件却没有集成这些方便操作的功能。下面我将通过对粘贴板uipasteboard这个类来详细说明在ios中粘贴板的使用方法。

1、剪切板管理类uipasteboard详解

uipasteboard类有3个初始化方法,如下:

//获取系统级别的剪切板
+ (uipasteboard *)generalpasteboard;
//获取一个自定义的剪切板 name参数为此剪切板的名称 create参数用于设置当这个剪切板不存在时 是否进行创建
+ (nullable uipasteboard *)pasteboardwithname:(nsstring *)pasteboardname create:(bool)create;
//获取一个应用内可用的剪切板
+ (uipasteboard *)pasteboardwithuniquename;

上面3个初始化方法,分别获取或创建3个级别不同的剪切板,下面我们详解一下在什么情况下用哪种初始化方法

+ (uipasteboard *)generalpasteboard;系统级别的剪切板在整个设备中共享,即是应用程序被删掉,其向系统级的剪切板中写入的数据依然在。

+ (nullable uipasteboard *)pasteboardwithname:(nsstring *)pasteboardname create:(bool)create;自定义的剪切板通过一个特定的名称字符串进行创建,它在应用程序内或者同一开发者开发的其他应用程序中可以进行数据共享。

举个例子:比如你开发了多款应用,用户全部下载了,在a应用中用户拷贝了一些数据(为了数据安全,不用系统级别的pasteboard),在打开b应用时就会自动识别,提高用户体验。

+ (uipasteboard *)pasteboardwithuniquename;第3个方法创建的剪切板等价为使用第2个方法创建的剪切板,只是其名称字符串为nil,它通常用于当前应用内部。(当然也可以跨应用使用,但必须bundle identifier 例com.maoshaoqian.** 星号前部一样)

注意:使用第3个方法创建的剪切板默认是不进行数据持久化的,及当应用程序退出后,剪切板中内容将别抹去。若要实现持久化,需要设置persistent属性为yes。

下面我们来看一下uipasteboard的常用属性

//剪切板的名称
@property(readonly,nonatomic) nsstring *name;
//根据名称删除一个剪切板
+ (void)removepasteboardwithname:(nsstring *)pasteboardname;
//是否进行持久化
@property(getter=ispersistent,nonatomic) bool persistent;
//此剪切板的改变次数 系统级别的剪切板只有当设备重新启动时 这个值才会清零
@property(readonly,nonatomic) nsinteger changecount;

uipasteboard数据类型判断及其存取

//获取剪切板中最新数据的类型
- (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;
//根据数据类型获取一组数据的nsdata数据
- (nullable nsarray *)dataforpasteboardtype:(nsstring *)pasteboardtype initemset:(nullable nsindexset *)itemset;
//所有数据对象
@property(nonatomic,copy) nsarray *items;
//添加一组数据对象
- (void)additems:(nsarray<nsdictionary<nsstring *, id> *> *)items;

上面方法中很多需要传入数据类型参数,这些参数是系统定义好的一些字符窜,如下:

//所有字符串类型数据的类型定义字符串数组
uikit_extern nsarray<nsstring *> *uipasteboardtypeliststring;
//所有url类型数据的类型定义字符串数组
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;
//获取或设置剪切板中的url数据
@property(nullable,nonatomic,copy) nsurl *url;
//获取或设置剪切板中的url数组
@property(nullable,nonatomic,copy) nsarray<nsurl *> *urls;
//获取或s何止剪切板中的图片数据
@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;
//部分代码参考
- (bool)canbecomefirstresponder {
    return yes;
}
- (bool)canperformaction:(sel)action withsender:(id)sender {
        //action 会返回很多,想用哪个就写那个(action == @selector(cut:) )
    return (action == @selector(copy:) || action == @selector(paste:) );
}
-(void)copy:(id)sender{
    uipasteboard* pasteboard = [uipasteboard generalpasteboard];
    [pasteboard setimage:self.image];
    if ([self.delegate respondstoselector:@selector(transsometing:)]) {
        [self.delegate transsometing:pasteboard.image];
        nslog(@"%@",self.image);
    }
    nslog(@"您点击的是拷贝%@",pasteboard.items);
}
-(void)paste:(id)sender{
    uipasteboard* pasteboard = [uipasteboard generalpasteboard];
    uiimage *image = [pasteboard image];
    if (image) {
        self.image = image;
    }
    nslog(@"您点击的是粘贴");
}
- (void)cut:(id)sender {
    uipasteboard *pasteboard = [uipasteboard generalpasteboard];
    [pasteboard setimage:self.image];
    nslog(@"您点击的是剪切");
}
- (void)select:(id)sender {
    nslog(@"您点击的是选择");
}
-(void)selectall:(id)sender {
    nslog(@"您点击的是全选");
}

对剪切板的某些操作会触发如下通知:

//剪切板内容发生变化时发送的通知
uikit_extern nsstring *const uipasteboardchangednotification;
//剪切板数据类型键值增加时发送的通知
uikit_extern nsstring *const uipasteboardchangedtypesaddedkey;
//剪切板数据类型键值移除时发送的通知
uikit_extern nsstring *const uipasteboardchangedtypesremovedkey;
//剪切板被删除时发送的通知
uikit_extern nsstring *const uipasteboardremovednotification;
//使用举例
//当剪切板被删除时,监听通知,可处理相应事件;
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(menucontrollerwillhide) name:uipasteboardremovednotification object:nil];

2、剪切板管理类uipasteboard具体使用

我们以系统粘贴板+ (uipasteboard *)generalpasteboard;来举例子我们给uiimageview添加复制粘贴事件

//  viewcontroller.m
//  practice_uipasteboard
//
//
#import "viewcontroller.h"
#import "pasteboardlabel.h"
#import "pasteboardimageview.h"
#import <mobilecoreservices/mobilecoreservices.h>
@interface viewcontroller ()<transsometing>
@property (strong, nonatomic) iboutlet pasteboardimageview *leftimageview;
@end
@implementation viewcontroller
- (void)viewdidload {
    [super viewdidload];
    self.leftlabel.userinteractionenabled = yes;
//用于监听 uimenucontroller的变化
    [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(menucontrollerwillshow) name:uimenucontrollerwillshowmenunotification object:nil];
    [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(menucontrollerwillhide) name:uimenucontrollerwillhidemenunotification object:nil];
    // do any additional setup after loading the view, typically from a nib.
}
- (ibaction)longpressgestureaction:(uilongpressgesturerecognizer *)sender {
//要将图片变为第一响应者,而且要把图片设为**可交换状态**
        [self.leftimageview becomefirstresponder];
        self.leftimageview.userinteractionenabled = yes;
        self.leftimageview.delegate = self;
        uimenucontroller *menucontroller = [uimenucontroller sharedmenucontroller];
        [menucontroller settargetrect:self.leftimageview.frame inview:self.view];
        [menucontroller setmenuvisible:yes animated:yes];
    }
}
//  pasteboardimageview.m
//  practice_uipasteboard
#import "pasteboardimageview.h"
@implementation pasteboardimageview
//这个方法不能少
- (bool)canbecomefirstresponder {
    return yes;
}
- (bool)canperformaction:(sel)action withsender:(id)sender {
    return (action == @selector(copy:) || action == @selector(paste:) );
}
-(void)copy:(id)sender{
    uipasteboard* pasteboard = [uipasteboard generalpasteboard];
    [pasteboard setimage:self.image];
    if ([self.delegate respondstoselector:@selector(transsometing:)]) {
        [self.delegate transsometing:pasteboard.image];
        nslog(@"%@",self.image);
    }
    nslog(@"您点击的是拷贝%@",pasteboard.items);
}
-(void)paste:(id)sender{
    uipasteboard* pasteboard = [uipasteboard generalpasteboard];
    uiimage *image = [pasteboard image];
    if (image) {
        self.image = image;
    }
    nslog(@"您点击的是粘贴");
}
/*
// only override drawrect: if you perform custom drawing.
// an empty implementation adversely affects performance during animation.
- (void)drawrect:(cgrect)rect {
    // drawing code
}
*/
@end

以上就是ios开发uipasteboard类的粘贴板全面详解的详细内容,更多关于ios开发uipasteboard粘贴板的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com