-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73caab5
commit 116fdc8
Showing
23 changed files
with
1,233 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# webview | ||
```swift | ||
// | ||
// ContentView.swift | ||
// content-pwa | ||
// | ||
// Created by jiayou on 2024/03/25. | ||
// | ||
|
||
import SwiftUI | ||
import WebKit | ||
import UIKit | ||
import Dispatch | ||
|
||
struct WebViewContainer: UIViewRepresentable { | ||
@Binding var urlString: String | ||
@Binding var showAddressBar: Bool | ||
@Binding var progress: Float | ||
let configuration: WKWebViewConfiguration | ||
|
||
func makeUIView(context: Context) -> WKWebView { | ||
let webview = WKWebView(frame: .zero,configuration: configuration) | ||
webview.navigationDelegate = context.coordinator | ||
return webview; | ||
} | ||
func updateUIView (_ uiView: WKWebView, context: Context){ | ||
if let url = URL(string: urlString) { | ||
let request = URLRequest(url: url); | ||
uiView.load(request); | ||
uiView.allowsBackForwardNavigationGestures = true | ||
} | ||
} | ||
func makeCoordinator() -> Coordinator { | ||
return Coordinator(parent: self) | ||
} | ||
|
||
class Coordinator: NSObject, WKNavigationDelegate { | ||
var parent: WebViewContainer | ||
|
||
init(parent: WebViewContainer) { | ||
self.parent = parent | ||
} | ||
|
||
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | ||
self.parent.urlString = webView.url?.absoluteString ?? "" | ||
} | ||
} | ||
|
||
} | ||
|
||
struct ContentView: View { | ||
@State private var realURL: String = "http://max.local:3000/novel" | ||
@State private var urlString: String = "http://ios.nat300.top/novel" | ||
@State private var showAddressBar: Bool = true | ||
@State private var progress: Float = 0 | ||
var body: some View { | ||
// https://7ce0-27-38-191-148.ngrok-free.app | ||
WebViewContainer(urlString: $realURL, showAddressBar: $showAddressBar, progress: $progress, configuration: createConfiguration()) | ||
.edgesIgnoringSafeArea(.all) | ||
} | ||
|
||
func createConfiguration() -> WKWebViewConfiguration { | ||
let configuration = WKWebViewConfiguration() | ||
configuration.allowsInlineMediaPlayback = true; | ||
// 添加自定义偏好设置 | ||
let preferences = WKPreferences() | ||
return configuration; | ||
} | ||
} | ||
|
||
struct ContentView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
ContentView() | ||
} | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# shttp 库 | ||
|
||
```swift | ||
// | ||
// shttp.swift | ||
// content-pwa | ||
// | ||
// Created by jiayou on 2024/04/17. | ||
// | ||
|
||
import Foundation | ||
import SwiftyJSON | ||
|
||
// 添加 SwiftyJSON 库 | ||
// 使用示例 | ||
// SHttp | ||
// .get("https://u67631x482.vicp.fun/gw/novel/v1/public/boot") | ||
// .send { result in | ||
// switch result { | ||
// case .success(let data): | ||
// // 处理返回的数据 | ||
// let code = data["code"].int | ||
// guard let data = try? data["data"].rawData() else { | ||
// return | ||
// } | ||
// do { | ||
// self.data = try JSONDecoder().decode(BootData.self, from: data) | ||
// } catch{ | ||
// print("fail") | ||
// } | ||
// print("Received data: code: \(code ?? 0)") | ||
// case .failure(let error): | ||
// // 处理错误 | ||
// print("Network request failed with error: \(error)") | ||
// } | ||
// | ||
// } | ||
//} | ||
enum NetworkError: Error { | ||
case invalidURL | ||
case requestFailed | ||
case invalidResponse | ||
case invalidData | ||
case bizError | ||
} | ||
|
||
enum JSONResult { | ||
case dictionary([String: Any]) | ||
case swiftyJSON(JSON) | ||
case NetworkError | ||
} | ||
|
||
class SHttp { | ||
static func head (_ url: String) -> Request { | ||
return Request(url).setMethod("HEAD") | ||
} | ||
static func get (_ url: String) -> Request { | ||
return Request(url).setMethod("GET") | ||
} | ||
static func post (_ url: String) -> Request { | ||
return Request(url).setMethod("POST") | ||
} | ||
static func put (_ url: String) -> Request { | ||
return Request(url).setMethod("PUT") | ||
} | ||
static func delete (_ url: String) -> Request { | ||
return Request(url).setMethod("DELETE") | ||
} | ||
static func patch (_ url: String) -> Request { | ||
return Request(url).setMethod("PATCH") | ||
} | ||
|
||
} | ||
|
||
class Request { | ||
|
||
private var url: URL? | ||
private var method: String = "GET" | ||
private var query: [URLQueryItem]? | ||
private var body: Data? | ||
private var headers: [String: String]? | ||
|
||
init(_ url: String) { | ||
self.url = URL(string: url) | ||
} | ||
|
||
func setMethod(_ method: String) -> Request { | ||
self.method = method | ||
return self | ||
} | ||
|
||
func setQuery(_ query: [URLQueryItem]) -> Request { | ||
self.query = query | ||
return self | ||
} | ||
|
||
func setBody(_ body: Data) -> Request { | ||
self.body = body | ||
return self | ||
} | ||
|
||
func setHeaders(_ headers: [String: String]) -> Request { | ||
self.headers = headers | ||
return self | ||
} | ||
|
||
func send(completion: @escaping (Result<JSON, NetworkError>) -> Void) { | ||
guard let url = self.url else { | ||
completion(.failure(.invalidURL)) | ||
return | ||
} | ||
|
||
var components = URLComponents(url: url, resolvingAgainstBaseURL: false) | ||
components?.queryItems = self.query | ||
|
||
guard let finalURL = components?.url else { | ||
completion(.failure(.invalidURL)) | ||
return | ||
} | ||
|
||
var request = URLRequest(url: finalURL) | ||
request.httpMethod = method | ||
request.httpBody = body | ||
|
||
if let headers = headers { | ||
for (key, value) in headers { | ||
request.setValue(value, forHTTPHeaderField: key) | ||
} | ||
} | ||
|
||
let session = URLSession.shared | ||
let task = session.dataTask(with: request) { data, response, error in | ||
guard error == nil else { | ||
completion(.failure(.requestFailed)) | ||
return | ||
} | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else { | ||
completion(.failure(.invalidResponse)) | ||
return | ||
} | ||
|
||
guard let data = data else { | ||
completion(.failure(.invalidData)) | ||
return | ||
} | ||
|
||
// 将数据转换为 JSON 对象 | ||
let json = JSON(data) | ||
completion(.success(json)) | ||
return | ||
} | ||
task.resume() | ||
} | ||
} | ||
|
||
``` |
Oops, something went wrong.