Three20显示本地html文件

时间:2011-06-03 21:22:39

标签: html ios three20

我正在尝试从three20表控制器显示本地html文件。表格显示正确,但是当我选择项目时,我得到的只是一个空白屏幕。如果我使用外部URL,页面会正确显示。我没有尝试使用UIWebView,但我想知道是否有一种使用Three20框架的简单方法。

感谢。

1 个答案:

答案 0 :(得分:2)

编辑:我最近忘记了that has changed ...

假设您已在导航器中注册了TTWebController

TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:@"*" toViewController:[TTWebController class]];

您可以通过以下方式打开本地html文件:

NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
TTOpenURL([url description]);

然后您只需将TTTableLinkedItem或子类(几乎所有TT * Cell都添加)添加到您的dataSource,并使用本地文件URL:

NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
TTTableTextItem *item = [TTTableTextItem itemWithText:@"foobar" URL:[url description]];

比我之前写的更简单......

<强> ENDOFEDIT

忘记了什么,除非你对别人的解决方案感兴趣......(更复杂......或者不是,请参阅最后一篇)


基本上,您可以在UIWebView

中加载内容
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];

three20 TTWebController可以在查询中接收请求:

- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
  if (self = [self initWithNibName:nil bundle:nil]) {
    NSURLRequest* request = [query objectForKey:@"request"];
    if (nil != request) {
      [self openRequest:request];
    } else {
      [self openURL:URL];
    }
  }
  return self;
}

因此,要在TTWebController中打开本地html文件,您可以执行此操作:

NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[TTNavigator navigator] openURLAction:
 [[[TTURLAction actionWithURLPath:@"url://to/your/ttwebcontroller"]
   applyQuery:[NSDictionary dictionaryWithObject:ulrRequest
                                          forKey:@"request"]]
  applyAnimated:YES]];

现在是最后一部分,从TTTableViewController触发... 您已在ViewController中实施:

- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
  // there you retrieve a *TTTableItem* corresponding to your row
  // and call previous snippet from URL...
  // TTTableItem class has userInfo property you could use:
  TTTableItem *item = (TTTableItem *)object;
  NSString *htmlFile = item.userInfo.
  [self openLocalHtmlFile:htmlFile];
}

希望有所帮助! 没有任何测试,根本不应该编译或任何东西,但这是一个解决方案。

这是一个使用基本的three20 + iOS功能的解决方案。你也可以写一个继承自TTCustomWebController TTWebController的{​​{1}},它会像@"myapp://openLocalHtml?file=credits"这样的网址那样难以做到......

......这是草稿:

@interface TTCustomWebController : TTWebController {}
@end

@implementation TTCustomWebController

- (id)initWithFile:(NSString *)name {
  self = [super init];
  if (self) {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"html"];
    NSURL *URL = [NSURL fileURLWithPath:filePath];
    [self openURL:URL];
  }
  return self;
}

@end

然后您可以在数据源中使用TTTableLinkedItem指向这些@"myapp://openLocalHtml?file=credits" ...

祝你好运! :)