The practice of sharing iOS system to your own app

Here we mainly talk about using sharing extension ( share extension) to realize the function of sharing the system to your own app, jumping to the host app in the extension, and data transmission, etc.

1. Add sharing extension

image.png

image.png

Successful creation will generate a folder

image.png

info.plistThere are rules for activating extensions. Matching rules will cause your own app to appear in the system sharing.

NSExtensionActivationRule: The default is the string "TRUEPREDICATE", which means the extension is always displayed in the share menu. It is not available in the production environment. You must change the type to Dictionary type and then add the following fields:

NSExtensionActivationSupportsAttachmentsWithMaxCount

Attachments include three categories: File, Image and Movie. The total number of single or mixed selections does not exceed the specified amount.

NSExtensionActivationSupportsAttachmentsWithMinCount

The minimum restriction on attachments takes effect when the maximum number of fields above is set. At least 1 attachment is selected by default, and the extension icon is displayed in the sharing menu.

NSExtensionActivationSupportsImageWithMaxCount

The maximum number of images is limited, and neither single nor mixed selection exceeds the specified number.

NSExtensionActivationSupportsMovieWithMaxCount

The maximum number of videos is limited, and neither single nor mixed selection exceeds the specified number.

NSExtensionActivationSupportsText

Whether to support text type, Boolean type, not supported by default. Such as [memo] sharing

NSExtensionActivationSupportsWebPageWithMaxCount

The maximum limit of the web page, which is a numerical type. Web page sharing is not supported by default, and you need to set a value yourself.

NSExtensionActivationSupportsWebURLWithMaxCount

The maximum limit of web links, which is a numeric type. Sharing hyperlinks is not supported by default, and you need to set a value yourself.
The default sharing extension class ShareViewController comes with its own views, which can be fully customized. I won’t go into details here.

image.png

2. Get shared data and jump to the host app

 dispatch_group_t group = dispatch_group_create();

    NSExtensionItem *extensionItem = self.extensionContext.inputItems[0];
    
    for (NSItemProvider *attachment in extensionItem.attachments) {
    
    
        
        if ([attachment hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
    
    
            
            dispatch_group_enter(group);

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
    
                [attachment loadItemForTypeIdentifier:(NSString *)kUTTypeImage
                                              options:nil
                                    completionHandler:^(id<NSSecureCoding>  _Nullable item, NSError * _Null_unspecified error) {
    
    
                    
                    NSData *data = [[NSData alloc]initWithContentsOfURL:item];

                    dispatch_group_leave(group);

                    
                }];
            });
            
            
        }
        
        if ([attachment hasItemConformingToTypeIdentifier:(NSString *)kUTTypeMovie]) {
    
    
            
            
            dispatch_group_enter(group);
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
    
                
                [attachment loadItemForTypeIdentifier:(NSString *)kUTTypeMovie
                                              options:nil
                                    completionHandler:^(id<NSSecureCoding>  _Nullable item, NSError * _Null_unspecified error) {
    
    
                    
                    NSData *data = [[NSData alloc]initWithContentsOfURL:item];
                    
                    dispatch_group_leave(group);
                    
                    
                }];
            });
        }
        
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    
    
         UIResponder* responder =self;
            
         responder = [responder nextResponder];
            
         while((responder = [responder nextResponder]) !=nil) {
    
    
                
             if([responder respondsToSelector:@selector(openURL:)] ==YES) {
    
    
                    
                 //打开APP
                    
                 //这里的asancloud是app的URL Schemes  ,home是自己随便定义的,用于判断
                    
                 [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:[NSString stringWithFormat:@"custome://"]]];
                    
                 //执行分享内容处理
                    
                 [self.extensionContext completeRequestReturningItems:@[] completionHandler:NULL];
            }   
        }
    });

After jumping to the host app, handle the logic in the following method

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options;

3. Data transmission (there are 3 types, NSUserDefaults is introduced here)

First, you need to configure the same App Groups, which must start with group.

image.png

Insert image description here

NSUserDefaults *de = [[NSUserDefaults alloc]initWithSuiteName:@"group.(跟上面app groups一致)"];
[de setObject:value forKey:@"key"];

Host app gets data

NSUserDefaults *de = [[NSUserDefaults alloc]initWithSuiteName:@"group.(跟上面app groups一致)"];
id value = [de objectForKey:@"key"];

4. Debugging

image.png

The extension must choose the corresponding host app to run, but the extension is an independent process. When debugging, when the extension is run, the breakpoints in the host app will not be executed. Similarly, when the host app is run, the breakpoints in the extension will not be executed.

Guess you like

Origin blog.csdn.net/SSY_1992/article/details/132504693