ios 跨軟件傳輸數(shù)據(jù)之Share Extension容器程序數(shù)據(jù)處理與上線(四)

容器程序獲取分享數(shù)據(jù)

插件的工作基本上已經(jīng)全部開發(fā)完成了,接下來就是容器程序獲取數(shù)據(jù)并進(jìn)行操作。下面是容器程序的處理代碼:

- (void)applicationDidBecomeActive:(UIApplication *)application
{ //獲取共享的UserDefaults
    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.cn.vimfung.ShareExtensionDemo"]; if ([userDefaults boolForKey:@"has-new-share"])
    {
        NSLog(@"新的分享 : %@", [userDefaults valueForKey:@"share-url"]); //重置分享標(biāo)識
        [userDefaults setBool:NO forKey:@"has-new-share"];
    }
}

為了方便演示,這里直接在AppDelegate中的applicationDidBecomeActive:方法中檢測是否有新的分享,如果有則通過Log打印鏈接出來。

至此,整個(gè)Share Extension開發(fā)的過程已經(jīng)完成。

提審AppStore的注意事項(xiàng)

  1. 擴(kuò)展中的處理不能太長時(shí)間阻塞主線程(建議放入線程中處處理),否則可能導(dǎo)致蘋果拒絕你的應(yīng)用。
  2. 擴(kuò)展不能單獨(dú)提審,必須要跟容器程序一起提交AppStore進(jìn)行審核。
  3. 提審的擴(kuò)展和容器程序的Build Version要保持一致,否則在上傳審核包的時(shí)候會(huì)提示警告,導(dǎo)致程序無法正常提審。(Info.plist : 里面的版本號必須要和主工程的版本號一致,否則審核可能被拒。)

PS:如果想讓Share Extension跳轉(zhuǎn)、打開宿主APP

其實(shí)蘋果官方除了Today Extension外,其他Extension是不提供跳轉(zhuǎn)接口的。所以這里總結(jié)的是兩種非正常的方式。

  • 如果只需要在點(diǎn)擊分享框的時(shí)候處理:
- (void)didSelectPost {
    // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
    
    // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
   
    NSString *str = [NSString stringWithFormat:@"%@%@", @"comcihaicihaiyun://jump/Search?searchStr=", self.contentText];
    
    [self goConnationWithUrlSTr:[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
    [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
    
}
  • 如果需要在控制器內(nèi)處理,需要在自定義的控制器內(nèi):
- (void)viewDidLoad
{
    [super viewDidLoad];
    //獲取分享內(nèi)容
    __block BOOL hasGetUrl = NO;
    [self.extensionContext.inputItems enumerateObjectsUsingBlock:^(NSExtensionItem *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [obj.attachments enumerateObjectsUsingBlock:^(NSItemProvider *  _Nonnull itemProvider, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([itemProvider hasItemConformingToTypeIdentifier:@"public.plain-text"])
            {
                [itemProvider loadItemForTypeIdentifier:@"public.plain-text" options:nil completionHandler:^(id<NSSecureCoding>  _Nullable item, NSError * _Null_unspecified error) {
                    
                    if ([(NSObject *)item isKindOfClass:[NSString class]])
                    {
                        dispatch_async(dispatch_get_main_queue(), ^{ 
                            [self goConnationWithUrlSTr:[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
                            [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
                            
                        });
                    }
                    
                }];
                
                hasGetUrl = YES;
                *stop = YES;
            }
            
            *stop = hasGetUrl;
            
        }];
        
    }];
}
  • 第一種方法在Share Extension中無法獲取到UIApplication對象,則通過拼接字符串獲取。
 - (void)goConnationWithUrlSTr:(NSString *)urlStr
{
     NSURL *destinationURL = [NSURL URLWithString:[NSString stringWithFormat:@"sharefile://%@",saveFilePath]];
    //     Get "UIApplication" class name through ASCII Character codes.
    NSString *className = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned char []){0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E} length:13] encoding:NSASCIIStringEncoding];
    if (NSClassFromString(className)) {
        id object = [NSClassFromString(className) performSelector:@selector(sharedApplication)];
        [object performSelector:@selector(openURL:) withObject:destinationURL];
    }
}
  • 第二種方法通過響應(yīng)鏈找到Host App的UIApplication對象,通過該對象調(diào)用openURL方法返回自己的應(yīng)用。
 - (void)goConnationWithUrlSTr:(NSString *)urlStr
{
    UIResponder *responder = self;
    while (responder) {
        SEL openSelector = NSSelectorFromString(@"openURL:");
        if([responder respondsToSelector:openSelector]) {
            [responder performSelector:openSelector withObject:[NSURL URLWithString:urlStr] afterDelay:1.0];
            break;
        }
        responder = [responder nextResponder];
    }
}

第二種方法最后記得添加URL Scheme


配置對應(yīng)的URL Scheme

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容