使Ionic应用程序出现在“共享”列表中并接收数据

时间:2017-04-30 23:12:34

标签: android ios cordova ionic-framework ionic3

当用户点击图片的共享按钮时,我试图让“离子”应用出现在“共享”列表中。

Sharing example

据我了解,我必须添加类似

的内容
<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来做。
然后,我必须以某种方式处理这个意图,这对我来说是棘手的。似乎目前为意图维护的唯一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年的历史了(链接​​的iOS部分5年),并且在答案中为Android指定的项目webintent甚至不再存在。

如果有人可以帮助我,那会很棒。

还相关:

更新

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

最终结论&amp;恩惠

赏金
经过长时间的考虑,我已经结束了给予@Ghandi赏金。虽然没有人能给出完整的答案,但他是唯一一个试图回答整个问题的人 - 包括iOS部分。我没想到一个完整的代码解决方案,只是Android和iOS正确方向的指针,这就是他最接近所有答案的东西。我知道这是一个非常广泛的问题,我要感谢所有花时间回答和/或评论这个问题的人。

对于其他想要完成同样事情的人来说,这是我对所有研究和答案的总结

的Android
正如我在上面的问题中所描述的那样,您必须将这些行添加到AndroidManifest.xml。 Android将使您的应用程序显示在共享列表中。您的应用收到的数据必须通过所谓的Intent来处理。为此,您可以使用Ionic Native - Web Intent。截至9.5.2017,由于Plugin Ionic Native使用不再存在,因此无法使用。然而,我已经创建了一个issue on Github我被告知Ionic Native的下一个版本(我认为3.7.0)应该在接下来的两周内发布,应该使用{{3}来解决这个问题。我已经在上面的问题中提到了。这解决了自己必须在Ionic Framework上玩游戏并且只能使用Ionic Native的问题。

的iOS
在iOS中,它看起来有点棘手,而且在网络上也找不到它。你最好按照@Ghandi在下面的答案中提供的链接。

4 个答案:

答案 0 :(得分:6)

经过一些详细的分析,我可以得出结论:

在Android中,您可以使用cordova-plugin-intent将您的应用程序添加到共享列表中,如here所述。您还可以通过在here

所述的活动中添加意图过滤器来实现此目的

在iOS中,这有点棘手,因为没有直接插件或现成的解决方案可用于实现此目的。但是我可以在iOS共享菜单中添加应用程序的最佳链接是getting listed in share menu该链接包含用于执行此操作的Apple文档以及Info.plist中的一些调整以实现此目的。

这是我能想到的最好的答案。希望能帮助到你。欢呼声。

答案 1 :(得分:2)

要运行插件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中导入,因此您需要标识全局对象。这将在插件文件夹 - &gt; 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));
          }
      );
    }
    

答案 2 :(得分:2)

尝试

window.intentShim.registerBroadcastReceiver

或在

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

答案 3 :(得分:0)

您可以通过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);
    });
  }