Load local html into UIWebView using swift(使用 swift 将本地 html 加载到 UIWebView)
问题描述
今天一大早就让我发疯了.我想将一些本地 html 加载到 web 视图中:
This one is driving me crazy early this morning. I want to load some local html into a web view:
class PrivacyController: UIViewController {
@IBOutlet weak var webView:UIWebView!
override func viewDidLoad() {
let url = NSURL(fileURLWithPath: "privacy.html")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
}
}
html 文件位于我项目的根文件夹中,但在一个组内.webview对我来说是空白的.有什么想法有什么问题吗?我在 xcode 6.1 上并在我的 iphone 6 上运行这个示例.
The html file is located in the root folder of my project but is inside a group. The webview is blank for me. Any ideas whats wrong? I am on xcode 6.1 and running this example on my iphone 6.
推荐答案
要获取应用程序资源的 URL,你应该使用 NSBundle
类的 URLForResource
方法.
To retrieve URLs for application resources, you should use URLForResource
method of NSBundle
class.
斯威夫特 2
let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html")
斯威夫特 3
let url = Bundle.main.url(forResource: "privacy", withExtension: "html")
这篇关于使用 swift 将本地 html 加载到 UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 swift 将本地 html 加载到 UIWebView


基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01