Cordova / jQuery - 确定您的应用是在Web / Mobile浏览器或移动应用(Android / iOS)上运行

时间:2016-06-20 10:02:03

标签: android jquery ios cordova jquery-mobile

我使用Cordova / Phonegap为Android / iOS开发应用程序。我通常会为我的网络和移动内容使用单一代码库。我在移动应用程序时使用SQLite数据库和/或其他本机插件,并且当我在网络上时必须避免使用这些LOC。

但是我在确定我的应用是在桌面/ Mac / Android / iOS上的网络浏览器上运行还是作为移动应用(Android / iOS)时遇到问题。

我尝试过userAgent嗅探,但这种正则表达式技术失败,特别是在移动浏览器上运行代码时。以下是我用来识别操作系统和设备版本的代码:

getOSAndVersion: function() {
        var that = this;
        var userOS;    // will either be iOS, Android or unknown
        var userOSver; // this is a string, used to denote OS version
        var ua = navigator.userAgent;
        var uaindex;

        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua)) {
            window.deviceType = "Mobile";                                  
        } else {
            window.deviceType = "Web";                    
        }

        // determine OS
        if (ua.match(/iPad/i) || ua.match(/iPhone/i)) {
            userOS = 'iOS';
            uaindex = ua.indexOf('OS ');
        }
        else if (ua.match(/Android/i)) {
            userOS = 'Android';
            uaindex = ua.indexOf('Android ');
        }
        else {
            userOS = 'unknown';
        }

        // determine version
        if (userOS === 'iOS' && uaindex > -1) {
            userOSver = ua.substr(uaindex + 3, 3).replace('_', '.');
        }
        else if (userOS === 'Android' && uaindex > -1) {
            userOSver = ua.substr(uaindex + 8, 3);
        }
        else {
            userOSver = 'unknown';
        }
        return { osVersion: userOSver, os: userOS, deviceType: window.deviceType };
    }

我是否可以使用其他任何技术来可靠地识别我的代码运行位置?

P.S。 :我反对使用任何其他Cordova / JS插件来识别它,但仍然可以讨论。

2 个答案:

答案 0 :(得分:1)

在Cordova中,当app运行到app时,网址以file://为前缀,在移动浏览器中运行时,网址会以httphttps协议为前缀。

解决方案:

  1. 获取当前页面的网址(check this
  2. 如果file://其应用
  3. ,请确定其前缀
  4. 如果是http或https,那么移动浏览器

答案 1 :(得分:0)

您可以检查是否定义了<?php if(isset($_POST['save'])!="") { $workname=mysql_real_escape_string($_POST['workname']); $assignto=mysql_real_escape_string($_POST['name']); $phoneno=mysql_real_escape_string($_POST['phone_no']); $emailid=mysql_real_escape_string($_POST['emails']); $issuedate=mysql_real_escape_string($_POST['issue_date']); $deldate=mysql_real_escape_string($_POST['delivery_date']); $actualdeldate=mysql_real_escape_string($_POST['actual_delivery_date']); $remstrtfrm=mysql_real_escape_string($_POST['reminder_start']); $remtmeint=mysql_real_escape_string($_POST['reminder_time']); $remmsg=mysql_real_escape_string($_POST['reminder_message']); $wrkstat=mysql_real_escape_string($_POST['work_status']); $maker_id=mysql_real_escape_string($_SESSION['ADMIN_GAME_ID']); $create=mysql_query("INSERT INTO dizypro_work_status_details(jobname,assign_to,phone_no,email_id,issue_date,delev ry_date,actual_del_date, rem_strt_frm,reminder_time,reminder_message,work_status,maker_id,maker_date,row_ no) VALUES('$workname','$assignto','$phoneno','$emailid','$issuedate','$deldate','$a ctualdeldate','$remstrtfrm','$remtmeint','$remmsg','$wrkstat','$maker_id',now(), '$row')"); if($create) { $msg="Successfully Updated!!"; echo "<script type='text/javascript'>alert('$msg');</script>"; header('Location:add-work-detail.php'); } else { $errormsg="Something went wrong, Try again"; echo "<script type='text/javascript'>alert('$errormsg');</script>"; } } ?> <?php if(isset($_POST['edit'])!="") { $checkbox=mysql_real_escape_string($_POST['check']); $assignto=mysql_real_escape_string($_POST['name']); $phoneno=mysql_real_escape_string($_POST['phone_no']); $emailid=mysql_real_escape_string($_POST['emails']); $issuedate=mysql_real_escape_string($_POST['issue_date']); $deldate=mysql_real_escape_string($_POST['delivery_date']); $actualdeldate=mysql_real_escape_string($_POST['actual_delivery_date']); $remstrtfrm=mysql_real_escape_string($_POST['reminder_start']); $remtmeint=mysql_real_escape_string($_POST['reminder_time']); $remmsg=mysql_real_escape_string($_POST['reminder_message']); $wrkstat=mysql_real_escape_string($_POST['work_status']); $maker_id=mysql_real_escape_string($_SESSION['ADMIN_GAME_ID']); $update=mysql_query("UPDATE dizypro_work_status_details(check_box,jobname,assign_to,phone_no,email_id,issue_ date,delevry_date,actual_del_date, rem_strt_frm,reminder_time,reminder_message,work_status,maker_id,maker_date,row_ no) VALUES('$checkbox','$work_name','$assignto','$phoneno','$emailid','$issuedate',' $deldate','$actualdeldate','$remstrtfrm','$remtmeint','$remmsg','$wrkstat','$mak er_id',now(),'$row') WHERE maker_id='".$_SESSION['ADMIN_GAME_ID']."'"); if($update) { $msg="Successfully Updated!!"; echo "<script type='text/javascript'>alert('$msg');</script>"; header('Location:add-work-detail.php'); } else { $errormsg="Something went wrong, Try again"; echo "<script type='text/javascript'>alert('$errormsg');</script>"; } } ?>

cordova