Loading...

共享屏幕RPSystemBroadcastPickerView无法自动消失

iOS共享屏幕需要唤起系统的RPSystemBroadcastPickerView,开始共享倒计时结束后RPSystemBroadcastPickerView无法自动消失。

问题原因

iOS共享屏幕需要依赖系统提供的Replaykit框架,开启共享时需要弹出系统的RPSystemBroadcastPickerView,由于RPSystemBroadcastPickerView受系统限制,我们无法通过removeFromSuperview、hidden等方式隐藏,所以需要采用其他方式。

解决方法

观察弹出视图的方式是presentViewController,所以我们可以通过Method Swizzling交换系统的presentViewController方法,从而拿到RPSystemBroadcastPickerView所在的ViewController,在需要的时机调用dismissViewControllerAnimated即可。此方式不支持iOS14及以上系统,如果系统是iOS14及以上,则采用Scheme方式。示例代码如下:

iOS14以下:

#import "UIViewController+XYSwizzling.h"
#import <objc/runtime.h>
#import "XYSDKManager.h"

@implementation UIViewController (XYSwizzling)
+ (void)load {
[self swizzleSelector:@selector(presentViewController:animated:completion:) withAnotherSelector:@selector(xy_presentViewController:animated:completion:)];
}

+ (void)swizzleSelector:(SEL)originalSelector withAnotherSelector:(SEL)swizzledSelector
{
Class aClass = [self class];

Method originalMethod = class_getInstanceMethod(aClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector);

BOOL didAddMethod =
class_addMethod(aClass,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
class_replaceMethod(aClass,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}

#pragma mark - Method Swizzling

- (void)xy_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
if ([NSStringFromClass(viewControllerToPresent.class) isEqualToString:@"RPBroadcastPickerStandaloneViewController"]) {
//RPSystemVC记录RPSystemBroadcastPickerView所在的vc,在需要的时机调用dismissViewControllerAnimated即可
[XYSDKManager sharedInstance].RPSystemVC = viewControllerToPresent;
[self xy_presentViewController:viewControllerToPresent animated:flag completion:completion];
} else {
[self xy_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

}

@end

iOS14及以上:

//App随便设置一个scheme,在需要的时候调用此方法即可
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"xylinksdk://"] options:@{} completionHandler:^(BOOL success) {
NSLog(@"%d", success);
}];
意见反馈