"由於我自己的App下載量少,評論也少,出於App的aso優化,想盡辦法,而評論是aso裡邊比較重視的一塊,前面的版本都沒有誘導用戶評論的這一功能,導致有些被動。"
由此自己簡單的封裝了該功能,下面我們先看看效果圖:
彈出試圖並沒有做什麼處理,就是系統的8.0以前用的UIAlertView
8.0以上用的UIAlertController
具體的一些算法,都可以看代碼,閒話不多說,直接貼碼
新建一個NSObject的類命名為LBToAppStore
具體代碼如下
#import #import @interface LBToAppStore : NSObject{ #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0 UIAlertView *alertViewTest; #else UIAlertController *alertController; #endif } @property (nonatomic,strong) NSString * myAppID;//appID - (void)showGotoAppStore:(UIViewController *)VC; @end
#import "LBToAppStore.h" @implementation LBToAppStore - (void)showGotoAppStore:(UIViewController *)VC{ //當前版本號 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue]; //userDefaults裡的天數 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; int udtheDays = [[userDefaults objectForKey:@"theDays"] intValue]; //userDefaults裡的版本號 float udAppVersion = [[userDefaults objectForKey:@"appVersion"] intValue]; //userDefaults裡用戶上次的選項 int udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue]; //時間戳的天數 NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]; int daySeconds = 24 * 60 * 60; NSInteger theDays = interval / daySeconds; //版本升級之後的處理,全部規則清空,開始彈窗 if (udAppVersion && appVersion>udAppVersion) { [userDefaults removeObjectForKey:@"theDays"]; [userDefaults removeObjectForKey:@"appVersion"]; [userDefaults removeObjectForKey:@"userOptChoose"]; [self alertUserCommentView:VC]; } //1,從來沒彈出過的 //2,用戶選擇????我要吐槽,7天之後再彈出 //3,用戶選擇????殘忍拒絕後,7天內,每過1天會彈一次 //4,用戶選擇????殘忍拒絕的30天後,才會彈出 else if (!udUserChoose || (udUserChoose==2 && theDays-udtheDays>7) || (udUserChoose>=3 && theDays-udtheDaysudUserChoose-3) || (udUserChoose>=3 && theDays-udtheDays>30)) { [self alertUserCommentView:VC]; } } -(void)alertUserCommentView:(UIViewController *)VC{ if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; //當前時間戳的天數 NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]; int daySeconds = 24 * 60 * 60; NSInteger theDays = interval / daySeconds; //當前版本號 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue]; //userDefaults裡版本號 float udAppVersion = [[userDefaults objectForKey:@"appVersion"] intValue]; //userDefaults裡用戶選擇項目 int udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue]; //userDefaults裡用戶天數 int udtheDays = [[userDefaults objectForKey:@"theDays"] intValue]; //當前版本比userDefaults裡版本號高 if (appVersion>udAppVersion) { [userDefaults setObject:[NSString stringWithFormat:@"%f",appVersion] forKey:@"appVersion"]; } alertController = [UIAlertController alertControllerWithTitle:@"致開發者的一封信" message:@"有了您的支持才能更好的為您服務,提供更加優質的,更加適合您的App,當然您也可以直接反饋問題給到我們" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *refuseAction = [UIAlertAction actionWithTitle:@"????殘忍拒絕" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { [userDefaults setObject:@"1" forKey:@"userOptChoose"]; [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"]; }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"????好評贊賞" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { [userDefaults setObject:@"2" forKey:@"userOptChoose"]; [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"]; NSString *str = [NSString stringWithFormat: @"https://itunes.apple.com/cn/app/id%@?mt=8", self.myAppID ]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }]; UIAlertAction *showAction = [UIAlertAction actionWithTitle:@"????我要吐槽" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { if (udUserChoose30) { [userDefaults setObject:@"3" forKey:@"userOptChoose"]; [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"]; }else{ [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)(theDays-udtheDays+3)] forKey:@"userOptChoose"]; } NSString *str = [NSString stringWithFormat: @"https://itunes.apple.com/cn/app/id%@?mt=8", self.myAppID ]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }]; [alertController addAction:refuseAction]; [alertController addAction:okAction]; [alertController addAction:showAction]; // NSLog(@"%@",[userDefaults objectForKey:@"appVersion"]); // NSLog(@"%@",[userDefaults objectForKey:@"userOptChoose"]); // NSLog(@"%@",[userDefaults objectForKey:@"theDays"]); [VC presentViewController:alertController animated:YES completion:nil]; }else{ #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0 alertViewTest = [[UIAlertView alloc] initWithTitle:@"致開發者的一封信" message:@"有了您的支持才能更好的為您服務,提供更加優質的,更加適合您的App,當然您也可以直接反饋問題給到我們" delegate:self cancelButtonTitle:@"????殘忍拒絕" otherButtonTitles:@"????好評贊賞",@"????我要吐槽", nil]; [alertViewTest show]; #endif } } #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; //當前時間戳的天數 NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]; int daySeconds = 24 * 60 * 60; NSInteger theDays = interval / daySeconds; //當前版本號 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue]; //userDefaults裡版本號 float udAppVersion = [[userDefaults objectForKey:@"appVersion"] intValue]; //userDefaults裡用戶選擇項目 int udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue]; //userDefaults裡用戶天數 int udtheDays = [[userDefaults objectForKey:@"theDays"] intValue]; //當前版本比userDefaults裡版本號高 if (appVersion>udAppVersion) { [userDefaults setObject:[NSString stringWithFormat:@"%f",appVersion] forKey:@"appVersion"]; } switch (buttonIndex) { case 0: //殘忍的拒絕 if (udUserChoose30) { [userDefaults setObject:@"3" forKey:@"userOptChoose"]; [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"]; }else{ [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)(theDays-udtheDays+3)] forKey:@"userOptChoose"]; } break; case 1:{ //好評 [userDefaults setObject:@"1" forKey:@"userOptChoose"]; [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"]; NSString *str = [NSString stringWithFormat: @"https://itunes.apple.com/cn/app/id%@?mt=8", self.myAppID ]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; } break; case 2:{ //不好用,我要提意見 [userDefaults setObject:@"2" forKey:@"userOptChoose"]; [userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"]; NSString *str = [NSString stringWithFormat: @"https://itunes.apple.com/cn/app/id%@?mt=8", self.myAppID ]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; } break; default: break; } // NSLog(@"%@",[userDefaults objectForKey:@"appVersion"]); // NSLog(@"%@",[userDefaults objectForKey:@"userOptChoose"]); // NSLog(@"%@",[userDefaults objectForKey:@"theDays"]); } #endif @end
具體使用方法如下:
#import "ViewController.h" #import "LBToAppStore.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(void)viewDidAppear:(BOOL)animated{ //用戶好評系統 LBToAppStore *toAppStore = [[LBToAppStore alloc]init]; toAppStore.myAppID = @"1067787090"; [toAppStore showGotoAppStore:self]; } @end
demo下載地址戳我