This topic describes how to implement screen sharing on iOS. The DingRTC screen sharing solution uses the iOS ReplayKit framework to share the device's screen. To enable this feature, you must add an Extension component to your app.
Note
For security reasons, screen sharing is disabled by default on mobile clients. To enable this feature, submit a ticket.
Create a Broadcast Upload Extension
In Xcode, choose File > New > Target, and then select Broadcast Upload Extension.
Select the new target, click the Capabilities tab, and then add App Groups, as shown in the following figure:

3. Enter the App Group ID, as shown in the following figure. This step requires an Apple developer certificate or a personal account. If you use different certificates for development and release, you must also register the App Group with the release certificate. For more information, see the official documentation.
Select the target for your main app, and then repeat steps 2 and 3 for the main app.
Select the new target and add DingRTC.framework to the Frameworks, Libraries, and Embedded Content section, as shown in the following figure:

6. In the new target, Xcode automatically creates a file named SampleHandler.m. Replace the contents of this file with the following code. Change kAppGroup in the code to the App Group ID that you entered previously.
#import "SampleHandler.h"
#import <DingRtc/DingRTC.h>
static NSString * _Nonnull kAppGroup = @"group.com.dingtalk.DingRTCSample";
@interface SampleHandler() <DingScreenShareExtDelegate>
@end
@implementation SampleHandler
- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
// User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
[[DingRtcScreenShareExt sharedInstance] setupWithAppGroup:kAppGroup delegate:self];
}
- (void)broadcastPaused {
// User has requested to pause the broadcast. Samples will stop being delivered.
}
- (void)broadcastResumed {
// User has requested to resume the broadcast. Samples delivery will resume.
}
- (void)broadcastFinished {
// User has requested to finish the broadcast.
}
- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
@autoreleasepool {
[[DingRtcScreenShareExt sharedInstance] sendSampleBuffer:sampleBuffer type:sampleBufferType];
}
}
#pragma mark - DingRtcScreenShareExtDelegate
- (void)finishBroadcastWithError:(DingRtcScreenShareExt *)broadcast error:(NSError *)error {
[self finishBroadcastWithError:error];
}
@end
Receive screen recording data from the Broadcast Upload Extension process in the main app
1. In your main app, call the -startScreenShare:mode: method and pass the App Group ID to start screen sharing.
[DingRTCClient.instance.rtcEngine startScreenShare:@"group.com.dingtalk.DingRTCSample" mode:DingRtcScreenShareAll];
2. Wait for the user to start screen sharing. Users typically start sharing by long-pressing the screen recording button in the iOS Control Center. Alternatively, you can use the following code in your main app, which uses RPSystemBroadcastPickerView to display a prompt for the user to confirm.
Apple added RPSystemBroadcastPickerView in iOS 12.0. This view displays a system UI within an app for the user to start screen sharing. RPSystemBroadcastPickerView does not support UI customization, and there is no official method to trigger it programmatically.
The sample code works by traversing the subviews of RPSystemBroadcastPickerView to find a UIButton and then programmatically triggering its click event.
This method is not recommended by Apple and may stop working in future system updates. Use this sample code at your own risk.
@property (strong, nonatomic) RPSystemBroadcastPickerView *broadcastPickerView;
- (void)launchBroadcastPickerView {
if (@available(iOS 12.0, *)) {
RPSystemBroadcastPickerView *pickerView = [[RPSystemBroadcastPickerView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
pickerView.showsMicrophoneButton = NO;
pickerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
NSString *pluginPath = [NSBundle mainBundle].builtInPlugInsPath;
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pluginPath error:nil];
for (NSString *content in contents) {
if (![content hasSuffix:@".appex"]) {
continue;
}
NSBundle *bundle = [NSBundle bundleWithPath:[[NSURL fileURLWithPath:pluginPath] URLByAppendingPathComponent:content].path];
if (bundle) {
NSString *identifier = [bundle.infoDictionary valueForKeyPath:@"NSExtension.NSExtensionPointIdentifier"];
if ([identifier isEqualToString:@"com.apple.broadcast-services-upload"]) {
pickerView.preferredExtension = bundle.bundleIdentifier;
}
}
}
self.broadcastPickerView = pickerView;
for (UIView *view in self.broadcastPickerView.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
[(UIButton *)view sendActionsForControlEvents:UIControlEventAllEvents];
}
}
}
}
3. You can call -stopScreenShare to stop screen sharing at any time.
[DingRTCClient.instance.rtcEngine stopScreenShare];