我使用此函数将URL复制到剪贴板:

function CopyUrl($this){

  var querySelector = $this.next().attr("id");
  var emailLink = document.querySelector("#"+querySelector);

  var range = document.createRange();
  range.selectNode(emailLink);  
  window.getSelection().addRange(range);  

  try {  
    // Now that we've selected the anchor text, execute the copy command  
    var successful = document.execCommand('copy', false, null);
    var msg = successful ? 'successful' : 'unsuccessful'; 

    if(true){
        $this.addClass("copied").html("Copied");
    }

  } catch(err) {  
    console.log('Oops, unable to copy');  
  }  

  // Remove the selections - NOTE: Should use   
  // removeRange(range) when it is supported  
  window.getSelection().removeAllRanges();
}

在桌面浏览器上一切正常,但在iOS设备上就不行了,在iOS设备上,我的函数成功返回,但数据根本不会复制到剪贴板.是什么导致了这个问题,我怎么才能解决这个问题呢?

推荐答案

更新!IOS>=10

Looks like with the help of selection ranges and some little hack it is possible to directly copy to the clipboard on iOS (>= 10) Safari. I personally tested this on iPhone 5C iOS 10.3.3 and iPhone 8 iOS 11.1. However, there seem to be some restrictions, which are:

  1. 只能从<input><textarea>元素复制文本.
  2. 如果包含文本的元素在<form>内是not,则它必须是contenteditable.
  3. 包含文本的元素必须是notreadonly(尽管您可以try ,但这不是任何地方记录的"官方"方法).
  4. 元素内的文本必须在 Select 范围内.

要满足这四个"要求",您必须:

  1. 将要复制的文本放入<input><textarea>元素中.
  2. 保存元素的旧值contenteditablereadonly,以便能够在复制后恢复它们.
  3. contenteditable改为true,将readonly改为false.
  4. 创建一个range以 Select 所需的元素,并将其添加到窗口的 Select 中.
  5. 为整个元素设置selection range.
  6. 恢复之前的contenteditablereadonly值.
  7. execCommand('copy')英里.

这将导致用户设备的插入符号移动并 Select 所需元素中的所有文本,然后自动发出复制命令.用户将看到选中的文本,并显示带有 Select /复制/粘贴选项的工具提示.

Now, this looks a little bit complicated and too much of an hassle to just issue a copy command, so I'm not sure this was an intended design choice by Apple, but who knows... in the mean time, this currently works on iOS >= 10.

也就是说,可以使用像this one这样的多边形填充来简化此操作,并使其跨浏览器兼容(感谢@Toskan在注释中提供链接).

Working example

总而言之,您需要的代码如下所示:

function iosCopyToClipboard(el) {
    var oldContentEditable = el.contentEditable,
        oldReadOnly = el.readOnly,
        range = document.createRange();

    el.contentEditable = true;
    el.readOnly = false;
    range.selectNodeContents(el);

    var s = window.getSelection();
    s.removeAllRanges();
    s.addRange(range);

    el.setSelectionRange(0, 999999); // A big number, to cover anything that could be inside the element.

    el.contentEditable = oldContentEditable;
    el.readOnly = oldReadOnly;

    document.execCommand('copy');
}

请注意,此函数的el参数必须是<input><textarea>.

旧答案:以前的iOS版本

iOS < 10上,Safari(实际上是安全措施)对Clipboard API有一些限制:

  • 它仅在有效 Select 上激发copy个事件,仅在聚焦的可编辑字段中激发cutpaste个事件.
  • It only supports OS clipboard reading/writing via shortcut keys, not through 100.请注意,"shorcut键"是指一些可点击的键(例如复制/粘贴操作菜单或自定义iOS键盘快捷键)或物理键(例如连接的蓝牙键盘).
  • 它不支持ClipboardEvent构造函数.

所以(至少到目前为止)it's not possible to programmatically copy some text/value in the clipboard on an iOS device using Javascript个.只有用户可以决定是否复制某些内容.

It is however possible to select something programmatically,这样用户只需点击选项上显示的"复制"工具提示.这可以通过与上面完全相同的代码来实现,只需删除execCommand('copy'),这确实是行不通的.

Ios相关问答推荐

如何遮罩,动画和添加阴影到CAShapeLayer?

在带有可编码的SWIFT5中,可以轻松处理以美元符号开头的JSON密钥,例如$DATE";

当包含UITextView的inputAccessoryView显示键盘时,iOS UITableView内容会出现UINavigationBar奇怪的错误

如何从Windows PC在iPhone上安装Flutter 应用程序

磁盘上的Objective-C ISA指针与实例化对象时的比较

许多文本字段的性能问题

SwiftUI中的Tap手势无法识别视图上的偏移变化

长按 SwiftUI 更改项目的顺序

如何使用 pushViewController 从 UIHostingController 的 SwiftUI 视图中推送 ViewController?

Task.cancel() 的 TaskPriority 的区别

FileHandle 不会在 iOS 中释放内存

SwiftUI:通过Sheet将数据添加到Realm DB时视图不更新

SwiftUI 文本字段代理绑定代码忽略空格不起作用?

如何快速设置条形按钮的图像?

如何设置imageView的圆角半径?

ios 崩溃 EXC_BAD_ACCESS KERN_INVALID_ADDRESS

以编程方式创建按钮并设置背景图像

正确使用 Alamofire 的 URLRequestConvertible

Swift - 获取设备的 WIFI IP 地址

更新字段时,UITextField值已更改未触发