【问题标题】:How to set file-path in ViewController?如何在 ViewController 中设置文件路径?
【发布时间】:2018-07-16 13:37:52
【问题描述】:

这是我的项目结构:

我有 2 个不同的视图控制器 (GameViewController, TaskViewController)。他们都应该能够向用户显示web view

  • GameViewControllerWeb 视图 应加载文件www/game/index.html

  • TaskViewControllerWeb 视图 应加载文件 www/task/index.html

添加网页视图非常简单。但是我怎样才能确定正确的路径呢?

这是我在GameViewController 中尝试过的:

let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "www/game") // not working

let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "game") // not working

let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "myApplication/www/game") // not working

如何正确设置文件的路径(同名 [index.html] )?

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 您必须将 HTML 文件作为“文件夹引用”添加到您的项目中,否则所有文件将被复制到应用程序包中的同一目录中。参见例如stackoverflow.com/questions/6420925/…

标签: ios iphone swift webview uiviewcontroller


【解决方案1】:

尝试添加选中引用的文件,假设我有一个名为 pp 的文件夹,其中有一个 premium.txt 当我将它拖到 Xcode selected >创建文件夹引用并用子目录轻松引用它,文件夹将具有蓝色

【讨论】:

  • 我删除了我的 html 文件并将它们再次拖到文件夹中。我还启用了“创建文件夹引用”,但使用此代码:let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "game") 也不起作用。
  • 游戏文件夹必须直接包含 index.html 没有内部子目录,然后才能将文件夹拖到 Xcode ????
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2016-02-17
  • 2019-09-30
  • 2015-02-12
相关资源
最近更新 更多