- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的 View Controller 中嵌入了一个 UIWebView,如下所示:
我的 WebView ( _graphTotal
) 有一个导出,我可以成功加载 test.html
的内容在其中使用:
[_graphTotal loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];
我现在正尝试将数据传递到 WebView ,但没有成功。我添加了 <UIWebViewDelegate>
这就是我正在尝试的:
NSString *userName = [_graphTotal stringByEvaluatingJavaScriptFromString:@"testFunction()"];
NSLog(@"Web response: %@",userName);
这里是test.html
的内容在我的项目中:
<html>
<head></head>
<body>
Testing...
<script type="text/javascript">
function testFunction() {
alert("made it!");
return "hi!";
}
</script>
</body>
</html>
我可以在我的 webView 中看到“Testing...”,但我没有看到警报,也没有看到返回的“hi!”字符串。
知道我做错了什么吗?
最佳答案
好吧,问题是您试图在 webview 有机会加载页面之前评估您的 javascript。先采纳<UIWebViewDelegate>
协议(protocol)进入你的 View Controller :
@interface ViewController : UIViewController <UIWebViewDelegate>
然后将您的 webview 的委托(delegate)连接到您的 View Controller ,发出请求并最终实现 delegate methods .这是在 webview 完成加载时通知您的:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.graphTotal loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]isDirectory:NO]]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *userName = [self.graphTotal stringByEvaluatingJavaScriptFromString:@"testFunction()"];
NSLog(@"Web response: %@",userName);
}
PS:当您以这种方式评估 javascript 时,您必须注意的限制之一是您的脚本必须在 10 秒内执行。因此,例如,如果您等待超过 10 秒才关闭警报,您将收到错误消息(等待 10 秒后无法返回)。查找有关限制的更多信息 in the docs .
关于ios - 将数据传入和传出嵌入式 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17117713/
我正在寻找一种方法来观察 UIWebView 中文本选择已开始或结束的通知。一旦出现大头针和蓝色选择覆盖,它会自动更新 Javascript 选择和范围对象,但是当您点击选择并结束时,似乎没有一种干净
我需要获取 UIWebView 的 url在用户单击一次后显示。 我尝试在 UIWebView 下放置一个按钮,该按钮调用确定 url 的方法。 ,但这样按钮不起作用。我试过在 UIWebView 上
我正在使用 AFNetworking UIWebView 类别,我想知道是否有任何方法可以通过证书固定检查加载 url。 最好的问候, 最佳答案 UIWebView 没有向它公开任何直接 API,但它
我想从应用程序中禁用长触摸。我无法控制在 WebView 上加载的 HTML。 最佳答案 在 webViewDidFinishLoad委托(delegate)我在加载的 html 页面上运行一个禁用长
每个人: 我可以通过将scalesPageToFit 设置为YES 来启用缩放。但是,我如何知道 UIWebView 是否正在缩放? 有人知道吗? 提前致谢。 最佳答案 已解决。 向 XXXViewC
我在每个单元格中都有几个自定义单元格和 WebView 。现在,我的要求是找到要在 webview 上加载的 HTML 字符串的高度,然后根据此更改自定义单元格高度和 webview 高度。 我知道可
我想嵌入一个 UIWebView进入我的 MonoTouch 应用程序,用于尚未本地实现的区域。 为了对网站进行身份验证,我想设置一个包含当前 session key 的 cookie。 我尝试创建一
我的应用程序使用 UIWebview,它在 iOS 5 和 iOS 6 中运行良好。但是,当我在 Xcode 5 中构建并运行相同的代码时,它不会在 iOS 7 中加载网页。 - (void)web
我有一个嵌入式网站,有很多链接,但 WebView 窗口相当小,允许放大和缩小列表上方的较大图像。如果可能的话,我需要 webview 响应到带有第二个嵌入式 UIWebView 的新 Control
我正在 Monotouch 上构建 iPhone 应用程序。我的项目的一部分使用本地网站内容。我将网站使用的所有 html、js、css 和图像复制到一个文件夹中,并将其导入到我的 monotouch
在下面的示例中,infoScroller 是一个UIWebView,println(HTMLDescription) 打印一个可爱的 HTML 字符串。但是,尝试 loadHTMLString 会出现
我有一个包含多个 UIWebView 的应用程序。我有一个 UIWebView 保存所有业务逻辑和其他 UIWebViews 用于显示目的。但是,我发现如果 UIWebView 不是最后一个加载的,断
我的一个包含链接的 View Controller 底部有一个 Web View 。我想要做的是,当用户单击 Web View 中的一个链接时,它会打开一个 Web View ,该 View 占据屏幕
我已经在 webview 中实现了 ScrollView 的委托(delegate)。由于,iOS 5 默认 ScrollView 不再响应 didZoom 事件。为什么会出现这种行为? 最佳答案 i
我正在使用新的 Xcode UI Testing来自 XCTest Framework与 Xcode 7 GM .我有一个带有简单 UIWebView 的应用程序(它只是一个带有 web View 和
我无法从 UIWebView 响应中获取 header ,因为响应显然没有被缓存。有解决方法吗?我试过来自 here 的代码. 我的应用程序使用混合的原生 iOS View Controller 和
我想制作一个简单的应用程序,其中具有自定义内容的 UIWebView 将具有多个指向具有类似内容的其他页面的链接(顶部有一个导航栏,只有一个后退按钮)。我阅读了this的答案问题,但是我不确定是否应该
我制作了一个简单的测试项目,其中仅包含一个 UIWebView,其中 UIView 填充了窗口。当 UIWebView 的宽度与 UIView 相同时,一切正常。当UIWebView的宽度小于容器的宽
在我的 iPad 应用程序中,有一个显示文本内容的 UIWebview。当我按住并选择一个文本时,应该会弹出一个带有 2 个自定义菜单的菜单。 说,|菜单 1 |菜单2 | 但似乎 COPY 菜单也会
是否可以在 uiwebview.js 中加载应用商店链接。我尝试在 uiwebview 中打开它,但它没有加载到那里。我不希望用户在单击我的应用程序中的网址时离开我的应用程序。可能吗? 问候 潘卡杰
我是一名优秀的程序员,十分优秀!