ios - Phonegap 确认提醒

标签 ios cordova alert confirmation

我尝试将以下代码放入 html 中并运行它,然后将其上传到我的服务器,然后在我的 iPhone 中的 Safari 浏览器中打开链接,然后单击“显示确认”,但没有弹出窗口!有人可以帮忙吗。

<!DOCTYPE html>
<html>
  <head>
    <title>Notification Example</title>
    <script type="text/javascript" charset="utf-8" src="http://mobile-web-development-with-phonegap.eclipselabs.org.codespot.com/svn-history/r99/trunk/com.mds.apg/resources/phonegap/js/phonegap-1.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">
        // Wait for PhoneGap to load
        document.addEventListener("deviceready", onDeviceReady, false);
        // PhoneGap is ready        
        function onDeviceReady() {
            // Empty
        }
        // process the confirmation dialog result
        function onConfirm(button) {
            alert('You selected button ' + button);
        }
        // Show a custom confirmation dialog
                function showConfirm() {
            navigator.notification.confirm(
            'You are the winner!',  // message
            onConfirm,              // callback to invoke with index of button pressed
            'Game Over',            // title
            'Restart,Exit'          // buttonLabels
        );
        }
    </script>
  </head>
  <body>
    <p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p>
  </body>
</html>

最佳答案

您正在使用的代码 (navigator.notification.confirm) 专门用于移动平台,它旨在在 PhoneGap 移动应用程序中运行。如果您想在将其编译到应用程序之前在浏览器上测试对话框/确认消息,我建议使用一种混合方法来检测应用程序的环境并使用 native confirm(message) 或 PhoneGap 特定的通知 API。下面是一个为我工作的示例对象:

/**
 * The object encapsulates messaging functionality to work both in PhoneGap and 
 * browser environment. 
 * @author Zorayr Khalapyan
 * 
 */
var MessageDialogController = (function () {

    var that = {};

    /**
     * Invokes the method 'fun' if it is a valid function. In case the function
     * method is null, or undefined then the error will be silently ignored.
     *
     * @param fun  the name of the function to be invoked.
     * @param args the arguments to pass to the callback function.
     */
    var invoke = function( fun, args ) {
        if( fun && typeof fun === 'function' ) {
            fun( args );
        }
    };

    that.showMessage = function( message, callback, title, buttonName ) {

        title = title || "DEFAULT_TITLE";
        buttonName = buttonName || 'OK';

        if( navigator.notification && navigator.notification.alert ) {

            navigator.notification.alert(
                message,    // message
                callback,   // callback
                title,      // title
                buttonName  // buttonName
            );

        } else {

            alert( message );
            invoke( callback );
        }

    };

    that.showConfirm = function( message, callback, buttonLabels, title ) {

        //Set default values if not specified by the user.
        buttonLabels = buttonLabels || 'OK,Cancel';
        var buttonList = buttonLabels.split(',');

        title = title || "DEFAULT TITLE";

        //Use Cordova version of the confirm box if possible.
        if (navigator.notification && navigator.notification.confirm) {

                var _callback = function (index) {
                    if ( callback ) {
                        //The ordering of the buttons are different on iOS vs. Android.
                        if(navigator.userAgent.match(/(iPhone|iPod|iPad)/)) {
                            index = buttonList.length - index;
                        }
                        callback( index == 1 );
                    }
                };

                navigator.notification.confirm(
                    message,      // message
                    _callback,    // callback
                    title,        // title
                    buttonLabels  // buttonName
                );

        //Default to the usual JS confirm method.
        } else {
            invoke( callback, confirm( message ) );
        }

    };

    return that;

})();

希望对您有所帮助!如果您有任何问题,请告诉我。

关于ios - Phonegap 确认提醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14296131/

相关文章:

android - 无法添加 ionic 添加平台android

iphone - 我们可以将 'phonegap api ' 应用程序与纯 native 应用程序(obj-C)代码连接起来吗

iphone - 如何以编程方式获取iPhone的铃声列表

javascript - 如何在 javascript 中只显示一次警报?

iphone - Obj-C,带有 ARC 代码和警告的库 - 方法可能缺少 [super dealloc] 调用?

ios - 通过 iCloud 将现有核心数据数据库轻量级迁移到 iOS 7

ios - indexpath.row 从 1 而不是 0 开始

ios - 使用 downloadTaskWithRequest 时 ResumeData 为零

javascript - 如何使用 viewbag 在 View 页面中显示警报消息

iphone - 如何将此脚本安装到 PhoneGap for iOS