項(xiàng)目中添加一個(gè)應(yīng)用評(píng)分功能,其實(shí)就是做一個(gè)彈出框,跳轉(zhuǎn)到App Store 中我們的應(yīng)用位置,需要在開發(fā)者平臺(tái)上App信息找到我們的App的AppID,有這個(gè)我們就可以跳轉(zhuǎn)到我們的應(yīng)用了
創(chuàng)建一個(gè)類,提供兩個(gè)接口,一個(gè)是appID,還有一個(gè)推出這個(gè)控制器方法,在.h文件中聲明以下屬性和方法:
UIAlertController *alertController;/// 這個(gè)是推出提示的控制器
@property (nonatomic,strong) NSString * myAppID;//appID
- (void)showGotoAppStore:(UIViewController *)VC;
.m代碼如下:
- (void)showGotoAppStore:(UIViewController *)VC{
//當(dāng)前版本號(hào)
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue];
//userDefaults里的天數(shù)
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int udtheDays = [[userDefaults objectForKey:@"theDays"] intValue];
//userDefaults里的版本號(hào)
float udAppVersion = [[userDefaults objectForKey:@"appVersion"] floatValue];
NSLog(@"%.2lf",[[userDefaults objectForKey:@"appVersion"] floatValue]);
//userDefaults里用戶上次的選項(xiàng)
int udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue];
//時(shí)間戳的天數(shù)
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
int daySeconds = 24 * 60 * 60;
NSInteger theDays = interval / daySeconds;
//版本升級(jí)之后的處理,全部規(guī)則清空,開始彈窗
if (udAppVersion && appVersion>udAppVersion) {
[userDefaults removeObjectForKey:@"theDays"];
[userDefaults removeObjectForKey:@"appVersion"];
[userDefaults removeObjectForKey:@"userOptChoose"];
[self alertUserCommentView:VC];
}
//1,從來沒彈出過的
//2,用戶選擇??我要吐槽让禀,5天之后再彈出
//3,用戶選擇??殘忍拒絕后谤牡,7天內(nèi)孩哑,每過1天會(huì)彈一次
//4,用戶選擇??殘忍拒絕的10天后,才會(huì)彈出
else if (!udUserChoose ||
(udUserChoose==2 && theDays-udtheDays>5) ||
(udUserChoose>=3 && theDays-udtheDays<=7 && theDays-udtheDays>udUserChoose-3) ||
(udUserChoose>=3 && theDays-udtheDays>10))
{
[self alertUserCommentView:VC];
}
warning 測(cè)試的時(shí)候使用
/// 測(cè)試的時(shí)候使用
// [self alertUserCommentView:VC];
}
-(void)alertUserCommentView:(UIViewController *)VC{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//當(dāng)前時(shí)間戳的天數(shù)
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
int daySeconds = 24 * 60 * 60;
NSInteger theDays = interval / daySeconds;
//當(dāng)前版本號(hào)
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue];
//userDefaults里版本號(hào)
float udAppVersion = [[userDefaults objectForKey:@"appVersion"] intValue];
//userDefaults里用戶選擇項(xiàng)目
int udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue];
//userDefaults里用戶天數(shù)
int udtheDays = [[userDefaults objectForKey:@"theDays"] intValue];
//當(dāng)前版本比userDefaults里版本號(hào)高
if (appVersion>udAppVersion) {
[userDefaults setObject:[NSString stringWithFormat:@"%f",appVersion] forKey:@"appVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%f",[[userDefaults objectForKey:@"appVersion"] floatValue]);
NSLog(@"%f",appVersion);
}
alertController = [UIAlertController alertControllerWithTitle:@"致開發(fā)者的一封信" message:@"有了您的支持才能更好的為您服務(wù),提供更加優(yōu)質(zhì)的,更加適合您的App,當(dāng)然您也可以直接反饋問題給到我們" 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:@"??好評(píng)贊賞" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
[userDefaults setObject:@"2" forKey:@"userOptChoose"];
[userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"];
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",
self.myAppID ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}];
UIAlertAction *showAction = [UIAlertAction actionWithTitle:@"??我要吐槽" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
if (udUserChoose<=3 || theDays-[[userDefaults objectForKey:@"theDays"] intValue]>10) {
[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:
@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",
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:@"致開發(fā)者的一封信" message:@"有了您的支持才能更好的為您服務(wù),提供更加優(yōu)質(zhì)的箕母,更加適合您的App,當(dāng)然您也可以直接反饋問題給到我們" delegate:self cancelButtonTitle:@"??殘忍拒絕" otherButtonTitles:@"??好評(píng)贊賞",@"??我要吐槽", nil];
[alertViewTest show];
#endif
}
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//當(dāng)前時(shí)間戳的天數(shù)
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
int daySeconds = 24 * 60 * 60;
NSInteger theDays = interval / daySeconds;
//當(dāng)前版本號(hào)
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
float appVersion = [[infoDictionary objectForKey:@"CFBundleShortVersionString"] floatValue];
//userDefaults里版本號(hào)
float udAppVersion = [[userDefaults objectForKey:@"appVersion"] intValue];
//userDefaults里用戶選擇項(xiàng)目
int udUserChoose = [[userDefaults objectForKey:@"userOptChoose"] intValue];
//userDefaults里用戶天數(shù)
int udtheDays = [[userDefaults objectForKey:@"theDays"] intValue];
//當(dāng)前版本比userDefaults里版本號(hào)高
if (appVersion>udAppVersion) {
[userDefaults setObject:[NSString stringWithFormat:@"%f",appVersion] forKey:@"appVersion"];
}
switch (buttonIndex) {
case 0: //殘忍的拒絕
if (udUserChoose<=3 || theDays-[[userDefaults objectForKey:@"theDays"] intValue]>10) {
[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:{ //好評(píng)
[userDefaults setObject:@"1" forKey:@"userOptChoose"];
[userDefaults setObject:[NSString stringWithFormat:@"%d",(int)theDays] forKey:@"theDays"];
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",
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:
@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",
self.myAppID ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
break;
default:
break;
}
// NSLog(@"%@",[userDefaults objectForKey:@"appVersion"]);
// NSLog(@"%@",[userDefaults objectForKey:@"userOptChoose"]);
// NSLog(@"%@",[userDefaults objectForKey:@"theDays"]);
}
#endif
使用的方法:
在合適的位置:
//用戶好評(píng)系統(tǒng)
DAppStore *toAppStore = [[DAppStore alloc]init];
toAppStore.myAppID = @"你的應(yīng)用的appID";
[toAppStore showGotoAppStore:self];