产品设计要求是这样的:
对应的初步代码是这样的:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIImageView *imageView = [[UIImageView alloc] init]; imageView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 200); imageView.image = [UIImage imageNamed:@"123.jpeg"]; [self.view addSubview:imageView]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self showStoreProductInApp:@"423084029"]; } - (void)showStoreProductInApp:(NSString *)appID{ Class isAllow = NSClassFromString(@"SKStoreProductViewController"); if (isAllow != nil) { SKStoreProductViewController *sKStoreProductViewController = [[SKStoreProductViewController alloc] init]; [sKStoreProductViewController setDelegate:self]; [sKStoreProductViewController.view setFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 200)]; [self.view addSubview:sKStoreProductViewController.view]; __weak typeof(self) weakSelf = self; [sKStoreProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: appID} completionBlock:^(BOOL result, NSError *error) { __strong typeof(weakSelf) strongSelf = weakSelf; if (result) { /* // 也可以再这里实现该界面的预加载(会先出来一个连返回按钮都没有的空白页) [strongSelf.view addSubview:sKStoreProductViewController.view]; */ }else{ NSLog(@"error:%@",error); } }]; }else{ //低于iOS6的系统版本没有这个类,不支持这个功能 NSString *string = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/xxxxxxx/app/id%@?mt=8",appID]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]]; } } - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController { [viewController.view removeFromSuperview]; }
对应的初步效果是这样的:
(<--- iOS11以后)(<--- iOS11之前)