element-getnativeview.uvue 3.5 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3 4
<template>
  <view id="view" style="flex: 1;">
    <input id="input" value="input" class="input" />
    <textarea id="textarea" value="textarea" class="textarea" />
5
    <web-view id="webview" src="/hybrid/html/local.html" class="web-view"></web-view>
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
6
    // 注意:iOS平台真机运行时需要安装 Xcode 开发工具具备 UTS 开发环境,或提交自定基座打包后 checkNativeView 相关方法才会生效
7 8 9 10
    <button class="button" type="primary" @click="checkViewNativeView">检测view标签原生View</button>
    <button class="button" type="primary" @click="checkInputNativeView">检测input标签原生View</button>
    <button class="button" type="primary" @click="checkTextareaNativeView">检测textarea标签原生View</button>
    <button class="button" type="primary" @click="checkWebViewNativeView">检测webview标签原生View</button>
shutao-dc's avatar
shutao-dc 已提交
11 12 13 14 15 16 17 18 19 20 21 22
  </view>
</template>

<script>
  import { checkWebViewNativeView, checkInputNativeView, checkTextareaNativeView, checkViewNativeView } from '@/uni_modules/uts-get-native-view';
  export default {
    data() {
      return {

      }
    },
    methods: {
23 24 25 26 27 28
      checkViewNativeView() : boolean {
        var viewName = "ViewGroup"
        // #ifdef APP-IOS
        viewName = "UIView"
        // #endif
        const msg = "检测view组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
29
        if (checkViewNativeView("view")) {
30
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
31 32
          return true
        }
33
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
34 35
        return false
      },
36 37 38 39 40 41
      checkInputNativeView() : boolean {
        var viewName = "AppCompatEditText"
        // #ifdef APP-IOS
        viewName = "UITextField"
        // #endif
        const msg = "检测input组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
42
        if (checkInputNativeView("input")) {
43
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
44 45
          return true
        }
46
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
47 48
        return false
      },
49
      checkTextareaNativeView() : boolean {
50
        var viewName = "AppCompatEditText"
51
        // #ifdef APP-IOS
52
        viewName = "UITextView"
53 54
        // #endif
        const msg = "检测textarea组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
55
        if (checkTextareaNativeView("textarea")) {
56
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
57 58
          return true
        }
59
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
60 61
        return false
      },
62 63 64 65 66 67
      checkWebViewNativeView() : boolean {
        var viewName = "WebView"
        // #ifdef APP-IOS
        viewName = "WKWebView"
        // #endif
        const msg = "检测webview组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
68
        if (checkWebViewNativeView("webview")) {
69
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
70 71
          return true
        }
72
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
73
        return false
74 75 76 77 78 79 80
      },
      showTip(title: string) {
        console.log("title==="+title)
        uni.showToast({
          title: title,
          icon: "none"
        })
shutao-dc's avatar
shutao-dc 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
      }
    }
  }
</script>

<style>
  .input {
    width: 300px;
    height: 40px;
    border-radius: 4px;
    border-width: 1px;
    border-color: black;
    border-style: solid;
    margin: 20px auto;
  }

  .textarea {
    width: 300px;
    height: 80px;
    border-radius: 4px;
    border-width: 1px;
    border-color: black;
    border-style: solid;
    margin: 20px auto;
  }

  .web-view {
    width: 300px;
    height: 120px;
    margin: 20px auto;
    border-radius: 4px;
    border-width: 1px;
    border-color: black;
    border-style: solid;
115 116 117 118
  }

  .button {
    margin: 10px 20px;
shutao-dc's avatar
shutao-dc 已提交
119 120
  }
</style>