iOS14剪切板探究,淘宝实现方法分析

随着iOS 14的发布,剪切板的滥用也被你们所知晓。只要是APP读取剪切板内容,系统都会在顶部弹出提醒,并且这个提醒不可以关闭。这样,你们在使用APP的过程当中就可以看到哪些APP使用了剪切板。react

正好咱们本身的应用也使用了剪切板,升级了iOS 14以后弹的着实让人心烦。就想着怎么处理一下,翻了一下UIPasteboard的文档,发现相关的内容并很少。
读取UIPasteboardstringstringsURLURLsimageimagescolorcolors的时候会触发系统提示。
使用hasStringshasURLshasImageshasColors等方法的时候不会触发系统提示。
那么思路就是尽量少的去调用会触发系统提示的方法,根据其余方法去判断确实须要读取的时候再去调用那些方法。
根据咱们本身的状况,只有判断hasStringsYES就去读取,又不能清剪切板,其实仍是有点不尽人意,而后又看到这个属性:ios

@property(readonly, nonatomic) NSInteger changeCount;
The number of times the pasteboard’s contents have changed.

Whenever the contents of a pasteboard changes—specifically, when pasteboard items are added, modified, or removed—UIPasteboard increments the value of this property. After it increments the change count, UIPasteboard posts the notifications named UIPasteboardChangedNotification (for additions and modifications) and UIPasteboardRemovedNotification (for removals). These notifications include (in the userInfo dictionary) the types of the pasteboard items added or removed. Because UIPasteboard waits until the end of the current event loop before incrementing the change count, notifications can be batched. The class also updates the change count when an app reactivates and another app has changed the pasteboard contents. When users restart a device, the change count is reset to zero.

而后就又加了一个条件,记录一下真正读取剪切板时的changeCount,若是下次读取的时候没有发生变化则不读取。
这样一来效果就好多了,应用运行生命周期内,基本上只会弹出一次提示。git

可是,后来看到淘宝更牛逼,命中了真正分享出来的内容才会弹,其余则不会弹。而后就研究了一下淘宝的分享内容和新的API文档,大概获得了答案。
先看下淘宝的分享内容:github

9👈幅治内容 Http:/T$AJg8c4IfW3q$打開👉绹..寶👈【贵州茅台酒 茅台 飞天53度酱香型白酒收藏 500ml*1单瓶装送礼高度】

组成部分:数字+文字+连接的形势,再结合iOS 14提供的新API:app

detectPatternsForPatterns:completionHandler:
detectPatternsForPatterns:inItemSet:completionHandler:

UIPasteboardDetectionPattern系统只提供了三种:async

  1. 数字 UIPasteboardDetectionPatternNumber
  2. 连接 UIPasteboardDetectionPatternProbableWebURL
  3. 搜索UIPasteboardDetectionPatternProbableWebSearch

大胆猜想,淘宝应该是经过判断是否符合数字和连接的规则来判断是否命中分享内容。oop

而后就写了一个demo来验证,核心代码以下:post

UIPasteboard *board = [UIPasteboard generalPasteboard];
    
[board detectPatternsForPatterns:[NSSet setWithObjects:UIPasteboardDetectionPatternProbableWebURL, UIPasteboardDetectionPatternNumber, UIPasteboardDetectionPatternProbableWebSearch, nil]
                   completionHandler:^(NSSet<UIPasteboardDetectionPattern> * _Nullable set, NSError * _Nullable error) {
        
        BOOL hasNumber = NO, hasURL = NO;
        for (NSString *type in set) {
            if ([type isEqualToString:UIPasteboardDetectionPatternProbableWebURL]) {
                hasURL = YES;
            } else if ([type isEqualToString:UIPasteboardDetectionPatternNumber]) {
                hasNumber = YES;
            }
        }
        
        if (hasNumber && hasURL) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"舒适提示" message:[NSString stringWithFormat:@"%@\n%@", [board string], @"符合淘宝的读取标准"] preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleCancel handler:nil];
                [alert addAction:cancelAction];
                [self presentViewController:alert animated:YES completion:nil];
            });
        }
    }];

而后构造了一个看起来符合条件1 http:/123abc的串去反向验证了一下发现demo和淘宝的结果的表现是一致的,都弹了系统提示。ui

具体的demo可去github下载。this

原地址:https://y500.me/2020/09/27/io...