在使用iphone/ipad應用的時候,有時候應用有更新升級,appstore會提醒使用者有相應的更新,程式中需要在使用者開啟應用的時候提醒使用者更新,那麼就需要自己在程式當中寫一個提醒事項,簡曆彈出框提醒使用者一下,就ok了!
具體代碼分享給大家,請大家注意,必須要有app的id。那麼你會想應用第一次沒有id怎麼辦?審請上線的時候就會得到id了,到時候有了id直接填上去就行了。
首先寫一個單例類:
//// AppUpdateGrade.h// QingDaoBroadcastIpad//// Created by iHope on 13-7-23.// Copyright (c) 2013年 hlren. All rights reserved.// 任海麗#import <Foundation/Foundation.h>@interface AppUpdateGrade : NSObject{ NSString *appId; //app的id NSString *trackViewUrl; //app的地址}+(AppUpdateGrade*)sharedAppupdateGrade; //建立-(void)appUpdate:(NSString *)appleID; //更新-(void)appGrade:(NSString *)appleID; //評分@end
//// AppUpdateGrade.m// QingDaoBroadcastIpad//// Created by iHope on 13-7-23.// Copyright (c) 2013年 hlren. All rights reserved.//#import "AppUpdateGrade.h"@implementation AppUpdateGradestatic AppUpdateGrade* appUpdateGrade = nil;+(AppUpdateGrade*)sharedAppupdateGrade{ @synchronized(self) { if (appUpdateGrade == nil) { appUpdateGrade = [[self alloc] init]; } } return appUpdateGrade;}//更新升級-(void)appUpdate:(NSString *)appleID{ appId = appleID; //http://itunes.apple.com/lookup?id=xx //根據appid從蘋果伺服器上得到json資料,再從json資料中得到版本資訊 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; // 設定URL [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]]; // 設定HTTP方法 [request setHTTPMethod:@"GET"]; // 發送同步請求, 這裡得returnData就是返回得數據楽 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil]; NSLog(@"%@",jsonData); NSArray *infoArray = [jsonData objectForKey:@"results"]; if (infoArray.count!=0) { NSDictionary *releaseInfo = [infoArray objectAtIndex:0]; NSString *latestVersion = [releaseInfo objectForKey:@"version"]; NSString *trackViewUrl1 = [releaseInfo objectForKey:@"trackViewUrl"];//地址trackViewUrl trackViewUrl = trackViewUrl1; //地址 double doubleUpdateVersion = [latestVersion doubleValue]; //擷取當前version版本資訊 //當前運行程式的版本資訊,可以在 mainBundle 裡面擷取: NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"]; double doubleCurrentVersion = [currentVersion doubleValue]; NSLog(@"doubleCurrentVersion:%f,%f",doubleCurrentVersion,doubleUpdateVersion); if (doubleCurrentVersion < doubleUpdateVersion) { UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:@"app應用程式名稱" message:@"有新版本,是否升級!" delegate: self cancelButtonTitle:@"取消" otherButtonTitles: @"升級", nil]; alert.tag = 1001; [alert show]; } }else{ //無此應用 } }//評分-(void)appGrade:(NSString *)appleID{ appId = appleID; BOOL neverGrade = [[[NSUserDefaults standardUserDefaults] objectForKey:@"neverGrade"] boolValue]; if(neverGrade != YES) { //提醒評分 UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:@"app應用程式名稱" message:@"請去appstore給我們評分" delegate: self cancelButtonTitle:@"取消" otherButtonTitles: @"現在去",@"不再提醒 ", nil]; alert.tag = 1000; [alert show]; }}-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (alertView.tag) { case 1000: { //評分 // Never Review Button if (buttonIndex == 2) { NSString *number = [NSString stringWithFormat:@"%d", YES]; [[NSUserDefaults standardUserDefaults] setObject:number forKey:@"neverGrade"]; [[NSUserDefaults standardUserDefaults] synchronize]; } // Review Button else if (buttonIndex == 1) { NSString *number = [NSString stringWithFormat:@"%d", YES]; [[NSUserDefaults standardUserDefaults] setObject:number forKey:@"neverGrade"]; [[NSUserDefaults standardUserDefaults] synchronize]; //"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id= NSString *str = [NSString stringWithFormat: @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appId ]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; } } break; case 1001: { //升級 if (buttonIndex == 1) { NSLog(@"%@",trackViewUrl); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]]; } } break; default: break; } }@end
1、更新升級
需要得到當前應用的version版本,獲得之前版本的version,比較之下是否需要更新!
當前應用的version:
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
之前應用的version:
需要請求http://itunes.apple.com/lookup?id=appid來擷取資料,分析出version;
2、應用評分
"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=appid“
直接開啟這個連結就可以給應用評份;
使用,匯入#import "AppUpdateGrade.h"
升級 [[AppUpdateGrade sharedAppupdateGrade]appUpdate:@"appid"]; //評分 afterDelay秒 60*1==60分鐘,表示1分鐘後調用pinfen方法 [self performSelector:@selector(pinfen) withObject:self afterDelay:1]; //升級 [[AppUpdateGrade sharedAppupdateGrade]appUpdate:@"appid"]; //評分 afterDelay秒 60*1==60分鐘,表示1分鐘後調用pinfen方法 [self performSelector:@selector(pinfen) withObject:self afterDelay:1];
(void)pinfen { //評分 [[AppUpdateGrade sharedAppupdateGrade]appGrade:@"appid"]; } - (void)pinfen{ //評分 [[AppUpdateGrade sharedAppupdateGrade]appGrade:@"appid"];}