ios Safari浏览器不支持 js 复制字符串

ios Safari浏览器不支持 js 复制字符串

本来设计成:点击"复制"按钮,可以复制订单号到剪切板的.


ios Safari浏览器不支持 js 复制字符串
 

可以发现在 ios Safari 浏览器中不起作用.

后来在http://stackoverflow.com/ 查到


ios Safari浏览器不支持 js 复制字符串
 参考:http://stackoverflow.com/questions/34045777/copy-to-clipboard-using-javascript-in-ios

解决方法:

判断操作系统类型,如果是 ios 则不显示"复制"按钮(因为显示了,也无用)

(1)判断操作系统类型

ClientOsInfo info =SpringMVCUtil.getMobileOsInfo(request);
        model.addAttribute("ostype",info.getOsType());

 osType的取值为"Ios","Android","WINDOWS PHONE"

public static final String OSTYPE_ANDROID="Android";
    public static final String OSTYPE_IOS="Ios";
    public static final String OSTYPE_WP="WINDOWS PHONE";
    /***
     * 黑莓
     */
    public static final String OSTYPE_BLACKBERRY="BLACKBERRY";

 (2)前端通过 freeMark 判断:

<#if !(ostype??)|| ostype!='Ios'>
        <div id="copy_order"  >复制</div>
        <div id="copy_money"  >复制</div>
    </#if>

 SpringMVCUtil.getMobileOsInfo

方法实现如下

