插屏广告功能实现

#import <StoreKit/StoreKit.h>

//rootviewcontroller.h
@interface RootViewController : UIViewController<SKStoreProductViewControllerDelegate> {

}

+ (RootViewController*)getRootViewController;
- (void) downloadAppWithUrlString:(NSString*)urlString appId:(NSString*)appId;
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController;
@end

//rootviewcontroller.mm
- (void) downloadAppWithUrlString:(NSString*)urlString appId:(NSString*)appId{    
    if (appId && appId.length > 0) {                
        NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
        if ([systemVersion floatValue] >= 6.0){
            NSDictionary *appParameters = [NSDictionary dictionaryWithObject:appId
                                                                      forKey:SKStoreProductParameterITunesItemIdentifier];            
            SKStoreProductViewController *productViewController = [[SKStoreProductViewController alloc] init];
            [productViewController setDelegate:self];
            [productViewController loadProductWithParameters:appParameters
                                             completionBlock:^(BOOL result, NSError *error)
             {
                 if (NO == result) {
                     if (urlString && urlString.length > 0) {
                         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
                     }
                 }
             }];
            [self presentViewController:productViewController
                                animated:YES
                              completion:^{
                                  
                              }];            
            
        }
    }else{
        if (urlString && urlString.length > 0) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }
    }   
}

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
    [viewController dismissModalViewControllerAnimated:YES];
}

+ (RootViewController*)getRootViewController{
    UIViewController* rootController = nil;
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        NSArray* array=[[UIApplication sharedApplication]windows];
        UIWindow* win=[array objectAtIndex:0];
        
        UIView* ui=[[win subviews] objectAtIndex:0];
        rootController=(UIViewController*)[ui nextResponder];
    }
    else
    {
        // use this method on ios6
        rootController=[UIApplication sharedApplication].keyWindow.rootViewController;
    }
    
    return (RootViewController*)rootController;
}


 
//实现接口的.m

+ (void) downloadAppWithUrlString:(NSString*)urlString appId:(NSString*)appId{
    
    RootViewController *rootController = [ShareSDKEx getRootViewController];
    
    if (rootController) {
        if (rootController) {
            [rootController downloadAppWithUrlString:urlString appId:appId];
        }
    }else{
        if (urlString && urlString.length > 0) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }
    }
}


 
//cpp调用
    
    [UIImage downloadAppWithUrlString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=yx75d38221c9374c3089c3c9daa649a327" appId:@"yx75d38221c9374c3089c3c9daa649a327"];

ps:以上方法可以实现游戏内页面切换效果,另外一种方法虽然没有页面切换,但是相对简单,就是手动添加ccapplication.mm的openurl方法,然后直接调用即可。


//plateform->ios->ccapplication.mm
void CCApplication::openURL(const char* pszUrl)
{
    
    NSString *msg = [NSString stringWithCString:pszUrl encoding:NSASCIIStringEncoding];
    NSURL *nsUrl = [NSURL URLWithString:msg];
    [[UIApplication sharedApplication] openURL:nsUrl];
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值