iOS 使用 HTML 链接获取数据
我正在制作一本用于学习语言的互动书。它有阅读、测验和一些简单的游戏。每章的内容都是一个 HTML 文件。这本书允许用户了解文本中存在的大约 300 个单词。每个单词都包含在一个链接中,如下所示:< a href="word">word< /a>当用户触摸该链接时,会出现一个模态视图,其中包含该单词的翻译和信息。
这是我用来获取链接的代码:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// currentWord is the word in the link
self.currentWord = [[request URL] lastPathComponent];
// okToCatchLinks is a BOOL that I use to avoid showing the modalview when the page
// is loaded for the first time.
if (okToCatchLinks){
NSLog(@"Ok to catch links!");
WordViewController *viewController = [[WordViewController alloc] initWithNibName:@"WordViewController" bundle:nil]
// I did my homework creating the delegate protocol to dismiss the modal view.
viewController.delegate = self;
// This is a label in the modalview showing the word in the HTML link.
viewController.labelTitle = self.currentWord;
// Create a Navigation controller to add the "DONE" dismiss button
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:viewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// show the navigation controller modally
[self presentModalViewController:navController animated:YES];
// Clean up resources
[navController release];
[viewController release];
}
okToCatchLinks = YES;
return YES;
}
使用此代码,我将在字符串变量中获取选定的单词。然后我使用 CoreData 在数据库中搜索该单词。 HTML 采用 UTF-8 编码。我必须能够用不同的语言搜索该单词。因此,如果用户单击单词(日本语)或(简历),我必须能够在数据库中找到它。 对于数据存储,我将 CSV 文件转换为 .sqlite 文件。
我想知道是否有更好的方法来做到这一点。我个人不喜欢使用 < a href="">获取链接,但我找不到任何其他方法来获取链接中的当前单词。另外,在数据库中执行该单词的搜索并不干净,因为我没有为每一行使用任何 UID。我只是将保存该单词的字符串与数据库相应列中的单词进行比较。
有没有更有效的方法来做到这一点?
要恢复:获得单词列表,在 HTML 中标记这些单词,以便用户可以触摸并从数据库中检索有关该单词的信息。
I'm creating an interactive book for learning languages. It has reading, quiz and some simple game. The content of each chapter is an HTML file. The book allows the user to learn about 300 words that exist in the text. Earch word was enclosed in a link like this: < a href="word">word< /a> when the user touch the link, a modal view appears with the translation and information about that word.
This is the code I'm using to get the link:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// currentWord is the word in the link
self.currentWord = [[request URL] lastPathComponent];
// okToCatchLinks is a BOOL that I use to avoid showing the modalview when the page
// is loaded for the first time.
if (okToCatchLinks){
NSLog(@"Ok to catch links!");
WordViewController *viewController = [[WordViewController alloc] initWithNibName:@"WordViewController" bundle:nil]
// I did my homework creating the delegate protocol to dismiss the modal view.
viewController.delegate = self;
// This is a label in the modalview showing the word in the HTML link.
viewController.labelTitle = self.currentWord;
// Create a Navigation controller to add the "DONE" dismiss button
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:viewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// show the navigation controller modally
[self presentModalViewController:navController animated:YES];
// Clean up resources
[navController release];
[viewController release];
}
okToCatchLinks = YES;
return YES;
}
With this code I will get the selected word in a string variable. Then I search that word in the DB using CoreData.
The HTML is UTF-8 encoded. I have to be able to search for that word in different languages. So if the user click on the word (日本語) or (résumé) I have to be able to find it in the DB.
For the data store I converted an CSV file to a .sqlite file.
I wonder if there is a better way to do this. Personally I don't like to use < a href=""> to get the link, but I couldn't find any other way to get the current word in a link. Also performing the search of that word in the DB is not clean, because I'm not using any UID for each row. I just compare the string holding the word with the words in the corresponding column of the DB.
Is there a more efficient way to do this?
To resume: Having a list of words, mark those words in the HTML so the user can touch and retrieve information about that word from a DB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些观察结果:
UID:只要您的单词是唯一的,您就可以将该列用作主键,而无需使用单独的 UID。实际上,Core Data 无论如何都会为您创建一个,因此您可以使用
objectWithID
来检索您的单词。HTML 链接: 您使用
UIWebView
、HTML 文件和链接的解决方案对我来说似乎是可行的。但是,您当然也可以使用UITextView
并使用NSRange
以编程方式处理选择。这可能会更复杂,但也更“本土”。委托:如果您想将信息传递回首先呈现模型视图控制器的视图,则只需要委托(作业太多!;-))。只需从内部将其解雇
Some observations:
UIDs: As long as your words are unique, you can use that column as the primary key without using a separate UID. Actually, Core Data will create one for you anyway, so you can use
objectWithID
to retrieve your word.HTML Links: Your solution with
UIWebView
, HTML files and links seems feasible to me. However, you could of course also use aUITextView
and handle the selections programmatically, working withNSRange
. That would perhaps be more complicated, but also more "native".Delegate: You only need a delegate if you want to pass information back to the view that first presented the model view controller (too much homework! ;-)). Just dismiss it from within with