美文网首页
iOS AppStore封装

iOS AppStore封装

作者: 和女神经常玩 | 来源:发表于2022-12-14 22:36 被阅读0次

    接口部分

    @interface APPStoreManager : NSObject
    
    @property (retain,nonatomic) NSString *appId;
    
    
    + (APPStoreManager *)shared;
    
    //跳转App Store
    -(void)gotoAppstore;
    //跳转App Store评分(可深度跳转)
    -(void)gotoAppstoreGradeIsDeep:(BOOL)isDeep;
    //应用内弹出评分界面
    -(void)presentInnerStoreInVc:(UIViewController *)vc;
    //应用内弹出下载界面
    -(void)presentInnerGrade;
    //获取App Store中的版本号
    -(void)getAppStoreVersionWithBlock:(void(^)(NSString *version)) block;
    
    @end
    

    实现部分

    @interface APPStoreManager ()<SKStoreProductViewControllerDelegate>
    
    @property (retain,nonatomic) NSString *appStoreStr;
    @property (retain,nonatomic) NSString *appStoreGradeStr;
    
    @end
    
    
    @implementation APPStoreManager
    
    static APPStoreManager *appStoreManager = nil;
    
    + (APPStoreManager *)shared
    {
        @synchronized (self) {
            if (appStoreManager == nil) {
                appStoreManager = [[super alloc] init];
            }
        }
        return appStoreManager;
    }
    
    + (instancetype)allocWithZone:(struct _NSZone *)zone
    {
        @synchronized (self) {
            if (appStoreManager == nil) {
                appStoreManager = [super allocWithZone:zone];
                return appStoreManager;
            }
        }
        return appStoreManager;
    }
    
    - (id)copy
    {
        return self;
    }
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            self.appStoreStr = @"itms-apps://itunes.apple.com/app/id";
            self.appStoreGradeStr = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=";
        }
        return self;
    }
    
    -(void)gotoAppstore
    {
        NSString *urlString = [NSString stringWithFormat:@"%@%@",self.appStoreStr,self.appId];
        NSURL *url = [NSURL URLWithString:urlString];
        if (@available(iOS 10, *)) {
            [UIApplication.sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {}];
        }
        else
        {
            [UIApplication.sharedApplication openURL:url];
        }
    }
    -(void)gotoAppstoreGradeIsDeep:(BOOL)isDeep
    {
        NSString *urlString = nil;
        if (isDeep) {
            urlString = [NSString stringWithFormat:@"%@%@?action=write-review",self.appStoreStr,self.appId];
        }
        else
        {
            urlString = [NSString stringWithFormat:@"%@%@",self.appStoreGradeStr,self.appId];
        }
        NSURL *url = [NSURL URLWithString:urlString];
        if (@available(iOS 10, *)) {
            [UIApplication.sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {}];
        }
        else
        {
            [UIApplication.sharedApplication openURL:url];
        }
    }
    
    -(void)presentInnerStoreInVc:(UIViewController *)vc
    {
        SKStoreProductViewController * storeProductVC = [[SKStoreProductViewController alloc] init];
        storeProductVC.delegate = self;
        [storeProductVC loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier : [NSNumber numberWithInt:self.appId.intValue]} completionBlock:^(BOOL result, NSError * _Nullable error) {
            [vc presentViewController:storeProductVC animated:YES completion:^{}];
        }];
    }
    
    -(void)presentInnerGrade
    {
        if([SKStoreReviewController respondsToSelector:@selector(requestReview)]){
            [SKStoreReviewController requestReview];
        }else{
            [self gotoAppstoreGradeIsDeep:YES];
        }
    }
    
    -(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
    {
        [viewController dismissViewControllerAnimated:YES completion:^{}];
    }
    
    
    - (void)getAppStoreVersionWithBlock:(void(^)(NSString *version)) block {
        AFHTTPSessionManager *manager  = [AFHTTPSessionManager manager];
        manager.requestSerializer =[AFHTTPRequestSerializer serializer];
        manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",@"text/javascript",nil];
        NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",self.appId];
        [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        } progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSArray *array = nil;
            NSDictionary *dic = nil;
            NSString *appStoreVersion = nil;
            if (responseObject) {
                array = responseObject[@"results"];
            }
            if (array) {
                dic = array[0];
            }
            if (dic) {
                appStoreVersion = dic[@"version"];
            }
            if (block) {
                block(appStoreVersion);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (block) {
                block(nil);
            }
        }];
    }
    
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS AppStore封装

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