我是iOS开发中的新手。我只想加载"描述" UIWebView中的关键数据来自我的网址这里我的网址就像
http://www.janvajevu.com/webservice/specific_post.php?post_id=2885
我解析数据就像
一样-(void)viewWillAppear:(BOOL)animated
{
[self.webSpinner startAnimating];
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/specific_post.php?post_id=2885"]];
dispatch_async(kBgQueue, ^{
data = [NSData dataWithContentsOfURL: url];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
-(void)fetchedData:(NSData *)responsedata
{
if (responsedata.length > 0)
{
NSError* error;
self.webDictionary= [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.webArray=[_webDictionary objectForKey:@"data"];
}
self.webViewString=[self.webArray valueForKey:@"description"];
NSString *descriptionString=[NSString stringWithFormat:@"%@",self.webViewString];
[self.webView loadHTMLString:descriptionString baseURL:nil];
然后它在WebView中打印数据,如
"<img style='width: 100% !important; height: auto; margin: 30px 20px 30px 20px;' class=\"alignnone size-full wp-image-2967\" src=\"http://www.janvajevu.com/wp-content/uploads/2014/12/Amazing-talented-people-in-the-world.jpg\" alt=\"Amazing talented people in the world\" width=\"0\" height=\"0\" />\U0a9c\U0ac1\U0a93 \U0aa6\U0ac1\U0aa8\U0abf\U0aaf\U0abe \U0aa8\U0abe\U0a85\U0a9c\U0aac \U0a97\U0a9c\U0aac \U0a9f\U0ac7\U0ab2\U0ac7\U0a82\U0a9f\U0ac7\U0aa1 \U0ab2\U0acb\U0a95\U0acb<br />
\n<br />
\nhttps://www.youtube.com/watch?v=Vo0Cazxj_yc"
当我像他们一样编码时
NSData *webViewDatastring = [descriptionString dataUsingEncoding:[NSString defaultCStringEncoding]];
NSString *webUnicodeString = [[NSString alloc] initWithData:webViewDatastring encoding:NSNonLossyASCIIStringEncoding];
NSString *webViewString=[webUnicodeString stringByTrimmingCharactersInSet:charsToTrim];
并将其加载到WebView中,如
[self.webView loadHTMLString:webViewString baseURL:nil];
然后它返回Null。所以我混淆了如何编码这个HTML数据,然后它显示在UIWebView请给我解决方案为此,给我这种类型的教程的资源。
答案 0 :(得分:2)
我只是按照以下流程直接为我工作。
NSData *jsonData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.janvajevu.com/webservice/specific_post.php?post_id=2885"]];
NSJSONSerialization *jsonParser=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *data=[[(NSDictionary *)jsonParser objectForKey:@"data"] lastObject];
[self.webView loadHTMLString:[data objectForKey:@"description"] baseURL:nil];
参见输出
希望它有所帮助。