最近做一個把自己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
}
}
});