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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
cookies control for WKWebView
DaidoujiChen committed Jul 4, 2020
commit abd3f62f268fcbd92a8f222c7b5907a63ad686dc
12 changes: 8 additions & 4 deletions Dai-Hentai.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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 = "";
@@ -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 = "";
4 changes: 2 additions & 2 deletions Dai-Hentai/Info.plist
Original file line number Diff line number Diff line change
@@ -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>
112 changes: 80 additions & 32 deletions Dai-Hentai/Models/ExCookieChecker/ExCookie.swift
Original file line number Diff line number Diff line change
@@ -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) {
@@ -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)
}
@@ -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
@@ -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);
@@ -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 是否正常