
# 屏幕共享
#### 功能描述
基于苹果的Replaykit方案，支持用户分享整个系统的屏幕内容，但需要App额外提供一个Extension扩展组件，可实现跨应用屏幕共享。
#### 创建App Group
由于华为云SparkRTC是通过App Goup进行进程间的数据通信的，所以需要创建一个App Group。
使用您的APP ID账号登录<https://developer.apple.com/> ，进行证书配置App Group。
**注意**：完成后需要重新下载对应的Provisioning Profile。
1. 单击"Certificates, Identifiers \& Profiles"，进入"Certificates，Identifiers \&Profiles"界面。
2. 在右侧界面中选择"Identifiers"，然后单击![](https://support.huaweicloud.com/bestpractice-rtc/zh-cn_image_0000001195054056.png)。
   ![](https://support.huaweicloud.com/bestpractice-rtc/zh-cn_image_0000001239695899.png)
   
3. 选择"App Groups \> Continue"。
4. 在弹出的表单中填写"Description"和"Identifier", 其中，Identifier需要传入接口中对应的App Group参数。
5. 填写完成后，单击"Continue"。
6. 返回"Identifiers"页面，在右上角的菜单中选择"App IDs"，然后单击您的App ID（主App与Extension的AppID需要进行同样的配置）。
7. 选中"App Groups"并单击"Edit"。 ![](https://support.huaweicloud.com/bestpractice-rtc/zh-cn_image_0000001239256031.png)
   
8. 在弹出的表单中选择您之前创建的App Group，单击"Continue"返回编辑页，再单击"Save"保存。 ![](https://support.huaweicloud.com/bestpractice-rtc/zh-cn_image_0000001239256041.png)
   
9. 重新下载Provisioning Profile并配置到XCode中。 **注意**：Extension证书也需要支持App Group。
   
 
#### 创建Broadcast Upload Extension
1. 在Xcode菜单中选择"File \> New \> Target... \> Broadcast Upload Extension"。
2. 在弹出的对话框中填写相关信息，不用勾选"Include UI Extension"，然后单击"Finish"完成创建。 bundle identifier规则需要遵循主App的bundle identigfier后面拼接上**.xxxx**。
   
3. 将下载的SDK包中的**HWRtcEngineReplayKit.framework**拖动到工程中，勾选刚刚新创建的Target。
4. 选中新增加的Target，单击"+ Capability"，双击"App Groups"，并选择您创建的AppGroup。 如下图所示，操作完成后，会在文件列表中生成一个名为*Target名.entitlements*的文件。
   ![](https://support.huaweicloud.com/bestpractice-rtc/zh-cn_image_0000001239579755.png)
   
5. 选中主App的Target，并按照上述步骤对主App的Target做同样的处理。
6. 在新创建的Target中，Xcode会自动创建一个名为"SampleHandler.h" 的文件，请您用如下代码进行替换。
   **注意** ：需将代码中的APPGROUP改为上文中的创建的App Group Identifier。
   ```
   #import "SampleHandler.h"
   #import <ReplayKit/ReplayKit.h>
   #import <HWRtcEngineReplayKit/HWRtcReplay.h>
   #define HWReplayAppGroupId    @"group.com.huawei.HwRTCLiveDemo"
   @interface SampleHandler() < HWRtcReplayDelegate >
   @property (nonatomic, assign)BOOL connectRet;
   @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.
   [[HWRtcReplay sharedInstance] setupWithAppGroup:HWReplayAppGroupId 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.
   [[HWRtcReplay sharedInstance] broadcastFinished];
   }
   -(void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType : (RPSampleBufferType)sampleBufferType {
   switch (sampleBufferType) {
   case RPSampleBufferTypeVideo:
   // Handle video sample buffer
   [[HWRtcReplay sharedInstance] sendVideoSampleBuffer:sampleBuffer];
   break;
   case RPSampleBufferTypeAudioApp:
   // Handle audio sample buffer for app audio
   break;
   case RPSampleBufferTypeAudioMic:
   // Handle audio sample buffer for mic audio
   break;
   default:
   break;
   }
   }
   -(void)replayBroadcastFinished {
   NSError *error;
   [self finishBroadcastWithError : error];
   }
   ```
   
 
#### 主APP接收逻辑
您可以按照如下步骤启动屏幕共享。
1. 调用startScreenShareWithAppGroup:方法，并传入自定义的AppGroup，SDK会进入等待状态。
2. 等待用户触发屏幕分享 ，开始正式屏幕共享。 屏幕共享需要您在iOS系统的控制中心，通过长按录屏按钮进行触发。
   ![](https://support.huaweicloud.com/bestpractice-rtc/zh-cn_image_0000001196708510.png)
   
3. 通过调用stopScreenShare接口结束屏幕共享。
 
