在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:turbolinks/turbolinks-ios开源软件地址:https://github.com/turbolinks/turbolinks-ios开源编程语言:Swift 82.0%开源软件介绍:Turbolinks for iOSBuild high-fidelity hybrid apps with native navigation and a single shared web view. Turbolinks for iOS provides the tooling to wrap your Turbolinks 5-enabled web app in a native iOS shell. It manages a single WKWebView instance across multiple view controllers, giving you native navigation UI with all the client-side performance benefits of Turbolinks. Features
RequirementsTurbolinks for iOS is written in Swift 5.0 and requires Xcode 10.2. It should also work with Swift 4.2 as well. It currently supports iOS 10 or higher, but we'll be dropping iOS 10 support in the next version. Web views are backed by WKWebView for full-speed JavaScript performance. Note: You should understand how Turbolinks works with web applications in the browser before attempting to use Turbolinks for iOS. See the Turbolinks 5 documentation for details. InstallationInstall Turbolinks manually by building Installing with CarthageAdd the following to your
Installing with CocoaPodsAdd the following to your use_frameworks!
pod 'Turbolinks', :git => 'https://github.com/turbolinks/turbolinks-ios.git' Then run Running the DemoThis repository includes a demo application to show off features of the framework. The demo bundles a simple HTTP server that serves a Turbolinks 5 web app on localhost at port 9292. To run the demo, clone this repository to your computer and change into its directory. Then, start the demo server by running Once you’ve started the demo server, explore the demo application in the Simulator by opening Getting StartedWe recommend playing with the demo app to get familiar with the framework. When you’re ready to start your own application, see our Quick Start Guide for step-by-step instructions to lay the foundation. Understanding Turbolinks ConceptsThe Session class is the central coordinator in a Turbolinks for iOS application. It creates and manages a single WKWebView instance, and lets its delegate—your application—choose how to handle link taps, present view controllers, and deal with network errors. A Visitable is a UIViewController that can be visited by the Session. Each Visitable view controller provides a VisitableView instance, which acts as a container for the Session’s shared WKWebView. The VisitableView has a pull-to-refresh control and an activity indicator. It also displays a screenshot of its contents when the web view moves to another VisitableView. When you tap a Turbolinks-enabled link in the web view, the Session asks your application how to handle the link’s URL. Most of the time, your application will visit the URL by creating and presenting a Visitable. But it might also choose to present a native view controller for the URL, or to ignore the URL entirely. Creating a SessionTo create a Session, first create a WKWebViewConfiguration and configure it as needed (see Customizing the Web View Configuration for details). Then pass this configuration to the Session initializer and set the The Session’s delegate must implement the following two methods. func session(session: Session, didProposeVisitToURL URL: NSURL, withAction action: Action) Turbolinks for iOS calls the See Responding to Visit Proposals for more details. func session(session: Session, didFailRequestForVisitable visitable: Visitable, withError error: NSError) Turbolinks calls See Handling Failed Requests for more details. Working with VisitablesVisitable view controllers must conform to the Visitable protocol by implementing the following three properties: protocol Visitable {
weak var visitableDelegate: VisitableDelegate? { get set }
var visitableView: VisitableView! { get }
var visitableURL: NSURL! { get }
} Turbolinks for iOS provides a VisitableViewController class that implements the Visitable protocol for you. This view controller displays the VisitableView as its single subview. Most applications will want to subclass VisitableViewController to customize its layout or add additional views. For example, the bundled demo application has a DemoViewController subclass that can display a custom error view in place of the VisitableView. If your application’s design prevents you from subclassing VisitableViewController, you can implement the Visitable protocol yourself. See the VisitableViewController implementation for details. Note that custom Visitable view controllers must forward their Building Your Turbolinks ApplicationInitiating a VisitTo visit a URL with Turbolinks, first instantiate a Visitable view controller. Then present the view controller and pass it to the Session’s For example, to create, display, and visit Turbolinks’ built-in VisitableViewController in a UINavigationController-based application, you might write: let visitable = VisitableViewController()
visitable.URL = NSURL(string: "http://localhost:9292/")!
navigationController.pushViewController(visitable, animated: true)
session.visit(visitable) Responding to Visit ProposalsWhen you tap a Turbolinks-enabled link, the link’s URL and action make their way from the web view to the Session as a proposed visit. Your Session’s delegate must implement the Normally you’ll respond to a visit proposal by simply initiating a visit and loading the URL with Turbolinks. See Initiating a Visit for more details. You can also choose to intercept the proposed visit and display a native view controller instead. This lets you transparently upgrade pages to native views on a per-URL basis. See the demo application for an example. Implementing Visit ActionsEach proposed visit has an Action, which tells you how you should present the Visitable. The default Action is When you follow a link annotated with Handling Form SubmissionBy default, Turbolinks for iOS prevents standard HTML form submissions. This is because a form submission often results in redirection to a different URL, which means the Visitable view controller’s URL would change in place. Instead, we recommend submitting forms with JavaScript using XMLHttpRequest, and using the response to tell Turbolinks where to navigate afterwards. See Redirecting After a Form Submission in the Turbolinks documentation for more details. Handling Failed RequestsTurbolinks for iOS calls the The NSError object provides details about the error. Access its An error code of An error code of func session(session: Session, didFailRequestForVisitable visitable: Visitable, withError error: NSError) {
guard let errorCode = ErrorCode(rawValue: error.code) else { return }
switch errorCode {
case .HTTPFailure:
let statusCode = error.userInfo["statusCode"] as! Int
// Display or handle the HTTP error code
case .NetworkFailure:
// Display the network failure or retry the visit
}
} HTTP error codes are a good way for the server to communicate specific requirements to your Turbolinks application. For example, you might use a See the demo app’s ApplicationController for a detailed example of how to present error messages and perform authorization. Setting Visitable TitlesBy default, Turbolinks for iOS sets your Visitable view controller’s If you want to customize the title or pull it from another element on the page, you can implement the func visitableDidRender() {
title = formatTitle(visitableView.webView?.title)
}
func formatTitle(title: String) -> String {
// ...
} Starting and Stopping the Global Network Activity IndicatorImplement the optional func sessionDidStartRequest(_ session: Session) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func sessionDidFinishRequest(_ session: Session) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
} Note that the network activity indicator is a shared resource, so your application will need to perform its own reference counting if other background operations update the indicator state. Changing How Turbolinks Opens External URLsBy default, Turbolinks for iOS opens external URLs in Safari. You can change this behavior by implementing the Session delegate’s optional For example, to open external URLs in an in-app SFSafariViewController, you might write: import SafariServices
// ...
func session(session: Session, openExternalURL URL: NSURL) {
let safariViewController = SFSafariViewController(URL: URL)
presentViewController(safariViewController, animated: true, completion: nil)
} Becoming the Web View’s Navigation DelegateYour application may require precise control over the web view’s navigation policy. If so, you can assign yourself as the WKWebView’s To assign the web view’s func sessionDidLoadWebView(_ session: Session) {
session.webView.navigationDelegate = self
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> ()) {
decisionHandler(WKNavigationActionPolicy.Cancel)
// ...
} Once you assign your own navigation delegate, Turbolinks will no longer invoke the Session delegate’s Note that your application must call the navigation delegate’s Customizing the Web View ConfigurationTurbolinks allows your application to provide a WKWebViewConfiguration when you instantiate a Session. Use this configuration to set a custom user agent, share cookies with other web views, or install custom JavaScript message handlers. let configuration = WKWebViewConfiguration()
let session = Session(webViewConfiguration: configuration) Note that changing this configuration after creating the Session has no effect. Setting a Custom User AgentSet the configuration.applicationNameForUserAgent = "MyApplication" Sharing Cookies with Other Web ViewsIf you’re using a separate web view for authentication purposes, or if your application has more than one Turbolinks Session, you can use a single WKProcessPool to share cookies across all web views. Create and retain a reference to a process pool in your application. Then configure your Turbolinks Session and any other web views you create to use this process pool. let processPool = WKProcessPool()
// ...
configuration.processPool = processPool Passing Messages from JavaScript to Your ApplicationYou can register a WKScriptMessageHandler on the configuration’s user content controller to send messages from JavaScript to your iOS application. class ScriptMessageHandler: WKScriptMessageHandler {
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
// ...
}
}
let scriptMessageHandler = ScriptMessageHandler()
configuration.userContentController.addScriptMessageHandler(scriptMessageHandler, name: "myApplication") document.addEventListener("click", function() {
webkit.messageHandlers.myApplication.postMessage("Hello!")
}) Contributing to TurbolinksTurbolinks for iOS is open-source software, freely distributable under the terms of an MIT-style license. The source code is hosted on GitHub. Development is sponsored by Basecamp. We welcome contributions in the form of bug reports, pull requests, or thoughtful discussions in the GitHub issue tracker. Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. © 2017 Basecamp, LLC |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论