iOS 使用 HTML 链接获取数据

发布于 2024-12-06 08:36:04 字数 2121 浏览 1 评论 0原文

我正在制作一本用于学习语言的互动书。它有阅读、测验和一些简单的游戏。每章的内容都是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆伤 2024-12-13 08:36:04

一些观察结果:

UID:只要您的单词是唯一的,您就可以将该列用作主键,而无需使用单独的 UID。实际上,Core Data 无论如何都会为您创建一个,因此您可以使用 objectWithID 来检索您的单词。

HTML 链接: 您使用 UIWebView、HTML 文件和链接的解决方案对我来说似乎是可行的。但是,您当然也可以使用 UITextView 并使用 NSRange 以编程方式处理选择。这可能会更复杂,但也更“本土”。

委托:如果您想将信息传递回首先呈现模型视图控制器的视图,则只需要委托(作业太多!;-))。只需从内部将其解雇

[self dismissModalViewControllerAnimated:YES];

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 a UITextView and handle the selections programmatically, working with NSRange. 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

[self dismissModalViewControllerAnimated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文