应用程序内的 pdf 链接

发布于 2024-10-11 15:38:41 字数 256 浏览 2 评论 0原文

我有一个 iOS 应用程序,它会在网络视图中打开一个指向网站的链接。这些链接保存在 plist 文件中(因此随着应用程序的发展很容易维护它们)。接下来我想做的是链接到保存在应用程序中的 PDF(或任何文本文件格式的图片,甚至是 html 格式,这很灵活)。我希望在现有的应用程序结构中尽可能地做到这一点。那么,是否可以创建一个可以作为 Web 链接放入 plist 中的链接,但可以在设备本身上打开一个文件(可能在 Web 视图中)?我该怎么做呢?有什么想法吗?

提前感谢您的帮助!

I've got an iOS app that at one point opens a link to a website in a webview. These links are kept in a plist file (so it is easy to maintain them as the app evolves). What I want to do next is to also link to PDF's (or any picture of text file format, of even a html format, this is flexible) that are kept within the app. And I would like to do this as much as possible from within the existing app structure. So, is it possible to create a link that can be put in the plist as a web-link, but instead opens a file on the device itself (possibly in the webview)? And how would I go about that? Any ideas?

Thanx in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鹊巢 2024-10-18 15:38:41

您需要在运行时创建链接。我建议在本地 URL 中使用特定的前缀,例如 mylocalfile:filename。然后,在加载 plist 的代码中,检查前缀并在必要时创建链接。您也可以只创建这些链接一次并将它们存储在单独的文件中,然后加载该链接而不是原始文件。

NSArray *links = nil; //I assumed your plist is an array. Change to dictionary if required
NSString *pathToStoredFile; //Get the path for the file you create with the updated links
if([[NSFileManager defaultManager] fileExistsAtPath:pathToStoredFile]) {
    links = [NSArray arrayWithContentsOfFile:pathToStoredFile];
} else {
    NSArray *tmp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ListOfLinks" ofType:@"plist"]];
    if(!tmp) {
        //handle error
    }
    NSMutableArray *tmpLinks = [[NSMutableArray alloc] initWithCapacity:[tmp count]];
    for(NSString *link in tmp) {
        if([link hasPrefix:@"mylocalfile:"]) {
            link = [link substringFromIndex:12]; //12 is the length of mylocalfile:
            NSURL *url = [[NSBundle mainBundle] urlForResource:[link stringByDeletingPathExtension] withExtension:[link pathExtension]];
            [tmpLinks addObject:[url absoluteString]];
        } else [tmpLinks addObject:link];
    }
    links = [tmpLinks copy];
    [tmpLinks release];
    [links writeToFile:pathToStoredFile atomically:NO];
}

You will need to create the links at runtime. I would suggest having a certain prefix to a local url, such as mylocalfile:filename. Then, in the code that loads the plist, check for the prefix and create the link when necessary. You could also just create these links once and store them in a separate file, then load that instead of the original.

NSArray *links = nil; //I assumed your plist is an array. Change to dictionary if required
NSString *pathToStoredFile; //Get the path for the file you create with the updated links
if([[NSFileManager defaultManager] fileExistsAtPath:pathToStoredFile]) {
    links = [NSArray arrayWithContentsOfFile:pathToStoredFile];
} else {
    NSArray *tmp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ListOfLinks" ofType:@"plist"]];
    if(!tmp) {
        //handle error
    }
    NSMutableArray *tmpLinks = [[NSMutableArray alloc] initWithCapacity:[tmp count]];
    for(NSString *link in tmp) {
        if([link hasPrefix:@"mylocalfile:"]) {
            link = [link substringFromIndex:12]; //12 is the length of mylocalfile:
            NSURL *url = [[NSBundle mainBundle] urlForResource:[link stringByDeletingPathExtension] withExtension:[link pathExtension]];
            [tmpLinks addObject:[url absoluteString]];
        } else [tmpLinks addObject:link];
    }
    links = [tmpLinks copy];
    [tmpLinks release];
    [links writeToFile:pathToStoredFile atomically:NO];
}
深海蓝天 2024-10-18 15:38:41

是的,我会选择 UIWebView。 iOS 应该能够自动处理某些 URL 处理程序,并且您的应用程序可以 根据需要注册以处理其余部分。

iOS 已经知道如何处理某些文件类型。例如,如果 Safari(或 UIWebView)遇到 http://somesite.com/afile.pdf,它就知道哪些应用程序可以处理该文件类型。另一个例子是电话号码:skype://8005555555。 iOS 知道打开 Skype 并将号码传递给它。 iOS 还知道 iBooks 可以处理 PDF 文件。

为您的应用程序注册适当的文件处理程序和类型。然后,用户可以点击并按住该链接以查看可用于处理该链接的应用程序的菜单。如果它是仅由一个应用程序使用的链接,则用户甚至不需要按住,轻按一下就足够了。

至于创建指向本地文件的链接,您可以使用 C 函数 NSDocumentsDirectory() 并将其附加到 url 处理程序。 (示例:http://NSDocumentsDirectory()/filename.pdf

Yes, I would go with a UIWebView. iOS should be able automatically handle certain URL handlers and your app can register to handle the rest, as necessary.

iOS knows how to handle certain file types already. For example, if Safari (or a UIWebView) encounters http://somesite.com/afile.pdf, it know which apps can handle the file type. Another example is a phone number: skype://8005555555. iOS knows to open Skype and pass the number to it. iOS also knows that iBooks can handle PDf files.

Register your app for the appropriate file handlers and types. Then, users can tap and hold on the link to see a menu of available apps to handle the link. If it's a link that's only used by one app, the user doesn't even need to hold, a tap will suffice.

As far as making a link pointing to a local file, you can, and you would use the C function NSDocumentsDirectory() and append that to a url handler. (Example: http://NSDocumentsDirectory()/filename.pdf)

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