2012-05-29 55 views
0

我期待如何让“一”和“公园”的字符串值(这是image_map坐标上的UIWebView的点击点)的onclick从HTML到iPhone原生OBJ-C。 我已经把代码得到字符串值,但它没有响应... PLZ检查和帮助如何在iPhone中使用UIWebview使图像地图可点击?

我已更新代码与可能的答案。如果图像地图和地图可点击表面更多,这将变得复杂。编码和解释任何帮助和建议,这是值得赞赏的。在此先感谢

更新时间: * 对于HTML代码 *

<html> 
<body onload="anyFunction();"> 
<script language="javascript"> 

function anyFunction(){ 
window.location='value'; 
} 

function callFromActivity(msg){ 
document.getElementById("circle").innerHTML = 'onclick'; 
    } 
</script> 

<img src="map_new123.png" width="1500" height="731" border="0" usemap="#map" /> 
<a href="didTap://button1"> 
<map name="map"> 
    //want ot get below onclick values one and park in to objective c ..... 
<area shape="circle" coords="1047,262,26" onclick="one" /> 
<area shape="circle" coords="1118,590,24" onclick="park" /> 
</a></map> 

对于对象 -

- (BOOL)webView:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
NSString *absoluteUrl = [[request URL] absoluteString]; 
if ([absoluteUrl isEqualToString:@"didTap://button1"]) { 
    //   
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"Connectivity!" message:@" Hello" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease]; 
    [myAlert show]; 

//the below line of code should get the string but its not working 

NSString* html = [myWeb stringByEvaluatingJavaScriptFromString:@"document.getElementById('circle').onclick"]; 

    return NO; 
} 
return YES; } 

可能的答案

//Html coding 
<area shape="circle" coords="1047,262,26" onclick="one" href="didTap://button1" value="one" /> 
<area shape="circle" coords="1118,590,24" onclick="park" href="didTap://button2" value="park"/> 


    //Obj C 

- (BOOL)webView:(UIWebView*)aWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
NSString *absoluteUrl = [[request URL] absoluteString]; 
if ([absoluteUrl isEqualToString:@"didTap://button1"]) { 
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"Selected Location!" message:@"One" delegate:self cancelButtonTitle:@"GetDirection" otherButtonTitles:@"Quit",nil] autorelease]; 
    [myAlert show]; 

    return NO; 
} 
if ([absoluteUrl isEqualToString:@"didTap://button2"]) 
{ 
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"Selected Location!" message:@"Park" delegate:self cancelButtonTitle:@"GetDirection" otherButtonTitles:@"Quit",nil] autorelease]; 
    [myAlert show]; 
    return NO; 
} 
return YES; 
} 

回答

0

我不知道究竟你的“从HTML获取”的意思。最有可能的解决方案将是使用UIWebView JavaScript方法:-(NSString*)stringByEvaluatingJavaScriptFromString:

如果你想从ID为“测试”标签的innerHTML

NSString *innerHTML = [aWebview stringByEvaluatingJavaScriptFromString:@"document.getElementById('test').innerHTML"]; 
+0

如果你看看我的HTML代码....我想要在我的本地iPhone obj-c方法(textfield)中获取'one'和'park'字符串。 –

+0

你的代码对于obj-c是正确的。但是,我应该把什么东西放入HTML中呢? –

+1

为什么不简单地删除一个href,然后将其添加到特定区域并调用不同的URL? 'didTap:// button1/one'然后你可以检查该方案来处理webview委托中的url。如果需要,检查主机'button1',然后检查路径。 – FelixLam

相关问题