Skip to content

Commit

Permalink
webview的back和forward事件处理
Browse files Browse the repository at this point in the history
  • Loading branch information
豆子 authored and 豆子 committed Nov 17, 2022
1 parent 8972fc0 commit fdecd33
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 41 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file not shown.
40 changes: 4 additions & 36 deletions Love Browser/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ struct ContentView: View {

@State private var text = ""
@State private var isSearch = false
@State private var canBack = false
@State private var canForward = false
@State private var showHome = false
@State private var backgroundImage = ""
@State private var backgroundImage = "default"


@Environment(\.managedObjectContext) private var viewContext
Expand Down Expand Up @@ -56,52 +53,23 @@ struct ContentView: View {

if isSearch {

HomeWebView(text: $text)
HomeWebView(text: $text, isSearch: $isSearch, backgroundImage: $backgroundImage)

} else {

HomePageView() { query in
HomePageView(backgroundImage: $backgroundImage) { query in
// 跳转到网页
text = query
isSearch = true
showHome = true
}

.background(
Image(backgroundImage)
.resizable()
.aspectRatio(contentMode: .fill)
)
.padding(.top,10)
}

BottomBar(clickHomeButton: {

isSearch = false
showHome = false

}, clickBackButton: {


}, clickForwardButton: {

}, changeWallpaper: { str in

// 切换壁纸
backgroundImage = str

}, openTabsView: {
// open tabs View

preparePreview { image in

// UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)

// let model = TabsModel(title: "", image: image ?? UIImage())
// TabManager(model: model)
}

}, canBack: $canBack, canForward: $canForward, showHome: $showHome)

}

.navigationBarHidden(true)
Expand Down
32 changes: 31 additions & 1 deletion Love Browser/Views/HomePageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import SwiftUI

struct HomePageView: View {

@State private var canBack = false
@State private var canForward = false
@State private var showHome = false

@Binding var backgroundImage: String

@FetchRequest(fetchRequest: HomePageCategory.all) private var homePgaeCategorys:FetchedResults<HomePageCategory>

Expand Down Expand Up @@ -73,16 +79,40 @@ struct HomePageView: View {
}
}
}
.padding(.top, 20)

BottomBar(clickHomeButton: {

showHome = false

}, clickBackButton: {


}, clickForwardButton: {

}, changeWallpaper: { str in

// 切换壁纸
backgroundImage = str

}, openTabsView: {
// open tabs View


}, canBack: $canBack, canForward: $canForward, showHome: $showHome)

}

}
}

struct HomePageView_Previews: PreviewProvider {

@State static var backgroundImage = "bg1"

static var previews: some View {

HomePageView { query in
HomePageView(backgroundImage: $backgroundImage) { query in

}
}
Expand Down
33 changes: 32 additions & 1 deletion Love Browser/Views/HomeWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import SwiftUI
struct HomeWebView: View {

@Binding var text: String
@Binding var isSearch: Bool
@Binding var backgroundImage: String

@State private var canBack = false
@State private var canForward = false
@State private var showHome = true


@ObservedObject var webViewModel = WebViewModel()

Expand Down Expand Up @@ -56,9 +63,33 @@ struct HomeWebView: View {
formatter1.dateStyle = .short
saveSearchHistoryCategory(date:formatter1.string(from: Date.now), title: title, url: url)


canBack = webViewModel.webViewCanGoBack()
canForward = webViewModel.webViewCanGoForward()
}

BottomBar(clickHomeButton: {

isSearch = false

}, clickBackButton: {

webViewModel.webView.goBack()

}, clickForwardButton: {

webViewModel.webView.goForward()

}, changeWallpaper: { str in

// 切换壁纸
backgroundImage = str

}, openTabsView: {
// open tabs View


}, canBack: $canBack, canForward: $canForward, showHome: $showHome)

}

}
Expand Down
1 change: 1 addition & 0 deletions Love Browser/Views/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct MenuView: View {
.padding(.top,24)

}
.background(Color.white)
.edgesIgnoringSafeArea([.bottom])
.frame(height: 267)
}
Expand Down
1 change: 0 additions & 1 deletion Love Browser/Views/WebSearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ struct WebSearchView: View {
.padding(.top, -8)

}


}

Expand Down
16 changes: 14 additions & 2 deletions Love Browser/Views/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ class WebViewModel: ObservableObject {
}
}

func webViewCanGoBack() -> Bool {
return webView.canGoBack

}

func fetchDataFromWebView() -> WKBackForwardList {
func webViewCanGoForward() -> Bool {

return webView.backForwardList
return webView.canGoForward
}

}
Expand Down Expand Up @@ -110,6 +114,14 @@ class WebViewCoordinator: NSObject,WKNavigationDelegate {
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {

}

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {

}

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {

}

Expand Down

0 comments on commit fdecd33

Please sign in to comment.