forked from NikantVohra/HackerNewsClient-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebViewController.swift
More file actions
93 lines (75 loc) · 2.73 KB
/
WebViewController.swift
File metadata and controls
93 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// WebViewController.swift
// HackerNews
//
// Created by Vohra, Nikant on 27/03/15.
// Copyright (c) 2015 Vohra, Nikant. All rights reserved.
//
import UIKit
class WebViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
var url: String!
var story: HNPost?
@IBOutlet weak var progressView: UIProgressView!
var hasFinishedLoading = false
@IBAction func didPressShareButton(sender: AnyObject) {
var title = story!.Title
var url = story!.UrlString
let activityViewController = UIActivityViewController(activityItems: [title, url], applicationActivities: nil)
activityViewController.setValue(title, forKey: "subject")
activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop]
presentViewController(activityViewController, animated: true, completion: nil)
}
@IBAction func closeButtonDidTouch(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
}
override func viewDidLoad() {
super.viewDidLoad()
let targetURL = NSURL(string: url)
let request = NSURLRequest(URL: targetURL!)
webView.loadRequest(request)
webView.delegate = self
}
func webViewDidStartLoad(webView: UIWebView) {
hasFinishedLoading = false
updateProgress()
}
func webViewDidFinishLoad(webView: UIWebView) {
delay(1) { [weak self] in
if let _self = self {
_self.hasFinishedLoading = true
}
}
}
deinit {
webView.stopLoading()
webView.delegate = nil
}
func updateProgress() {
if progressView.progress >= 1 {
progressView.hidden = true
} else {
if hasFinishedLoading {
progressView.progress += 0.002
} else {
if progressView.progress <= 0.3 {
progressView.progress += 0.004
} else if progressView.progress <= 0.6 {
progressView.progress += 0.002
} else if progressView.progress <= 0.9 {
progressView.progress += 0.001
} else if progressView.progress <= 0.94 {
progressView.progress += 0.0001
} else {
progressView.progress = 0.9401
}
}
delay(0.008) { [weak self] in
if let _self = self {
_self.updateProgress()
}
}
}
}
}