Programmatically trigger copy menu in iOS safari using javascript?

家住魔仙堡 提交于 2019-12-09 16:24:28

问题


I'm trying to implement a user-friendly way to copy some text from a text input field to the clipboard on iOS/Safari. I understand there is no way to programmatically do it on this platform, but I was hoping I could guide the user experience as much as possible.

On iOS/Safari, when a user manually highlights some text, a contextual Copy menu pops up. I was hoping the same menu would pop up when the text is selected programmatically, but it doesn't. Is it even possible to do that?

If not, any advice on how to best implement a user-friendly experience to copy some text to the clipboard on iOS/Safari?

For reference, I'm selecting the text using the method described in this question: Programmatically selecting text in an input field on iOS devices (mobile Safari)


回答1:


It's not possible unfortunately. I'd include some informative text below the input, hopefully that will work out okay in terms of user-friendliness.

Another option would be to go native, e.g. by wrapping using PhoneGap, but I guess you are already well aware of that option. If so, something like this would work in native code:

[UIPasteboard generalPasteboard].string = @"your string";



回答2:


From javascript it is possible with the help of iOS (objective C).

var getVal = $("#textid").val();
localStorage.setItem("getVal",getVal);

and then you can use your native code for getting this value from local storage.

I haven't knowledge of objective C but you can use that's method after js code.



来源:https://stackoverflow.com/questions/11442682/programmatically-trigger-copy-menu-in-ios-safari-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!