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...

相關文章
相關標籤/搜索