在使用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 AppUpdateGrade
static 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];
- (void)pinfen
{
//评分
[[AppUpdateGrade sharedAppupdateGrade]appGrade:@"appid"];
}
转载请注明地址!
代码下载链接:http://download.youkuaiyun.com/detail/rhljiayou/5870441