【问题标题】:Can I create a link from within my webview browser that will call a specific function我可以从我的 webview 浏览器中创建一个链接来调用特定函数吗
【发布时间】:2014-08-07 06:12:23
【问题描述】:

我们在创建 webview 组件时使用的地址字符串中使用本地地理定位数据(例如http://example.com?long=36.333&latt=-143.222)。我们这样做的原因是我们想检查位置,但不想使用浏览器定位工具,它每天都会弹出警报询问我们是否可以使用您的位置。

理想情况下,我希望能够创建一个新的 webview 组件(带有新的位置数据)以响应对 webview 组件中的链接的点击。我知道我们可以通过链接(例如 myappname://)调用应用程序,但我可以在此调用中添加参数以触发某些事件,例如我们重新加载 webview 组件的函数吗?

如果不能通过 html 中的链接完成,我想我必须在页面上放置一个本机按钮并从那里调用它。

【问题讨论】:

    标签: ios webview


    【解决方案1】:

    你可以的。首先,在您的 HTML 内容中设置像 http://example.com?#SetCustomLocation 这样的链接。

    设置 WebView Delegate 方法如下:

    -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
    {
        if ( inType == UIWebViewNavigationTypeLinkClicked )
        {
            NSString *url = [NSString stringWithFormat:@"%@", [inRequest URL]];
            if([url hasSuffix:@"#SetCustomLocation"])
            {
                NSString *strURL = @"http://example.com?long=36.333&latt=-143.222"; //Set Custom URL with Lat/long Whatever you need
                NSURL *cURL = [NSURL URLWithString:[strURL tringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
                [[UIApplication sharedApplication] openURL:cURL];
            }
        }
        return YES;
    }
    

    问候。

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      相关资源
      最近更新 更多