Skip to content

Commit

Permalink
Update lỗi timer, gốc vấn đề do thằng webview cũ nó reload liên tục
Browse files Browse the repository at this point in the history
  • Loading branch information
oneweekstudio committed Dec 26, 2019
1 parent 27c6adc commit bf3700c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 29 deletions.
2 changes: 1 addition & 1 deletion SmartmoveMobileAdsSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SmartmoveMobileAdsSDK'
s.version = '2.0.4'
s.version = '2.1.0'
s.summary = 'A short description of SmartmoveMobileAdsSDK.'

# This description is used to generate tags and improve search results.
Expand Down
103 changes: 78 additions & 25 deletions SmartmoveMobileAdsSDK/Classes/SMADNativeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,89 @@ import SafariServices
- Chú ý: Controller này chỉ xử lý deeplink

*/

class TimerManager : NSObject {

static let `default` = TimerManager()

var updateBlock: ((String) -> Void)?
var finishBlock: (() -> Void)?

private var countdown = 5
private var timer: Timer? = nil

private var isLoaded: Bool = false

func stopTimer() {
self.timer?.invalidate()
self.timer = nil
}

func startTimer() {
if isLoaded == false {
print("Start timer")
DispatchQueue.main.async {
self.stopTimer()
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateCounter), userInfo: nil, repeats: true)
}
isLoaded = true
}

}

@objc private func updateCounter() {
DispatchQueue.main.async {
self.countdown -= 1
print("Shot : \(self.countdown)")
self.updateBlock?("Remaining \(self.countdown)")
if self.countdown <= 0 {
self.timer?.invalidate()
self.timer = nil
self.countdown = 5
self.finishBlock?()
self.isLoaded = false
print("Kill timer")
}
}
}

}


public class SMADNativeViewController : UIViewController {

@IBOutlet weak var lblTitle: UILabel?
@IBOutlet weak var webView: UIWebView!

//Để vào trong thằng này 1 object, test thì để 1 cái string
var dynamicLink: String?
var countdown = 5
var timer: Timer?

override public func viewDidLoad() {
super.viewDidLoad()
self.load()
TimerManager.default.stopTimer()
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

TimerManager.default.updateBlock = { [weak self] str in
guard let `self` = self else { return }
self.lblTitle?.text = str
}

TimerManager.default.finishBlock = {
if self.webView != nil {
self.webView.removeFromSuperview()
}
self.willResignActive()
}

}

@objc func willResignActive() {
log.debug("UIApplicationDidEnterBackground: -> Ẩn controller xử lý deeplink ")
log.debug("Đóng SMADNativeViewController")
self.timer?.invalidate()
self.timer = nil
print("UIApplicationDidEnterBackground: -> Ẩn controller xử lý deeplink ")
print("Đóng SMADNativeViewController")
TimerManager.default.stopTimer()
self.dismiss(animated: false, completion: {

NotificationCenter.default.post(name: NSNotification.Name.init("DidfinshLoad"), object: nil)
})
}
Expand All @@ -47,13 +108,15 @@ public class SMADNativeViewController : UIViewController {

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
TimerManager.default.stopTimer()
}

public override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.webView.removeFromSuperview()
self.timer?.invalidate()
self.timer = nil
if self.webView != nil {
self.webView.removeFromSuperview()
}
TimerManager.default.stopTimer()
}
}

Expand All @@ -72,7 +135,7 @@ extension SMADNativeViewController : SFSafariViewControllerDelegate {
let url = URL.init(string: link)
else { return }

log.debug("URL: = \(link)")
print("URL: = \(link)")
let urlRequest = URLRequest.init(url: url)
self.redirectLink(URLRequest: urlRequest)

Expand All @@ -89,28 +152,18 @@ extension SMADNativeViewController : UIWebViewDelegate {


public func webViewDidStartLoad(_ webView: UIWebView) {
log.debug("webViewDidStartLoad")
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateCounter), userInfo: nil, repeats: true)
// print("webViewDidStartLoad")
TimerManager.default.startTimer()
}

public func webViewDidFinishLoad(_ webView: UIWebView) {
log.debug("webViewDidFinishLoad")
// print("webViewDidFinishLoad")
}

public func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
log.debug("didFailLoadWithError: \(error)")
//print("didFailLoadWithError: \(error)")
}

@objc func updateCounter() {
self.countdown -= 1
log.debug("Time left: \(self.countdown)")
self.lblTitle?.text = "Remaining \(self.countdown)"
if countdown == 0 {
self.timer?.invalidate()
self.timer = nil
self.countdown = 5
self.willResignActive()
}
}


}
6 changes: 3 additions & 3 deletions SmartmoveMobileAdsSDK/Classes/SMADRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ open class SMADRequest: NSObject {
device_type = "iphone"
}

bundleID = Bundle.main.bundleIdentifier!
bundleID = "com.harry.ScanningApp1"
// log.debug("Debug mode = \(SMADMobileAds.isDebug) : \(bundleID)")
baseParam = ["package_name":bundleID,
"lang":NSLocale.preferredLanguages[0],
Expand All @@ -72,11 +72,11 @@ open class SMADRequest: NSObject {
.responseJSON {response in
switch (response.result){
case.success(let data):
log.debug("--> \(data)")
print("--> \(data)")
completionHandler?(data)
break
case .failure(let error):
log.debug(error)
print(error)
failureHandler?(error)
break
}
Expand Down

0 comments on commit bf3700c

Please sign in to comment.