关于objective-c:iOS14剪切板弹提示一探究竟附淘宝实现方法分析和demo

3次阅读

共计 2943 个字符,预计需要花费 8 分钟才能阅读完成。

随着 iOS 14 的公布,剪切板的滥用也被大家所通晓。只有是 APP 读取剪切板内容,零碎都会在顶部弹出揭示,而且这个揭示不可能敞开。这样,大家在应用 APP 的过程中就可能看到哪些 APP 应用了剪切板。

正好咱们本人的利用也应用了剪切板,降级了 iOS 14 之后弹的着实让人心烦。就想着怎么解决一下,翻了一下 UIPasteboard 的文档,发现相干的内容并不多。
读取 UIPasteboardstringstringsURLURLsimageimagescolorcolors的时候会触发零碎提醒。
应用 hasStringshasURLshasImageshasColors 等办法的时候不会触发零碎提醒。
那么思路就是尽可能少的去调用会触发零碎提醒的办法,依据其余办法去判断的确须要读取的时候再去调用那些办法。
依据咱们本人的状况,只有判断 hasStringsYES就去读取,又不能清剪切板,其实还是有点不尽人意,而后又看到这个属性:

@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,如果下次读取的时候没有发生变化则不读取。
这样一来成果就好多了,利用运行生命周期内,基本上只会弹出一次提醒。

然而,起初看到淘宝更牛逼,命中了真正分享进去的内容才会弹,其余则不会弹。而后就钻研了一下淘宝的分享内容和新的 API 文档,大略失去了答案。
先看下淘宝的分享内容:

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

组成部分:数字 + 文字 + 链接的局势,再联合 iOS 14 提供的新 API:

detectPatternsForPatterns:completionHandler:
detectPatternsForPatterns:inItemSet:completionHandler:                                

UIPasteboardDetectionPattern 零碎只提供了三种:

  1. 数字 UIPasteboardDetectionPatternNumber
  2. 链接 UIPasteboardDetectionPatternProbableWebURL
  3. 搜寻UIPasteboardDetectionPatternProbableWebSearch

大胆猜想,淘宝应该是通过判断是否合乎数字和链接的规定来判断是否命中分享内容。

而后就写了一个 demo 来验证,外围代码如下:

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 和淘宝的后果的体现是统一的,都弹了零碎提醒。

具体的 demo 可去 github 下载。

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

本文由博客群发一文多发等经营工具平台 OpenWrite 公布

正文完
 0