Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用WKWebView來替代已棄用的UIWebView #153

Open
wants to merge 4 commits into
base: 3.0_develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Dai-Hentai.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -1046,7 +1046,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
Expand All @@ -1064,10 +1064,12 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 20200703;
DEVELOPMENT_TEAM = XLP7NJ2T65;
INFOPLIST_FILE = "Dai-Hentai/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 3.11;
PRODUCT_BUNDLE_IDENTIFIER = "tw.daidouji.Dai-Hentai";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand All @@ -1087,10 +1089,12 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 20200703;
DEVELOPMENT_TEAM = XLP7NJ2T65;
INFOPLIST_FILE = "Dai-Hentai/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 3.11;
PRODUCT_BUNDLE_IDENTIFIER = "tw.daidouji.Dai-Hentai";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand Down
4 changes: 2 additions & 2 deletions Dai-Hentai/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>3.9</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>20190922</string>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand Down
112 changes: 80 additions & 32 deletions Dai-Hentai/Models/ExCookieChecker/ExCookie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,43 @@
//

import Foundation
import WebKit

class ExCookie: NSObject {

// MARK: - Property

private static let url = URL(string: "https://e-hentai.org")!
private static let ehDomain = ".e-hentai.org"
private static let exDomain = ".exhentai.org"

// MARK: - Static Function

@objc static func isExist() -> Bool {
var isExist = false
guard let cookies = HTTPCookieStorage.shared.cookies(for: url) else {
return isExist
}

for cookie in cookies {
if let expiresDate = cookie.expiresDate, cookie.name == "ipb_pass_hash" {
if NSDate().compare(expiresDate) != .orderedAscending {
isExist = false
} else {
isExist = true
}
}
if isCookiesInStorage() {
return true
}

if isExist {
replace()
}
return isExist
findCookiesInWKDataStore()

return false
}

@objc static func clean() {

// 一般的 request 用的 cookies 放在 HTTPCookieStorage
let shared = HTTPCookieStorage.shared
for cookie in shared.cookies ?? [] {
shared.deleteCookie(cookie)
}

// WKWebView 的 cookie 放在 WKWebsiteDataStore
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) {
$0.forEach {
WKWebsiteDataStore.default().removeData(ofTypes: $0.dataTypes, for: [$0]) {
}
}
}
}

@objc static func manuallyAddCookie(exKey: String) {
Expand All @@ -65,7 +66,7 @@ class ExCookie: NSObject {
continue
}

properties[.domain] = ".e-hentai.org" // 將同樣的Cookie也添加到表站
properties[.domain] = ehDomain // 將同樣的Cookie也添加到表站
if let newCookie = HTTPCookie(properties: properties) {
HTTPCookieStorage.shared.setCookie(newCookie)
}
Expand All @@ -75,27 +76,74 @@ class ExCookie: NSObject {
// MARK: - Private Static Function

private static func replace() {
guard let cookies = HTTPCookieStorage.shared.cookies(for: url) else {
return
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
cookies.filter { $0.domain == ehDomain }.forEach { (cookie) in
if var newProperties = cookie.properties {
newProperties[.domain] = exDomain

if let newCookie = HTTPCookie(properties: newProperties) {

// 一般的 request 用的 cookies 放在 HTTPCookieStorage
HTTPCookieStorage.shared.setCookie(newCookie)

// WKWebView 的 cookie 放在 WKWebsiteDataStore
WKWebsiteDataStore.default().httpCookieStore.setCookie(newCookie, completionHandler: nil)
}
}
}
}
}

private static func createCookie(name: String, value: String) -> HTTPCookie {
return HTTPCookie(properties: [
.domain: exDomain,
.name: name,
.value: value,
.path: "/",
.expires: Date(timeInterval: 157784760, since: Date())
])! // 5年後過期
}

private static func isCookiesInStorage() -> Bool {
var isInStorage = false

guard
let cookies = HTTPCookieStorage.shared.cookies?.filter({ $0.domain == exDomain }),
!cookies.isEmpty else {

return isInStorage
}

for cookie in cookies {
guard var properties = cookie.properties else {
continue
if let expiresDate = cookie.expiresDate, cookie.name == "ipb_pass_hash" {
if NSDate().compare(expiresDate) != .orderedAscending {
isInStorage = false
} else {
isInStorage = true
}
}
}
return isInStorage
}

private static func findCookiesInWKDataStore() {
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in

properties[.domain] = ".exhentai.org"
if let newCookie = HTTPCookie(properties: properties) {
HTTPCookieStorage.shared.setCookie(newCookie)
var isInDataStore = false
cookies.filter { $0.domain == ehDomain }.forEach { (cookie) in
if cookie.name == "ipb_pass_hash", let expiresDate = cookie.expiresDate {
if Date().compare(expiresDate) != .orderedAscending {
isInDataStore = false
} else {
isInDataStore = true
}
}
}

if isInDataStore {
replace()
}
}
}

private static func createCookie(name: String, value: String) -> HTTPCookie {
return HTTPCookie(properties: [.domain: ".exhentai.org",
HTTPCookiePropertyKey.name: name,
HTTPCookiePropertyKey.value: value,
HTTPCookiePropertyKey.path: "/",
HTTPCookiePropertyKey.expires: Date(timeInterval: 157784760, since: Date())])! // 5年後過期
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import UIKit
import WebKit

class CheckPageViewController: UIViewController {

Expand Down Expand Up @@ -41,8 +42,8 @@ class CheckPageViewController: UIViewController {
return
}
// 開一個頁面
let webView = UIWebView(frame: view.bounds)
webView.loadRequest(URLRequest(url: url))
let webView = WKWebView(frame: view.bounds)
webView.load(URLRequest(url: url))
view.addSubview(webView)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import UIKit
import WebKit

class LoginViewController: UIViewController {

Expand Down Expand Up @@ -41,8 +42,8 @@ class LoginViewController: UIViewController {
// 開一個登入網頁
if let url = URL(string: "https://forums.e-hentai.org/index.php?act=Login&CODE=01") {
let request = URLRequest(url: url)
let webView = UIWebView(frame: view.bounds)
webView.loadRequest(request)
let webView = WKWebView(frame: view.bounds)
webView.load(request)
view.addSubview(webView)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ - (UIViewController *)checkViewControllerBy:(NSString *)reuseIdentifier {
}

- (void)displayListAndAPIStatus {
BOOL cookieExist = [ExCookie isExist];

@weakify(self);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@strongify(self);
Expand All @@ -73,7 +75,7 @@ - (void)displayListAndAPIStatus {
[self statusCheck:HentaiParserTypeEh listLabel:self.ehListCheckLabel apiLabel:self.ehAPICheckLabel];

// 如果沒有 cookies, 則直接設定字樣
if (![ExCookie isExist]) {
if (!cookieExist) {
[self localStatus:LocalStatusTypeExNotLogin listLabel:self.exListCheckLabel apiLabel:self.exAPICheckLabel];
}
// 測試 ex 是否正常
Expand Down