웹view 페이지 및 케이스 통신 라이브러리 (가상 버전)

6281 단어 webView
// PG 
(function() {
    var PG = {
            iosBridge: null,
            callbackId: 0,
            callbacks: [],
            commandQueue: [],
            commandQueueFlushing: false
        },
        ua = navigator.userAgent,
        isIOS = (ua.indexOf("iPhone") > -1 || ua.indexOf("iPad") > -1 || ua.indexOf("iPod") > -1) ? true : false;
    PG.getAndClearQueuedCommands = function () {
        var commandQueue_json = JSON.stringify(PG.commandQueue);
        PG.commandQueue = [];
        return commandQueue_json;
    };

    PG.exec = function(method, callback, args) {
        var callbackId = '';
        if (typeof(callback) == "undefined") {
            callback = null;
        }
        if (typeof(args) == "undefined") {
            args = {};
        }


        if (callback && typeof(callback) == 'function') {
            callbackId = method + PG.callbackId++;
            PG.callbacks[callbackId] = callback;
        }

        var obj = {
            Method: method,
            CallbackId: callbackId,
            Args: args
        };

        if (isIOS) {
            if (PG.iosBridge == null) {
                PG.iosBridge = document.createElement("iframe");
                PG.iosBridge.setAttribute("style", "display:none;");
                PG.iosBridge.setAttribute("height", "0px");
                PG.iosBridge.setAttribute("width", "0px");
                PG.iosBridge.setAttribute("frameborder", "0");
                document.documentElement.appendChild(PG.iosBridge);
            }

            PG.commandQueue.push(JSON.stringify(obj));
            if (!PG.commandQueueFlushing) {
                PG.iosBridge.src = 'pg://ready';
            }
            
            
        } else if (window.comjs) {
            // android
            window.comjs.notify('pg://' + encodeURIComponent(JSON.stringify(obj)));
        } else {
            console.log(" ios android , ");
        }


    };

    PG.callback = function(callbackId, args) {
        if (PG.callbacks[callbackId]) {
            try {
                var temp = decodeURIComponent(args),
                    obj = JSON.parse(temp);

                PG.callbacks[callbackId](obj);

            } catch (e) {
                console.log("Error in success callback: " + callbackId + " = " + e);
            }

            delete PG.callbacks[callbackId];

        }
    };

    
        
    if (typeof(window) === "object" && typeof(window.document) === "object") {
        window.PG = PG;
    }
        

    

})();

좋은 웹페이지 즐겨찾기