Skip to content

Commit

Permalink
fix(taro-h5): 修复 H5 下 iOS 设备 Taro.setClipboardData 无效 #4611
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 committed Oct 23, 2019
1 parent 87af6ee commit a94897d
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/taro-h5/src/api/clipboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,25 @@ export const setClipboardData = ({ data, success, fail, complete }) => {
key: CLIPBOARD_STORAGE_NAME,
data
}).then(() => {
if (document.execCommand('copy')) {
/**
* 已于 iPhone 6s Plus iOS 13.1.3 上的 Safari 测试通过
* iOS < 10 的系统可能无法使用编程方式访问剪贴板,参考:
* https://stackoverflow.com/questions/34045777/copy-to-clipboard-using-javascript-in-ios/34046084
*/
if (typeof document.execCommand === 'function') {
const input = document.createElement('input')
input.setAttribute('readonly', 'readonly')
input.setAttribute('value', data)
input.readOnly = true
input.value = data
input.style.position = 'absolute'
input.style.width = '100px'
input.style.left = '-10000px'
document.body.appendChild(input)
input.select()
input.setSelectionRange(0, 999999)
const results = document.execCommand('copy')
input.setSelectionRange(0, input.value.length)
document.execCommand('copy')
document.body.removeChild(input)
if (!results) {
throw new Error('复制失败')
}
} else {
throw new Error(`Unsupported Function: 'document.execCommand'.`)
}
const res = {
errMsg: 'setClipboardData:ok',
Expand Down

0 comments on commit a94897d

Please sign in to comment.