//// See LICENSE folder for this template’s licensing information.//// Abstract:// An auxiliary source file which is part of the book-level auxiliary sources.// Provides the implementation of the "always-on" live view.//import UIKit
import PlaygroundSupport
@objc(Book_Sources_LiveViewController)
publicclassLiveViewController: UIViewController, PlaygroundLiveViewMessageHandler, PlaygroundLiveViewSafeAreaContainer{
/*
public func liveViewMessageConnectionOpened() {
// Implement this method to be notified when the live view message connection is opened.
// The connection will be opened when the process running Contents.swift starts running and listening for messages.
}
*//*
public func liveViewMessageConnectionClosed() {
// Implement this method to be notified when the live view message connection is closed.
// The connection will be closed when the process running Contents.swift exits and is no longer listening for messages.
// This happens when the user's code naturally finishes running, if the user presses Stop, or if there is a crash.
}
*/publicfuncreceive(_ message: PlaygroundValue) {
// Implement this method to receive messages sent from the process running Contents.swift.// This method is *required* by the PlaygroundLiveViewMessageHandler protocol.// Use this method to decode any messages sent as PlaygroundValue values and respond accordingly.
}
}
这里需要注意的是 Swift 类被起了个别名
@objc(Book_Sources_LiveViewController) 暴露给 Objective-C。
LiveViewSupport.swift
//// See LICENSE folder for this template’s licensing information.//// Abstract:// Provides supporting functions for setting up a live view.//import UIKit
import PlaygroundSupport
/// Instantiates a new instance of a live view.////// By default, this loads an instance of `LiveViewController` from `LiveView.storyboard`.publicfuncinstantiateLiveView() -> PlaygroundLiveViewable {
let storyboard = UIStoryboard(name: "LiveView", bundle: nil)
guardlet viewController = storyboard.instantiateInitialViewController() else {
fatalError("LiveView.storyboard does not have an initial scene; please set one or update this function")
}
guardlet liveViewController = viewController as? LiveViewControllerelse {
fatalError("LiveView.storyboard's initial scene is not a LiveViewController; please either update the storyboard or this function")
}
return liveViewController
}
// Abstract:// Instantiates a live view and passes it to the PlaygroundSupport framework.//import UIKit
import PlaygroundSupport
// Instantiate a new instance of the live view from the book's auxiliary sources and pass it to PlaygroundSupport.PlaygroundPage.current.liveView = instantiateLiveView()
这里可能有个疑惑:instantiateLiveView 在 Pages 是怎么识别的?
原因是:Sources 文件夹中的源文件属于 Global files,并且该方法还是 public
import UIKit
import PlaygroundSupport
import LiveViewHost
import Book_Sources
@UIApplicationMainclassAppDelegate: LiveViewHost.AppDelegate{
overridefuncsetUpLiveView() -> PlaygroundLiveViewable {
// This method should return a fully-configured live view. This method must be implemented.//// The view or view controller returned from this method will be automatically be shown on screen,// as if it were a live view in Swift Playgrounds. You can control how the live view is shown by// changing the implementation of the `liveViewConfiguration` property below.returnBook_Sources.instantiateLiveView()
}
overridevar liveViewConfiguration: LiveViewConfiguration {
// Make this property return the configuration of the live view which you desire to test.//// Valid values are `.fullScreen`, which simulates when the user has expanded the live// view to fill the full screen in Swift Playgrounds, and `.sideBySide`, which simulates when// the live view is shown next to or above the source code editor in Swift Playgrounds.return .fullScreen
}
}