안드로이드 웹뷰 클립보드 - andeuloideu webbyu keullibbodeu

That's actually more difficult than you might think. The general permissions are easier to find. Android docs sometimes obfuscate the data and makes it harder to find. Because of this, some sites have popped up to aggregate the data in one place. But, it's still a moving target as with latest Android, Clipboard service got more security. To put it bluntly, if we start putting links on the docs site, we'd be changing them every x months because they keep changing them. For instance, I can't find android.permission.READ_CLIPBOARD anywhere on the Android site, but it's documented on dozens of other sites and have used it myself in the past. I'm sure it's there somewhere, but generally, I can't find any clipboard permissions at all (just how to use the clipboard service).

Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clibpboard.readtext function. It works just fine in mobile chrome, but in android webview, which is again based on chromium engine, it just does not work.

We have given all permission in android manifest

<uses-permission android:name="android.permission.READ_CLIPBOARD" />
<uses-permission android:name="android.permission.WRITE_CLIPBOARD" />
<uses-permission android:name="android.webkit.PermissionRequest" />

Stackoverflow has couple of threads on how to copy text clipboard inside android webview, but not on pasting the text from navigator.clipboard, so that's why had to raise this issue. Any ideas on how to solve this.

webview.setOnLongClickListener(gestureListener);

View.OnLongClickListener gestureListener = new View.OnLongClickListener() {

@Override

public boolean onLongClick(View v) {

_ALERT_CODE_=700;

Log.i(TAG,v.getClass().getSimpleName()+"");

if(v instanceof WebView){

alert.showConfirm(

"",// title

getResources().getString(R.string.copy_contents),// message 

getResources().getString(R.string.alert_ok),// button1 타이틀

getResources().getString(R.string.alert_cancel)// button2 타이틀

);

}

return false;

}

    };

    private Runnable onClipBoard=new Runnable() {

        public void run() {             

//           if (!clipboardManager.getText().toString().equalsIgnoreCase(description)) {

//                 Toast.makeText(getApplicationContext(),

//                         "selected Text = " + clipboardManager.getText().toString(),

//                         Toast.LENGTH_LONG).show();

//                 clipboardManager.setText(description);

//           } else {

        try{

        webview.postDelayed(onClipBoard, 1000);

}catch(ActivityNotFoundException e){ e.printStackTrace(); }

catch(Exception e){ e.printStackTrace(); }

//           }

        }

     };

JavaScript

[공유하기] 모바일 웹 URL 복사 기능 구현하기

작은곰- 2021. 3. 3. 07:53

오늘은 공유하기 기능 중 URL 직접 복사 기능을 설명해보려고합니다.

URL 복사 기능 구현하기 

안드로이드 웹뷰 클립보드 - andeuloideu webbyu keullibbodeu

1. Android 또는 iOS 13.4 이상

// 클립보드로 링크 복사
navigator.clipboard.writeText('https://dc2348.tistory.com/')
	.then(function () {
		alert('URL 복사가 완료되었습니다.')
	})

iOS 최신버전이거나 Android 기기에서는 위 코드 처럼 Clipboard API를 통해 간단하게 구현이 가능합니다.

그러나 Clipboard APIP가 iOS 13.4 미만 버전에서는 지원이 안되기 때문에, 이 경우에는 다르게 구현이 필요합니다.

2. iOS 13.4 미만

// 클립보드로 링크 복사
const textArea = document.createElement('textarea')
document.body.appendChild(textArea)
textArea.value = value
textArea.select()
document.execCommand('copy')
document.body.removeChild(textArea)
alert('URL 복사가 완료되었습니다.')

다른 구현 방법은 위와 같습니다.

1. textarea 엘리먼트를생성합니다.

2. body에 임시로 추가해 줍니다.

3. 생성한 textarea를 선택합니다.

4. execCommand를 통하여 Copy를 실행해 줍니다.

5. 생성했던 textarea를 삭제하면 됩니다.

여기서 사용된 execCommand는 이제 사용을 권장하지 않는 함수입니다.

그러니 iOS 13.4 이상만 지원되는 사이트에서는 Clipboard API를 사용할 것을 권장합니다.

+ 개발하며 겪은 이슈

1. iOS 버전 파편화에 따른 분기 처리