美文网首页
iOS弹出半截AppStore广告

iOS弹出半截AppStore广告

作者: 爱恨的潮汐 | 来源:发表于2020-05-08 10:29 被阅读0次

iOS弹出半截AppStore广告,播放器ZFPlayer

SKStoreProductViewController

//
//  ZFNoramlViewController.m
//  ZFPlayer
//
//  Created by 紫枫 on 2018/3/21.
//  Copyright © 2018年 紫枫. All rights reserved.
//
//视频播放器ZFPlayer
#import "HSVideoAdVC.h"
#import <ZFPlayer/ZFPlayer.h>
#import <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFPlayerControlView.h>
#import "UIImageView+ZFCache.h"
#import "ZFUtilities.h"

//内部打开app需要导入头文件
#import<StoreKit/StoreKit.h>
@interface HSVideoAdVC ()<SKStoreProductViewControllerDelegate>
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;

@property (nonatomic, strong) UIButton *playBtn;


//头部内容层
@property (nonatomic, strong) UIView *headerView;

//头部高度
@property (nonatomic, assign) CGFloat headerHeight;

//视频封面
@property (nonatomic, copy) NSString *videoCoverImageUrl;
//视频地址
@property (nonatomic, copy) NSString *videoUrl;

@end

@implementation HSVideoAdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //头高
    self.headerHeight = MC_StatusBarHeight+SCREEN_WIDTH*9/16;
    //视频封面
    self.videoCoverImageUrl = @"https://img.haomeiwen.com/i635942/14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
    //视频播放连接
    self.videoUrl = @"https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4";
    
    //打开App Store
    [self openAppStoreWithAppId:AppstoreId];
    
}
#pragma mark ---------打开App Store开始-----------------
///App内部打开AppStore,传入对应App的AppId
- (void)openAppStoreWithAppId:(NSString*)appId {
    SKStoreProductViewController*storeProductVC =[[SKStoreProductViewController alloc] init];
    storeProductVC.delegate=self;
    NSDictionary*dict = [NSDictionary dictionaryWithObject:appId forKey:SKStoreProductParameterITunesItemIdentifier];
    [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result,NSError*_Nullableerror) {
        if(result) {
            [self presentViewController:storeProductVC animated:YES completion:^{
                //处理模态出半截屏幕AppStore应用
                for(int i =0; i < storeProductVC.view.subviews.count; i++) {
                    UIView *subView = storeProductVC.view.subviews[I];
                    subView.frame = CGRectMake(0, self.headerHeight-30, SCREEN_WIDTH, SCREEN_HEIGHT-(self.headerHeight-30));
                }
                [self createVideoPlay];
            }];
        }
    }];
}
#pragma mark -点击关闭按钮
-(void)productViewControllerDidFinish:(SKStoreProductViewController*)viewController{
    NSLog(@"关闭界面");
    [viewController dismissViewControllerAnimated:YES completion:^{
        [self.player stop];//停止播放
        [self.headerView removeFromSuperview];//移除头部
        //返回
        [self.navigationController popViewControllerAnimated:NO];
    }];
}
#pragma mark ---------打开App Store结束-----------------

//创建视频播放器
- (void)createVideoPlay{
    //创建视频父视图
    self.headerView = [[UIView alloc]init];
    self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.headerHeight);
    [[UIApplication sharedApplication].keyWindow addSubview:self.headerView];
    
    //添加视频播放器到rootViewController
    [self.headerView addSubview:self.containerView];
    [self.containerView addSubview:self.playBtn];//播放按钮
    
    //播放器
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
    self.player.controlView = self.controlView;
    /// 设置退到后台继续播放
    self.player.pauseWhenAppResignActive = NO;
    
    @weakify(self)
    self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        @strongify(self)
        [self setNeedsStatusBarAppearanceUpdate];
    };
    
    /// 播放完成
    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        @strongify(self)
        //重播
        [self.player.currentPlayerManager replay];
//        [self.player playTheNext];
//        [self.player stop];
    };
    //设置播放连接
    self.player.assetURL = [NSURL URLWithString:self.videoUrl];
    
    //开始自动播放
    [self playClick];
}
//视频层
- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = YES;
    }
    return _controlView;
}

//视频封面
- (UIImageView *)containerView {
    if (!_containerView) {
        _containerView = [UIImageView new];
        [_containerView setImageWithURLString:self.videoCoverImageUrl placeholder:[ZFUtilities imageWithColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] size:CGSizeMake(1, 1)]];
    }
    return _containerView;
}
//播放按钮
- (UIButton *)playBtn {
    if (!_playBtn) {
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playBtn setImage:[UIImage imageNamed:@"帖子-视频播放"] forState:UIControlStateNormal];
        [_playBtn addTarget:self action:@selector(playClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}
//点击播放按钮
- (void)playClick{
    [self.player playTheIndex:0];
    [self.controlView showTitle:@"" coverURLString:self.videoCoverImageUrl fullScreenMode:ZFFullScreenModeAutomatic];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.player.viewControllerDisappear = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.player.viewControllerDisappear = YES;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    //视频内容坐标
    CGFloat x = 0;
    CGFloat y = MC_StatusBarHeight;
    CGFloat w = SCREEN_WIDTH;
    CGFloat h = w*9/16;
    self.containerView.frame = CGRectMake(x, y, w, h);
    
    //播放按钮坐标
    w = 44;
    h = w;
    x = (CGRectGetWidth(self.containerView.frame)-w)/2;
    y = (CGRectGetHeight(self.containerView.frame)-h)/2;
    self.playBtn.frame = CGRectMake(x, y, w, h);
    
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.player.isFullScreen) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return self.player.isStatusBarHidden;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationSlide;
}

- (BOOL)shouldAutorotate {
    return self.player.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

@end

如图效果:


WechatIMG9.jpeg

相关文章

网友评论

      本文标题:iOS弹出半截AppStore广告

      本文链接:https://www.haomeiwen.com/subject/labxnhtx.html