iOS. 仿淘宝剪切板

image.png

// app从后台进入前台都会调用这个方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecomeActive) name:UIApplicationWillEnterForegroundNotification object:nil];

image.png

//读取剪切板的内容只从系统剪切板读区
//应用内保存到剪切板的内容在主动读取的时候需要过滤掉
@implementation PasteboardUtils

  • (NSString *)read {
    //创建系统剪切板
    UIPasteboard *systemBoard = [UIPasteboard generalPasteboard];
    if(!systemBoard.numberOfItems) {
    //剪切板为空
    return nil;
    }
    if([self hasMark]) {
    //剪切板数据已经标记过了,则数据来自当前应用,不予处理
    return nil;
    }
    return systemBoard.string;
    }

  • (void)save:(NSString *)content {
    //创建系统剪切板
    UIPasteboard *systemBoard = [UIPasteboard generalPasteboard];
    //将文本写入剪切板
    systemBoard.string = content;
    //给剪切板加入一条标记性的数据,只是为了检测剪切板的数据是否来自当前应用
    NSDictionary *item = @{PASTEBOARD_MARK:content};
    [systemBoard addItems:@[item]];
    }

  • (void)clear {
    //创建系统剪切板
    UIPasteboard *systemBoard = [UIPasteboard generalPasteboard];
    systemBoard.string = @"";
    }

  • (BOOL)hasMark{
    //创建系统剪切板
    UIPasteboard *systemBoard = [UIPasteboard generalPasteboard];
    if(!systemBoard.numberOfItems) {
    return YES;
    }
    NSArray *> *items = systemBoard.items;
    long count = systemBoard.numberOfItems;
    for(int i=0; i < count; i++){
    NSDictionary *item = [items objectAtIndex:i];
    if([[item allKeys] containsObject:PASTEBOARD_MARK]){
    return YES;
    }
    }
    return NO;
    }

你可能感兴趣的:(iOS. 仿淘宝剪切板)