2011-03-30 29 views
0

有没有办法从网页加载UILabel文字?如果我定义了一个网页,无论如何我都可以从页面中取出文本?无论如何,从网页加载带有文本的UILabel?

如果在页面的源代码中显示“9.5000/9.5009”,我可以将“9.5000/9.5009”作为标签的文本,即使该数字发生变化,也可以通过使用宣布的“费率”?或者我错了,它被宣布为“率”。

我可以使用这样的代码:

#define kLatestKivaLoansURL @"http://www.roblox.com/Marketplace/TradeCurrency.aspx" 

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Rate"]; 
NSDictionary* loan = [latestLoans objectAtIndex:0]; 
NSString* rate = [loan objectForKey:@"Rate"]; 
label.text = [NSString stringWithFormat:@"%i",rate]; 

有没有办法做到这一点,而不输出JSON页面?

回答

0
NSURL *url = [NSURL URLWithString:@"http://www.domain.com/rateScript.php"]; 
NSString *response = [[NSString alloc] initWithContentsOfURL:url]; 
label.text=response; 
[response release]; 

使用下面的脚本:

<?php 
$ratefile=file_get_contents("http://www.roblox.com/Marketplace/TradeCurrency.aspx"); 
$position=0; 
$position=strpos($ratefile, '"Rate"',$position)+7; 
$start=strpos($ratefile, '"Rate"',$position)+7; 
$end=strpos($ratefile, '<',$start+1); 
echo substr($ratefile,$start,$end-$start); 
?> 
+0

谢谢,你知道,如果有任何的方式来做到这一点没有输出JSON页面? – cowbear16 2011-03-30 00:57:41

+0

你可以让一个PHP的刮板找到你想要的数据,然后只需要'NSURL * url = [NSURL URLWithString:@“http://www.domain.com/scraper.php”]; NSString * response = [[NSString alloc] initWithContentsOfURL:url]; label.text = response;' – 2011-03-30 01:01:43