#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];
}