I have web application, which is mostly designed to be run on mobile devices. I have one button, which will copy to device clipboard the passed text. I am using javascript for that. My code is working great on all mobile devices, except for iphone and ipad. Anybody knows what can be the problem? Here is my code:
CopyToClipboard = function(text, fallback){
var $t = $('<textarea />');
$t.val(text).appendTo('body');
$t.select();
document.execCommand('copy');
$t.remove();
return true;
};
I have also tried to go this way, but no result, still not working on iphone
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// IE 12 => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
function copytext(text) {
if (detectIE()) {
window.clipboardData.setData('Text', text);
}
var textField = document.createElement('textarea');
textField.innerText = text;
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
window.clipboardData.setData('Text', copytext);
textField.remove();
}
function copytext(text) {
var textField = document.createElement('textarea');
textField.innerText = text;
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
$(textField).remove();
}
If you're using a Mac then you'll need to enable the Handoff feature, which you'll find in System Preferences>General, with a tick box marked Allow Handoff between this Mac and your iCloud devices. The same setting will need to be turned on for iOS devices. So, go to Settings > General > Handoff, and enable it.
Note: To use Universal Clipboard, your Apple devices must meet Continuity system requirements. They must also have Wi-Fi, Bluetooth, and Handoff turned on in System Preferences (on your Mac) and in Settings (on your iOS and iPadOS devices). You must be signed in with the same Apple ID on all your devices.
Just go through the following steps and create a “Show Clipboard” shortcut on your iPhone. Search “Shortcuts” application. Tap on the “Gallery” Menu. Search “Adjust Clipboard” and open it.
You can use Universal Clipboard to cut or copy content (a block of text or an image, for example) on your iPhone, then paste it on iPad, on another iOS device, or on a Mac, and vice versa.
Try this. Works for me.
var copy = function(elementId) {
var input = document.getElementById(elementId);
var isiOSDevice = navigator.userAgent.match(/ipad|iphone/i);
if (isiOSDevice) {
var editable = input.contentEditable;
var readOnly = input.readOnly;
input.contentEditable = true;
input.readOnly = false;
var range = document.createRange();
range.selectNodeContents(input);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
input.setSelectionRange(0, 999999);
input.contentEditable = editable;
input.readOnly = readOnly;
} else {
input.select();
}
document.execCommand('copy');
}
<input type="text" id="foo" value="text to copy" />
<button onclick="copy('foo')">Copy text</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With