使 Ionic 应用出现在 "Share" 列表中并接收数据

Make Ionic app appear in "Share" list and receive data

我正在尝试让一个 Ionic 应用程序出现在“共享”列表中,例如当用户单击图片的共享按钮时。


据我所知,我必须添加类似

的内容
<intent-filter> 
   <action android:name="android.intent.action.SEND" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="image/*" />
</intent-filter>

AndroidManifest.xml。我认为我可以使用 cordova-custom-config plugin 来做到这一点。
然后我将不得不以某种方式处理该意图,这对我来说很棘手。似乎目前为 intents 维护的唯一 cordova 插件是 this one。我试过这样使用它:

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.registerBroadcastReceiver();
    });
  }
  private registerBroadcastReceiver(){
      window.plugins.intentShim.registerBroadcastReceiver({
          filterActions: [
              'com.darryncampbell.cordova.plugin.broadcastIntent.ACTION'
              ]
          },
          function(intent) {
              //  Broadcast received
              console.log('Received Intent: ' + JSON.stringify(intent.extras));
          }
      );
  }

但是这样我得到一个错误,说 window.plugins 是未定义的。 我真的不知道如何将它与 Ionic 集成

此外,这仅对 Android 有效,我希望对 iOS 也有效。 This SO question 是相关的,并提到了一种为 iOS 做的方法,但它已有大约 4 年的历史(linked iOS 部分 5 年)和指定的项目 webintent Android 中的答案甚至不存在了。

如果有人能帮助我,那就太好了。

还相关:

更新

所有答案都只关注 Android,我真的希望有人能为我指明 iOS 的正确方向,因为我在那里更需要它。 ..

最终结论和赏金

赏金
经过长时间的考虑,我决定将赏金交给@Ghandi。虽然没有人能给出完整的答案,但他是唯一一个试图回答整个问题的人——包括 iOS 部分。我并不期待完整的代码解决方案,只是 Android 和 iOS 的正确方向的指针,这是他最接近所有答案的地方。我知道这是一个非常广泛的问题,我要感谢所有花时间回答和/或评论这个问题的人。

对于试图完成同样事情的其他人,以下是我对所有研究的总结以及此处的答案

Android
正如我在上面的问题中所述,您必须将这些行添加到 AndroidManifest.xml。 Android 然后会使您的应用出现在共享列表中。您的应用收到的数据必须通过我上面的问题中提到的所谓 Intent. To do so you can use Ionic Native - Web Intent. As of 9.5.2017 this would not work yet as the Plugin Ionic Native is using does not exist anymore. I have however created an issue on Github where I have been told that the next version of Ionic Native (I think 3.7.0), which should be released in the next two weeks, should fix this by using the plugin 来处理。这解决了必须自己玩弄 Ionic Framework 并能够简单地使用 Ionic Native 的问题。

iOS
在 iOS 中,它似乎有点棘手,而且在网络上也很少找到它。最好遵循@Ghandi 在下面的回答中提供的 link。

对于 运行 插件 https://github.com/darryncampbell/darryncampbell-cordova-plugin-intent, 尝试:

  • 使用 --save 安装插件以确保插件已添加到您的 config.xml

    ionic plugin add https://github.com/darryncampbell/darryncampbell-cordova-plugin-intent --save
    
  • 由于ionic-native中没有引入此插件,需要识别全局对象。这将在插件文件夹->plugin.xml中声明。这里的对象是 intentShim.

       <js-module name="IntentShim" src="www/IntentShim.js">
          <clobbers target="intentShim" />
      </js-module>
    
  • 在您的代码中将全局对象声明为:

    declare var intentShim:any;
    

    在你的函数中,

    private registerBroadcastReceiver(){
      intentShim.registerBroadcastReceiver({
          filterActions: [
              'com.darryncampbell.cordova.plugin.broadcastIntent.ACTION'
              ]
          },
          function(intent) {
              //  Broadcast received
              console.log('Received Intent: ' + JSON.stringify(intent.extras));
          }
      );
    }
    

尝试

window.intentShim.registerBroadcastReceiver

或在

中调用函数
document.addEventListener('deviceready', function(){
    registerBroadcastReceiver() }, 
false);

经过详细分析,我得出的结论是:

在 Android 中,您可以按照 here. You can also achieve this by adding intent filter in the activity as described here

中所述使用 cordova-plugin-intent 将您的应用程序添加到共享列表中

在 iOS 中,这有点棘手,因为没有直接的插件或现成的解决方案可用于实现此目的。但是我能得到的最好的 link 与在 iOS 共享菜单中添加应用程序相关的是 getting listed in share menu link 包括执行此操作的苹果文档以及 [=20] 中的一些调整=] 来实现这个。

这是我能想到的最佳答案。希望能帮助到你。干杯。

您可以通过ionic提供的webIntent插件发送或接收数据。

Ionic:
   Ionic CLI          : 5.0.2 (C:\Windows\System32\node_modules\ionic)
   Ionic Framework    : ionic-angular 3.9.5
   @ionic/app-scripts : 3.2.2

Cordova:
   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 8.0.0
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.0, (and 5 other plugins)

Utility:
   cordova-res : not installed
   native-run  : 0.2.5

System:
   Android SDK Tools : 26.1.1 (D:\Android\Sdk)
   NodeJS            : v12.4.0 (D:\node.exe)
   npm               : 6.9.0
   OS                : Windows 8.1

安装插件的命令:

ionic cordova plugin add com-darryncampbell-cordova-plugin-intent
npm install --save @ionic-native/web-intent@4

接收数据的代码:(在提供者中添加'Web-Intent')

import { WebIntent } from '@ionic-native/web-intent';

clickMe() {
    console.log('clicked')
    this.webIntent.getIntent().then((data) => {
      console.log('Success', data);
    },
    err => {
      console.log('Error', err);
    });
  }