使用私有API调起AirPlay

AirPlay

在iPhone端无法显示Mirroring选项,连接后只能传输音频信号.

@property (nonatomic)UIViewController *routerController;
@property (nonatomic,strong)NSString *deviceName;
@property (strong, nonatomic) UIPopoverController *airplayPopoverController;

- (void)showAirPlay
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
 {
        MPVolumeView *volumView = [[MPVolumeView alloc] initWithFrame:CGRectMake(60, 100, 40, 40)];
        [volumView setRouteButtonImage:[UIImage imageNamed:@"class1_start"] forState:UIControlStateNormal];
        volumView.showsVolumeSlider = NO;
        weakSelf.deviceName = nil;
        [weakSelf.view addSubview:volumView];

        Class MPAVRoutingController = NSClassFromString(@"MPAVRoutingController");
        weakSelf.routerController = [[MPAVRoutingController alloc] init];
        [weakSelf.routerController setValue:weakSelf forKey:@"delegate"];
        [weakSelf.routerController setValue:[NSNumber numberWithLong:2] forKey:@"discoveryMode"];
    }
    else
    {

        if(self.airplayPopoverController == nil)
        {
            // create Class
            id TheClass = NSClassFromString(@"MPAudioVideoRoutingPopoverController");
            // create Obj
            id testObject = [[TheClass alloc] initWithContentViewController: weakSelf];
            
            //通过方法名字符串create SEL
            NSString *selectorStr = @"initWithType:includeMirroring:";
            SEL initMethod = NSSelectorFromString(selectorStr);

            if([testObject respondsToSelector: initMethod])
            {
                NSMethodSignature* signature = [TheClass instanceMethodSignatureForSelector: initMethod];
                //使用方法的签名来创建一个NSInvocation对象
                NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: signature];
                //要执行谁的(target)的哪个方法(selector)
                [invocation setTarget: testObject];
                [invocation setSelector:initMethod];

                NSInteger type = 0;
                BOOL showMirroring = YES;
                static id returnObject = nil;
                
                //使用setArgument:atIndex:方法给要执行的方法设置参数,注意下标从2开始,因为0、1已经被target与selector占用
                [invocation setArgument:&type atIndex:2];
                [invocation setArgument:&showMirroring atIndex:3];
                [invocation retainArguments];
                //执行方法
                [invocation invoke];
                //returnObject获取返回值
                [invocation getReturnValue: &returnObject];
                
                weakSelf.airplayPopoverController = returnObject;
                
            }
        }
        UIButton *button = sender;
        [weakSelf.airplayPopoverController presentPopoverFromRect:button.frame
                                                       inView:weakSelf.view
                                     permittedArrowDirections:UIPopoverArrowDirectionAny
                                                     animated:YES];
    }
}

-(void)routingControllerAvailableRoutesDidChange:(id)arg1{
if (self.deviceName == nil) {
    return;
}
NSArray *availableRoutes = [self.routerController valueForKey:@"availableRoutes"];
for (id router in availableRoutes) {
    NSString *routerName = [router valueForKey:@"routeName"];
    if ([routerName rangeOfString:self.deviceName].length >0) {
        BOOL picked = [[router valueForKey:@"picked"] boolValue];
        if (picked == NO)
        {
            #pragma clang diagnostic push
            #pragma clang diagnostic ignored "-Wundeclared-selector"
            [self.routerController performSelector:@selector(pickRoute:) withObject:router];
            #pragma clang diagnostic pop
        }
        return;
    }
}

}

说明:
私有API,使用需谨慎~demo上玩一下就好.

你可能感兴趣的:(使用私有API调起AirPlay)