【问题标题】:How to call JavaScript in WebView from iOS如何从 iOS 调用 WebView 中的 JavaScript
【发布时间】:2014-08-04 15:06:06
【问题描述】:

我有一个名为index.html(5 个动画幻灯片)的边缘动画本地存储在 iOS 应用程序中。我在 UIWebView 中加载该 html,并且我想向边缘动画发送消息以在 WebView 中显示特定幻灯片。我该怎么做?

我的 JavaScript 文件名为 index_edgeActions.js。这是代码:

//Edge symbol: 'Nav_Bar'
(function(symbolName) {
    Symbol.bindElementAction(compId, symbolName, "${_go_to_slide}", "click", function(sym, e) {
        var myPos = sym.getComposition().getStage().getPosition();
        var myTimeline = sym.getComposition().getStage();
        var x = document.getElementById("txt").value;
        if (x == 1) {
            $(function() {
                if (myPos > 500) {
                    myTimeline.playReverse(999);
                }
                if (myPos < 500) {
                    myTimeline.play(1);
                }
            });
        } else if (x == 2) {
            $(function() {
                if (myPos > 1000) {
                    myTimeline.playReverse(1499);
                }
                if (myPos < 1000) {
                    myTimeline.play(501);
                }
            });
        } else if (x == 3) {
            $(function() {
                if (myPos > 1500) {
                    myTimeline.playReverse(1999);
                }
                if (myPos < 1500) {
                    myTimeline.play(1001);
                }
            });
        } else if (x == 4) {
            $(function() {
                if (myPos > 2000) {
                    myTimeline.playReverse(2499);
                }
                if (myPos < 2000) {
                    myTimeline.play(1501);
                }
            });
        } else if (x == 5) {
            $(function() {
                if (myPos > 2500) {
                    myTimeline.playReverse(2999);
                }
                if (myPos < 2500) {
                    myTimeline.play(2001);
                }
            });
        } else if (x == 6) {
            $(function() {
                if (myPos > 3000) {
                    myTimeline.playReverse(3499);
                }
                if (myPos < 3000) {
                    myTimeline.play(2501);
                }
            });
        } else if (x == 7) {
            $(function() {
                if (myPos > 3500) {
                    myTimeline.playReverse(3999);
                }
                if (myPos < 3500) {
                    myTimeline.play(3001);
                }
            });
        } else if (x == 8) {
            $(function() {
                if (myPos > 4000) {
                    myTimeline.playReverse(4499);
                }
                if (myPos < 4000) {
                    myTimeline.play(3501);
                }
            });
        } else if (x == 9) {
            $(function() {
                if (myPos > 4500) {
                    myTimeline.playReverse(4999);
                }
                if (myPos < 4500) {
                    myTimeline.play(4001);
                }
            });
        } else if (x == 10) {
            $(function() {
                if (myPos > 5000) {
                    myTimeline.playReverse(5499);
                }
                if (myPos < 5000) {
                    myTimeline.play(4501);
                }
            });
        } else if (x == 11) {
            $(function() {
                if (myPos > 5500) {
                    myTimeline.playReverse(5999);
                }
                if (myPos < 5500) {
                    myTimeline.play(5001);
                }
            });
        } else if (x == 12) {
            $(function() {
                if (myPos > 6000) {
                    myTimeline.playReverse(6499);
                }
                if (myPos < 6000) {
                    myTimeline.play(5501);
                }
            });
        } else if (x == 13) {
            $(function() {
                if (myPos > 6500) {
                    myTimeline.playReverse(6999);
                }
                if (myPos < 6500) {
                    myTimeline.play(6001);
                }
            });
        } else if (x == 14) {
            $(function() {
                if (myPos > 7000) {
                    myTimeline.playReverse(7499);
                }
                if (myPos < 7000) {
                    myTimeline.play(6501);
                }
            });
        } else if (x == 15) {
            $(function() {
                if (myPos > 7500) {
                    myTimeline.playReverse(7999);
                }
                if (myPos < 7500) {
                    myTimeline.play(7001);
                }
            });
        } else if (x == 16) {
            $(function() {
                if (myPos > 8000) {
                    myTimeline.playReverse(8499);
                }
                if (myPos < 8000) {
                    myTimeline.play(7501);
                }
            });
        } else if (x == 17) {
            $(function() {
                if (myPos > 8500) {
                    myTimeline.playReverse(8999);
                }
                if (myPos < 8500) {
                    myTimeline.play(8001);
                }
            });
        } else if (x == 18) {
            $(function() {
                if (myPos > 9000) {
                    myTimeline.playReverse(9499);
                }
                if (myPos < 9000) {
                    myTimeline.play(8501);
                }
            });
        } else if (x == 19) {
            $(function() {
                if (myPos > 9500) {
                    myTimeline.playReverse(9999);
                }
                if (myPos < 9500) {
                    myTimeline.play(9001);
                }
            });
        } else if (x == 20) {
            $(function() {
                if (myPos > 10000) {
                    myTimeline.playReverse(10499);
                }
                if (myPos < 10000) {
                    myTimeline.play(9501);
                }
            });
        } else if (x == 21) {
            $(function() {
                if (myPos > 10500) {
                    myTimeline.playReverse(10999);
                }
                if (myPos < 10500) {
                    myTimeline.play(10001);
                }
            });
        } else if (x == 22) {
            $(function() {
                if (myPos > 11000) {
                    myTimeline.playReverse(11499);
                }
                if (myPos < 11000) {
                    myTimeline.play(10501);
                }
            });
        } else if (x == 23) {
            $(function() {
                if (myPos > 11500) {
                    myTimeline.playReverse(11999);
                }
                if (myPos < 11500) {
                    myTimeline.play(11001);
                }
            });
        } else if (x == 24) {
            $(function() {
                if (myPos > 12000) {
                    myTimeline.playReverse(12499);
                }
                if (myPos < 12000) {
                    myTimeline.play(11501);
                }
            });
        } else if (x == 25) {
            $(function() {
                if (myPos > 12500) {
                    myTimeline.playReverse(12999);
                }
                if (myPos < 12500) {
                    myTimeline.play(12001);
                }
            });
        } else if (x == 26) {
            $(function() {
                if (myPos > 13000) {
                    myTimeline.playReverse(13499);
                }
                if (myPos < 13000) {
                    myTimeline.play(12501);
                }
            });
        } else if (x == 27) {
            $(function() {
                if (myPos > 13500) {
                    myTimeline.playReverse(13999);
                }
                if (myPos < 13500) {
                    myTimeline.play(13001);
                }
            });
        } else if (x == 28) {
            $(function() {
                if (myPos < 14000) {
                    myTimeline.play(13501);
                }
            });
        } else {
            alert("Only Slides From 1-28");
        }
    });

并在 Xcode Action 中使用此代码

NSString* javascriptCommand = [NSString stringWithFormat:@"index_edgeActions.js"];
[webview stringByEvaluatingJavaScriptFromString:javascriptCommand];
[hema stringByEvaluatingJavaScriptFromString:@"getElementById(12)"]

【问题讨论】:

    标签: javascript ios uiwebview


    【解决方案1】:

    在 iOS 中,UIWebview 有一个执行 javascript 函数的方法:

    例如:

    [_myWebView stringByEvaluatingJavaScriptFromString:@"myFunc();"];
    

    【讨论】:

    • 或者你甚至可以发送一个手势
    • 抱歉打扰我有边缘动画 (index.html+index_edge.js+index_edgeActions.js+index_edgepreload.js) 如何从目标 c 代码发送动作到 html 或 js 文件告诉他们执行移动到特定幻灯片
    • 请记住,一个 html 文件包含 5 张幻灯片
    • 我更新了我的问题,你现在可以帮我吗?我的代码正确吗? NSString* javascriptCommand = [NSString stringWithFormat:@"index_edgeActions.js"]; [webview stringByEvaluatingJavaScriptFromString:javascriptCommand]; [hema stringByEvaluatingJavaScriptFromString:@"getElementById(12)"]
    猜你喜欢
    • 2014-06-16
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多