75 Display of the file in UIWebView

Swift3.0 // // ViewController.swift // UIKit075 // // Created by Misato Morino on 2016/08/15. // Copyright © 2016年 Misato Morino. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // WebViewを生成. let myWebView = UIWebView() myWebView.frame = self.view.bounds // viewにWebViewを追加. self.view.addSubview(myWebView) // プロジェクトに管理されているファイルのpathを取得. let path: NSString = Bundle.main.path(forResource: "document", ofType: "pdf")! let url = NSURL(fileURLWithPath: path as String) let request = NSURLRequest(url: url as URL) // リクエストロード. myWebView.loadRequest(request as URLRequest) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } Swift 2.3 // // ViewController.swift // UIKit075 // // Created by Misato Morino on 2016/08/15. // Copyright © 2016年 Misato Morino. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // WebViewを生成. let myWebView = UIWebView() myWebView.frame = self.view.bounds // viewにWebViewを追加. self.view.addSubview(myWebView) // プロジェクトに管理されているファイルのpathを取得. let path: NSString = NSBundle.mainBundle().pathForResource("document", ofType: "pdf")! let url = NSURL(fileURLWithPath: path as String) let request = NSURLRequest(URL: url) // リクエストロード. myWebView.loadRequest(request) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 2.3と3.0の差分 NSBundle.mainBundle().pathForResource から Bundle.main.path に変更

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.