APICloud开发者进阶之路|自己App加入系统分享

最近做一个把自己App加入系统分享的功能,分享一下自己踩坑的记录和成果;
安卓可以加入系统相册和文件管理器的分享菜单中;ios目前只做到了加入在其他应用里调起系统分享的菜单。


1.Android先配置config.xml ,Ios先配置Info.plist

//config.xml: android.intent.action.SEND你app接受的单文件,mimeType是文件格式,可以自己去参考安卓官网查询

<intent-filter> 
    <action name="android.intent.action.SEND"/>  
    <category name="android.intent.category.DEFAULT"/>  
    <data mimeType="image/*" path="/"/> 
</intent-filter>
<intent-filter> 
    <action name="android.intent.action.SEND_MULTIPLE"/>  
    <category name="android.intent.category.DEFAULT"/>  
    <data mimeType="image/*" path="/"/> 
</intent-filter>

 

//Info.plist:记得一定要配置CFBundleTypeName字段,我昨天上架不配置此字段包都传不上去了,同理 LSItemContentTypes 是你app支持的文件类型

  

<key>CFBundleDocumentTypes</key>
<array>
        <key>CFBundleTypeName</key>
                <string>A6026753217901</string>
        <key>LSItemContentTypes</key>
                <array>
                        <string>com.microsoft.word.doc</string>
                </array>
</array>

  

2.监听应用被其他应用调起

api.addEventListener({
                name : 'appintent'
        }, function(ret, err) {
                if(api.systemType === 'android'){

  

 

                       //点击系统分享菜单分享到自己app时,这里监听返回的参数是content://格式的,这个就是系统传过来的路径,不能直接使用,需要原生Uri对象转换
                       //不会原生自己封装模块的,我这边已经封装好了fileScan模块的streamToAbsolutePath

  if(appParam['android.intent.extra.STREAM']){

  

                            //大家仔细观察下这个返回的参数,他数组不像数组,中间还有个隐藏的空格字符,所以这里需要手动转换下,去掉中括号,去空格,转成以逗号分隔的形式

  var endPath =appParam['android.intent.extra.STREAM'].replace(/[|]/g,'')
                            var filePath =filePath.replace(/s+/g,'')
                            var fileScan =api.require('fileScan')
                            var param ={
                                streamPath:filePath
                            }
                            fileScan.streamToAbsolutePath(param,function(ret,err){

 

  //这里就已经成功拿到绝对路径了        

 alert(JSON.stringify(ret.data))
                             })
                     }
                }
               if(api.systemType === 'ios'){

  

 

 //ios可以直接返回绝对路径,这里不做多说

  
if(ret.iosUrl&&ret.iosUrl.indexOf('/') === 0){

  

   //拿到文件绝对路径

 
   var filePath =ret.iosUrl
                      }
              }
});

  

原文地址:https://www.cnblogs.com/APICloud/p/12858843.html