Skip to content

Adds an object that captures the screen using ReplayKit. #612

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
75 changes: 75 additions & 0 deletions C4/UI/ScreenRecorder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2016 C4
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

import ReplayKit

public class ScreenRecorder: NSObject, RPPreviewViewControllerDelegate {
public typealias PreviewControllerFinishedAction = (activities: Set<String>?) -> ()
public typealias RecorderStoppedAction = () -> ()

var recorder = RPScreenRecorder.sharedRecorder()
var preview: RPPreviewViewController?
var activities: Set<String>?

public var previewFinishedAction: PreviewControllerFinishedAction?
public var recordingEndedAction: RecorderStoppedAction?
public var enableMicrophone = false

public func start() {
preview = nil
recorder.startRecordingWithMicrophoneEnabled(enableMicrophone) { error in
if error != nil {
print("Start Recording Error: \(error?.localizedDescription)")
}
}
}

public func start(duration: Double) {
start()
delay(duration) {
self.stop()
}
}

public func stop() {
recorder.stopRecordingWithHandler { previewViewController, error in
self.preview = previewViewController
self.preview?.previewControllerDelegate = self
self.recordingEndedAction?()
}
}

public func showPreviewInController(controller: UIViewController) {
guard let p = preview else {
print("Recorder has no preview to show.")
return
}

controller.presentViewController(p, animated: true, completion: nil)
}

public func previewController(previewController: RPPreviewViewController, didFinishWithActivityTypes activityTypes: Set<String>) {
activities = activityTypes
}

public func previewControllerDidFinish(previewController: RPPreviewViewController) {
previewFinishedAction?(activities: activities)
preview?.parentViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
5 changes: 5 additions & 0 deletions C4App/Images.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
Expand Down
1 change: 0 additions & 1 deletion C4App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ import UIKit

class ViewController: CanvasController {
override func setup() {

}
}
4 changes: 4 additions & 0 deletions C4iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
1F754DCB1C48EE600036D39F /* ImageLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F754DCA1C48EE600036D39F /* ImageLayer.swift */; };
1F75FE951C5CADBF00EB62C2 /* ScreenRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F75FE941C5CADBF00EB62C2 /* ScreenRecorder.swift */; };
1F8252CC1BB3C65E0090E98A /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8252CB1BB3C65E0090E98A /* Timer.swift */; };
1F8A2FEB1C4C24FD0087063A /* View+Render.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8A2FEA1C4C24FD0087063A /* View+Render.swift */; };
1FAB35E81C5372CE00620741 /* Pixel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FAB35E71C5372CE00620741 /* Pixel.swift */; };
Expand Down Expand Up @@ -129,6 +130,7 @@

/* Begin PBXFileReference section */
1F754DCA1C48EE600036D39F /* ImageLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageLayer.swift; sourceTree = "<group>"; };
1F75FE941C5CADBF00EB62C2 /* ScreenRecorder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScreenRecorder.swift; sourceTree = "<group>"; };
1F8252CB1BB3C65E0090E98A /* Timer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = "<group>"; };
1F8A2FEA1C4C24FD0087063A /* View+Render.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+Render.swift"; sourceTree = "<group>"; };
1F8CB1D91A05781700A20783 /* RectTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RectTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -380,6 +382,7 @@
A9D4F6AB1B534F9F00F937AB /* UIViewController+C4View.swift */,
A9BC57261C50ABA80068BBA3 /* UIImage+Color.swift */,
1FAB35E71C5372CE00620741 /* Pixel.swift */,
1F75FE941C5CADBF00EB62C2 /* ScreenRecorder.swift */,
);
path = UI;
sourceTree = "<group>";
Expand Down Expand Up @@ -630,6 +633,7 @@
A9D4F6DB1B534F9F00F937AB /* Checkerboard.swift in Sources */,
A9D4F6BB1B534F9F00F937AB /* Arc.swift in Sources */,
A9D4F6D91B534F9F00F937AB /* Wedge.swift in Sources */,
1F75FE951C5CADBF00EB62C2 /* ScreenRecorder.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down