IOS检测版本更新

.h
//
//  myCheckUpdate.h
//  
//
//  Created by X on 13-9-25.
//
//

#import <Foundation/Foundation.h>

// 检查更新
@interface myCheckUpdate : NSObject <UIAlertViewDelegate, NSURLConnectionDelegate> 

+(void) check;

@end

.m

//
//  myCheckUpdate.m
//  
//
//  Created by X on 13-9-25.
//
//

#import "myCheckUpdate.h"
#import "CJSONDeserializer.h"

@implementation myCheckUpdate

// =============================================================
-(NSString*) getLocalVer {
    NSDictionary* dict = [[NSBundle mainBundle] infoDictionary];
    return [dict objectForKey:@"CFBundleVersion"];
}


#pragma mark-
#pragma mark- UIAlertViewDelegate
// =============================================================
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        // 弹出AppStore更新界面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=yourAppID"]];
    }
}


#pragma mark-
#pragma mark- NSURLConnectionDelegate
// =============================================================
- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error{
    // 当请求失败时的相关操作;
    NSLog(@"Error info: %@", [error debugDescription]);
}

// 获取版本成功
// =============================================================
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data {
    NSDictionary* dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:nil];
    if (dict) {
        NSArray* results = [dict objectForKey:@"results"];
        if (results && results.count != 0) {
            NSDictionary* resultsDict = [results objectAtIndex:0];
            if (resultsDict) {
                NSString* appstoreVer = [resultsDict objectForKey:@"version"];
                if (appstoreVer && ![appstoreVer isEqualToString:[self getLocalVer]]) {
                    
                    UIAlertView* view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检测到新版本,是否更新" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
                    
                    [view show];
                    [view autorelease];
                }
            }
        }
    }
}

// =============================================================
-(void) start {
    NSString* url = @"http://itunes.apple.com/cn/lookup?id=yourAppID";
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:40] autorelease];
    [request setHTTPMethod:@"GET"];
    
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

// =============================================================
+(void) check {
    myCheckUpdate* checkInst = [[myCheckUpdate alloc] init];
    [checkInst start];
}

@end


使用

[myCheckUpdate check];


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值