如何在iOS 11控制中心录制/广播栏添加应用(如TeamViewer)?
Hey there! Let's tackle your two questions about integrating apps with iOS 11's Control Center recording and broadcast features, including how to mirror TeamViewer's setup.
iOS 11 introduced ReplayKit 2, which lets apps integrate with Control Center's screen recording and broadcast workflows. Here's how to get your app listed there:
Key Setup for Control Center Integration
To appear in the Control Center's screen recording menu (the list that pops up when you long-press the screen recording button), you need two extensions:
Step 1: Add Broadcast Extensions to Your Project
In Xcode:
- Go to File > New > Target
- Search for Broadcast Upload Extension and add it (this handles capturing and processing screen/audio data)
- Add a Broadcast Setup UI Extension next (this shows a custom setup screen when users select your app from Control Center)
- Make sure both extensions use the same team signing as your main app, and set unique bundle identifiers (e.g.,
com.yourapp.BroadcastUploadandcom.yourapp.BroadcastSetupUI)
Step 2: Customize the Setup UI
The setup extension's view controller lets you add a custom interface for users to confirm settings (like enabling audio or linking to a remote device). Here's a basic example:
import UIKit import ReplayKit class BroadcastSetupViewController: UIViewController, RPBroadcastViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() // Add your custom UI elements here (e.g., a "Start Broadcast" button) } // Called when the user finishes setup func broadcastViewController(_ broadcastViewController: RPBroadcastViewController, didFinishWithBroadcastURL broadcastURL: URL?, error: Error?) { broadcastViewController.dismissBroadcastViewController(completionHandler: nil) } }
Step 3: Handle Broadcast Data in the Upload Extension
The upload extension's SampleHandler class processes the screen and audio data during the broadcast. For basic recording, you could save data to a file; for streaming, you'd send it over the network:
import ReplayKit class SampleHandler: RPBroadcastSampleHandler { override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) { // Initialize your recording/streaming logic here } override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) { switch sampleBufferType { case .video: // Process video frames (e.g., encode to H.264) break case .audioApp, .audioMic: // Process audio data (e.g., encode to AAC) break @unknown default: break } } override func broadcastFinished() { // Clean up resources when the broadcast ends } }
TeamViewer lets users start a remote screen share directly from Control Center by combining ReplayKit's broadcast capabilities with network streaming. Here's how to mirror this:
Core Workflow
TeamViewer uses the broadcast extension to capture the iOS screen, encodes the video/audio data, and streams it to a remote device over the network. The main app handles device pairing and connection management.
Step-by-Step Implementation
- Set Up Broadcast Extensions (follow the steps from question 1) – this gets your app into Control Center's menu.
- Add Device Pairing in the Main App
Implement a way for users to link their iOS device to a remote device (e.g., QR code scanning, PIN entry, or local network discovery). Store the connection details so the broadcast extension can access them. - Stream Data from the Upload Extension
In theSampleHandler, instead of saving data to a file, encode theCMSampleBufferinto a streamable format (H.264 for video, AAC for audio) and send it to the paired remote device via WebSocket, RTSP, or a custom network protocol. Example snippet:override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) { guard sampleBufferType == .video, let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } // Use AVFoundation to encode imageBuffer to H.264 // Send the encoded data over your established network connection } - Customize the Setup UI
Use the Broadcast Setup UI Extension to let users confirm the remote connection (e.g., show the paired device name, ask for a confirmation tap) before starting the broadcast, just like TeamViewer does.
Important Considerations
- Background Stability: Broadcast extensions run in the background, so optimize your network code to handle intermittent connectivity.
- Performance: Encoding screen data is CPU-heavy – use AVFoundation's hardware-accelerated encoders to avoid lag.
- Permissions: Request microphone access (if needed) and network permissions in both the main app and extensions.
内容的提问来源于stack exchange,提问作者thatmarcel