/**
     * 判断手机的操作系统 IOS/android/windows phone/BlackBerry
     *
     * @param UA
     * @return
     */
    public static ClientOsInfo getMobilOS(String UA) {
        if (UA == null) {
            return null;
        }
        UA=UA.toUpperCase();
        ClientOsInfo osInfo=new  ClientOsInfo();
        // 存放正则表达式
        String rex = "";
        // IOS 判断字符串
        String iosString = " LIKE MAC OS X";
        if (UA.indexOf(iosString) != -1) {
            if (StringUtil.isFind(UA, "\\([\\s]*iPhone[\\s]*;", Pattern.CASE_INSENSITIVE)) {
                osInfo.setDeviceType(DEVICE_TYPE_PHONE);
            } else if (StringUtil.isFind(UA, "\\([\\s]*iPad[\\s]*;", Pattern.CASE_INSENSITIVE)) {
                osInfo.setDeviceType(DEVICE_TYPE_PAD);
            }
            rex = ".*" + "[\\s]+(\\d[_\\d]*)" + iosString;
            Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(UA);
            boolean rs = m.find();
            if (rs) {
                String osVersion= m.group(1).replace("_", ".");
                osInfo.setVersion(osVersion);
//				System.out.println("Mobil OS is" + " IOS" +osVersion);
                osInfo.setOsTypeVersion(OSTYPE_IOS+"_" + osVersion);
            }else{
                System.out.println("IOS");
                osInfo.setOsTypeVersion(OSTYPE_IOS);
            }
            osInfo.setOsType(OSTYPE_IOS);
            return osInfo;
        }
        // Android 判断
        String androidString = "ANDROID";
        if (UA.indexOf(androidString) != -1) {
            if (StringUtil.isFind(UA, "\\bMobi", Pattern.CASE_INSENSITIVE)) {
                osInfo.setDeviceType(DEVICE_TYPE_PHONE);
            }else {
                osInfo.setDeviceType(DEVICE_TYPE_PAD);
            }
            rex = ".*" + androidString + "[\\s]*(\\d*[\\._\\d]*)";
            Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(UA);
            boolean rs = m.find();
            if (rs) {
                String version=m.group(1).replace("_", ".");
                osInfo.setVersion(version);
                System.out.println("Mobil OS is " + OSTYPE_ANDROID + version);
                osInfo.setOsTypeVersion(OSTYPE_ANDROID+"_" + version);
            }else{
                System.out.println("Android");
                osInfo.setOsTypeVersion(OSTYPE_ANDROID);
            }
            osInfo.setOsType(OSTYPE_ANDROID);
            return osInfo;
        }
        // windows phone 判断
        String wpString = "WINDOWS PHONE";
        if (osTypeMatch(UA, osInfo, wpString)) return osInfo;
        // BlackBerry 黑莓系统判断
        String bbString = "BLACKBERRY";
        if (UA.indexOf(bbString) != -1) {
            rex = ".*" + bbString + "[\\s]*([\\d]*)";
            Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(UA);
            boolean rs = m.find();
            if (rs) {
                System.out.println("Mobil OS is" + " BLACKBERRY " + m.group(1));
                String version=m.group(1);
                osInfo.setVersion(version);
                osInfo.setOsTypeVersion(OSTYPE_BLACKBERRY+"_" + version);
            }else{
                System.out.println("BLACKBERRY");
                osInfo.setOsTypeVersion(OSTYPE_BLACKBERRY);
            }
            osInfo.setOsType(OSTYPE_BLACKBERRY);
            return osInfo;
        }
        if(UA.contains("LINUX")){//android
            if (StringUtil.isFind(UA, "\\bMobi", Pattern.CASE_INSENSITIVE)) {
                osInfo.setDeviceType(DEVICE_TYPE_PHONE);
            }else {
                osInfo.setDeviceType(DEVICE_TYPE_PAD);
            }

            Pattern p = Pattern.compile("U;\\s*(Adr[\\s]*)?(\\d[\\.\\d]*\\d)[\\s]*;",Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(UA);
            boolean result = m.find();
            String find_result = null;
            if (result)
            {
                find_result = m.group(2);
            }
            if(StringUtil.isNullOrEmpty(find_result)){
                osInfo.setOsTypeVersion(OSTYPE_ANDROID);
                return osInfo;
            }else{
                osInfo.setVersion(find_result);
                osInfo.setOsTypeVersion(OSTYPE_ANDROID+"_"+find_result);
                return osInfo;
            }
        }

        //UCWEB/2.0 (iOS; U; iPh OS 4_3_2; zh-CN; iPh4)
        if(UA.matches(".*((IOS)|(iPAD)).*(IPH).*")){
            if (StringUtil.isFind(UA, "[\\s]*iPh[\\s]*", Pattern.CASE_INSENSITIVE)) {
                osInfo.setDeviceType(DEVICE_TYPE_PHONE);
            }else {
                osInfo.setDeviceType(DEVICE_TYPE_PAD);
            }
            Pattern p = Pattern.compile("U;\\s*(IPH[\\s]*)?(OS[\\s]*)?(\\d[\\._\\d]*\\d)[\\s]*;",Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(UA);
            boolean result = m.find();
            String find_result = null;
            if (result)
            {
                find_result = m.group(3);
            }
            if(StringUtil.isNullOrEmpty(find_result)){
                osInfo.setOsTypeVersion(OSTYPE_IOS);
                osInfo.setOsType(OSTYPE_IOS);
                return osInfo;
            }else{
                String version=find_result.replace("_", ".");
                osInfo.setVersion(version);
                osInfo.setOsTypeVersion(OSTYPE_IOS+"_"+version);
                osInfo.setOsType(OSTYPE_IOS);
                return osInfo;
            }
        }
        return osInfo;
    }

 /***
     * 当移动端(手机或Pad)访问网页时获取移动端操作系统信息
     * @param request
     * @return
     */
    public static ClientOsInfo getMobileOsInfo(HttpServletRequest request){
        String userAgent=request.getHeader("user-agent");
        if(StringUtil.isNullOrEmpty(userAgent)){
            userAgent=request.getHeader("User-Agent");
        }
        ClientOsInfo info= getMobilOS(userAgent);
        if (null == info) {//为了通过单元测试
            info = new ClientOsInfo();
        }
        info.setUserAgent(userAgent);
        return info;
    }

相关推荐