IOS 14 shear board pop prompt to find out, with Taobao implementation method analysis and demo

Time:2020-11-20

With the release of IOS 14, the abuse of clipboards is also known. As long as the app reads the contents of the clipboard, the system will pop up a reminder at the top, and this reminder cannot be turned off. In this way, you can see which apps use the shear board in the process of using the app.

It happens that our own application also uses the shear board. After upgrading IOS 14, it’s really annoying. I just thought about how to deal with it. I turned it overUIPasteboardThe relevant content is not much.
readUIPasteboardOfstringstringsURLURLsimageimagescolorcolorsThe system prompt will be triggered.
usehasStringshasURLshasImageshasColorsThe system prompt will not be triggered when waiting for the method.
The idea is to call as few methods as possible that will trigger the system prompt, and then call those methods when they really need to be read according to other methods.
According to our own situation, only judgmenthasStringsbyYESIn fact, it’s still a bit unsatisfactory to read the clipboard, but it can’t clear the clipboard. Then I see this attribute again

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

Then a condition is added to record the actual reading of the shear boardchangeCountIf there is no change in the next read, it will not be read.
In this way, the effect is much better. During the application life cycle, the prompt will only pop up once.

However, later saw Taobao more powerful, hit the real share out of the content will play, others will not play. Then I studied Taobao’s sharing content and new API documents, and got the answer.
Let’s take a look at the sharing content of Taobao

Content of 9 pieces of treatment http: T $ajg8c4ifw3q $open.. Bao [Guizhou Maotai Feitian 53 degree Maotai flavor liquor collection 500ml * 1 Single Bottle Gift height]

Components: the situation of number + text + link, combined with the new API provided by IOS 14:

detectPatternsForPatterns:completionHandler:
detectPatternsForPatterns:inItemSet:completionHandler:                                

andUIPasteboardDetectionPatternThe system only provides three types:

  1. numberUIPasteboardDetectionPatternNumber
  2. linkUIPasteboardDetectionPatternProbableWebURL
  3. searchUIPasteboardDetectionPatternProbableWebSearch

Bold guess, Taobao should be by judging whether the number and link rules to determine whether hit the shared content.

Then a demo is written to verify. The core code is as follows:

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: @ "warm prompt" message: [nsstring stringwithformat: @ "% @ \ n% @", [board string], @ "meet Taobao's reading standard"] safe rredStyle:UIAlertControllerStyleAlert ];
                Uialertaction * cancelaction = [uialertaction actionwithtitle: @ "OK" style:UIAlertActionStyleCancel handler :nil];
                [alert addAction:cancelAction];
                [self presentViewController:alert animated:YES completion:nil];
            });
        }
    }];

And then we construct one that looks like it meets the criteria1 http:/123abcWe found that the performance of demo and Taobao results are consistent, and both play the system prompt.

You can go to the specific demogithubDownload.

Original address:https://y500.me/2020/09/27/io…

In this paper, blog group send one article, multi post and other operation tool platformOpenWriterelease