iOS开发 - appstore评价与app内展示appstore

iOS开发 - appstore评价与app内展示appstore

之前开发中遇到app内通过广告直接展示某个app的appstore信息页面。StoreKit提供了SKStoreProductViewController可以app中直接访问app store 而不需要跳转到app store

一、代码实现

#import "SDStoreProductManager.h"
#import <StoreKit/StoreKit.h>

static SDStoreProductManager *shareInstance = nil;

@interface SDStoreProductManager ()<SKStoreProductViewControllerDelegate>

@property (nonatomic, weak) UIViewController *controller;

@end

@implementation SDStoreProductManager

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[SDStoreProductManager alloc] init];
    });
    return shareInstance;
}

/**
 有次数限制,一年只能使用三次
 使用次数超限后,需要跳转appstore
 */
- (void)appstoreSystemComent {
    if (@available(iOS 10.3, *)) {
        if([SKStoreReviewController respondsToSelector:@selector(requestReview)]) {// iOS 10.3 以上支持
            //防止键盘遮挡
            [[UIApplication sharedApplication].keyWindow endEditing:YES];
            [SKStoreReviewController requestReview];
        }
    } else {
        // Fallback on earlier versions
    }
}

/**
 可评分评论,打开appstore
 */
- (void)openAppStoreComment:(NSString *)appID {
    if (!(appID && [appID isKindOfClass:[NSString class]] && appID.length > 0)) {
        return;
    }
    NSString *urlString = [NSString  stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review",appID];//替换为对应的APPID
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}

/**
 app内部加载评价
 */
- (void)inAppOpenAppStore:(NSString *)appID controller:(UIViewController *)controller {
    if (!(appID && [appID isKindOfClass:[NSString class]] && appID.length > 0)) {
        return;
    }
    
    if (!(controller && [controller isKindOfClass:[UIViewController class]])) {
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    weakSelf.controller = controller;

    SKStoreProductViewController *storeProductViewContorller = [[SKStoreProductViewController alloc] init];
    storeProductViewContorller.delegate = self;
    //加载App Store视图展示
    [storeProductViewContorller loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:appID} completionBlock:^(BOOL result, NSError *error) {
         if(error) {
         } else {
             //模态弹出appstore
             [weakSelf.controller presentViewController:storeProductViewContorller animated:YES completion:^{
                  
             }];
         }
     }];
}

// 代理方法
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [self.controller dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end

二、小结

iOS开发 - appstore评价与app内展示appstore,使用StoreKit之SKStoreProductViewControl展示appstore某个应用的appstore页面。

学习记录,每天不停进步。

你可能感兴趣的:(iphone开发,移动开发,Objective-c,ios,cocoa,macos)