Updated on 2024-07-30 GMT+08:00

Screen Sharing Integration

You can share your mobile phone screen with others to improve communication efficiency.

About the Sharing Bundle ID

Bundle ID format of the ScreenShareExtension extension:

Main app bundle ID + ScreenShareExtension

Bundle ID format of the ScreenShareExtensionSetupUI extension:

Main app bundle ID + ScreenShareExtensionSetupUI

Examples:
Assume that the bundle ID of the main app is com.xx.xx.
The bundle ID of the ScreenShareExtension extension will be com.xx.xx.ScreenShareExtension.
The bundle ID of the ScreenShareExtensionSetupUI extension will be com.xx.xx.ScreenShareExtensionSetupUI.

You need to add an iOS extension and configure related code for iOS screen sharing. The following describes how to integrate the SDK to develop the screen sharing extension.

Related features depend on system capabilities. To use the screen sharing function, you need to use iOS 12 or a later version. For more information, see App Extension Programming Guide.

  1. Choose File > New > Target. Then, select the Broadcast Upload Extension template to create an extension target.

    Figure 1 Adding a target
    Figure 2 Selecting the Broadcast Upload Extension template and clicking Next
    Figure 3 Extension named ScreenShareExtension added

  2. Set the iOS version supported by the extension to 12.0.

    Figure 4 Setting the extension
    Figure 5 Setting the extension UI

  3. Add an app group to the main project and current extension, and set the group ID to that obtained in Preparations.

    Figure 6 Default setting (no app groups)
    Figure 7 Choosing TARGETS > Signing & Capabilities > Capability to add an app group
    Figure 8 Choosing App Groups

    Add app groups for ScreenShareExtension and ScreenShareExtensionSetupUI in the same way.

    Figure 9 App groups added

  4. Add app group IDs to the entitlement files. (The group IDs are also used during initialization. You can apply for app group IDs from Apple's official website.)

    Figure 10 App group ID added to an app
    Figure 11 App group ID added to ScreenShareExtension
    Figure 12 App group ID added to ScreenShareExtensionSetupUI

  5. Add HWMExtension.framework to ScreenShareExtension. Go to TARGETS > ScreenShareExtension > Build Settings > Other Linker Flags and add -lc++.

    Figure 13 Choosing ScreenShareExtension > Add Files to...
    Figure 14 Adding HWMExtension.framework to ScreenShareExtension
    Figure 15 Effect after HWMExtension.framework is added to ScreenShareExtension

  6. Configure the screen sharing code. Specifically, add the following code to the SampleHandler.m file of ScreenShareExtension.

    Figure 16 SampleHandler location
    The file can be directly used after the group ID is modified.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    #import "SampleHandler.h"
    #import <HWMExtension/HWMExtension.h>
     
     @interface SampleHandler()<ScreenShareManagerDelegate>
     @property (strong, nonatomic) HWMExensionDataManager *screenShareManager;
     @end
     
     @implementation SampleHandler
     
     - (instancetype)init {
         if (self = [super init]) {
    //         [HWMExtensionAppGroup setAppGroup:@"group.com.huawei.xxx"];// Replace the corresponding value with the app group you obtain.
            self.screenShareManager = [[HWMExensionDataManager alloc] init];
            self.screenShareManager.delegate = self;
         }
         return self;
     }
     
    - (void)dealloc {
        self.screenShareManager = nil;
    }
     
    - (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
        // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
        [self.screenShareManager broadcastStartedWithSetupInfo:setupInfo];
    }
     
    - (void)broadcastPaused {
        // User has requested to pause the broadcast. Samples will stop being delivered.
        [self.screenShareManager broadcastPaused];
    }
     
    - (void)broadcastResumed {
        // User has requested to resume the broadcast. Samples delivery will resume.
        [self.screenShareManager broadcastResumed];
    }
     
    - (void)broadcastFinished {
        // User has requested to finish the broadcast.
        [self.screenShareManager broadcastFinished];
    }
     
    - (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
        switch (sampleBufferType) {
            case RPSampleBufferTypeVideo:
                // Handle video sample buffer
                [self.screenShareManager processSampleBuffer:sampleBuffer withType:RPSampleBufferTypeVideo];  
                break;
            case RPSampleBufferTypeAudioApp:
                // Handle audio sample buffer for app audio
                // Audio sharing-related
                if ([self currentIsAudioShare]) {
                    [self.screenShareManager processSampleBuffer:sampleBuffer withType:RPSampleBufferTypeAudioApp];
                }
                break;
            case RPSampleBufferTypeAudioMic:
                // Handle audio sample buffer for mic audio
                break;
                
            default:
                break;
        }
    }
     
    - (void)screenShareManagerFinishBroadcastWithError:(NSError *)error {
        [self.screenShareManager broadcastFinished];
        [self finishBroadcastWithError: error];
    }
    - (BOOL)currentIsAudioShare {
        NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:[HWMExtensionAppGroup getAppGroup]];
        NSString *audioShareType = [userDefault objectForKey:kHWMExtensionAudioShareKey];
        if ([audioShareType isEqualToString:kHWMExtensionAudioShareValue]) {
            return YES;
        }
        return NO;
    }
     @end