效果图:
具体调用部分
if (currVersionInt<serviceVersionInt) {
SCVersionUpdateView *tipView = [[SCVersionUpdateView alloc] initWithVersion:result[@"required_version"] updateType:result[@"type"] updateCon:result[@"update_content"] updateUrl:result[@"update_url"]];
[tipView show];
block?block(0):nil;
}else{
block?block(1):nil;
}
具体实现:
.h
@interface SCVersionUpdateView : UIView
- (instancetype)initWithVersion:(NSString *)version updateType:(NSString *)updateType updateCon:(NSString *)updateCon updateUrl:(NSString *)updateUrl;
- (void)show;
@end
.m
@interface SCVersionUpdateView()
@property (nonatomic,copy) NSString *updateUrl;
@end
@implementation SCVersionUpdateView
- (instancetype)initWithVersion:(NSString *)version updateType:(NSString *)updateType updateCon:(NSString *)updateCon updateUrl:(NSString *)updateUrl{
if (self = [super init]) {
self.updateUrl = updateUrl;
self.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
UIView *backView = [[UIView alloc] initWithFrame:self.frame];
backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
[self addSubview:backView];
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(48, ( CGRectGetHeight([UIScreen mainScreen].bounds) - 340) / 2, CGRectGetWidth([UIScreen mainScreen].bounds) - 48*2, 340)];
mainView.backgroundColor = [UIColor whiteColor];
mainView.layer.masksToBounds = YES;
mainView.layer.cornerRadius = 7;
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sc_version_update"]];
[imageView setFrame:CGRectMake(0, 0, CGRectGetWidth(mainView.frame),CGRectGetWidth(mainView.frame)* 149/305)];
[mainView addSubview:imageView];
UILabel *updateConLab = [[UILabel alloc] init];
updateConLab.text = [NSString stringWithFormat:@"版本%@",version];
updateConLab.font = [UIFont boldSystemFontOfSize:16];
updateConLab.frame = CGRectMake(15, imageView.frame.size.height+imageView.frame.origin.y+7 + 10,200, 22);
updateConLab.textColor = [self colorWithHexString:@"#333333"];
[mainView addSubview:updateConLab];
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, updateConLab.frame.size.height+updateConLab.frame.origin.y+16, mainView.frame.size.width-40, 150)];
textView.textColor = [self colorWithHexString:@"#888888"];
textView.font = [UIFont systemFontOfSize:14];
textView.text = updateCon;
textView.editable = NO;
[mainView addSubview:textView];
UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake((mainView.frame.size.width-140)/2, mainView.frame.size.height - 60, 140, 40)];
confirmBtn.layer.cornerRadius = 20;
[confirmBtn setBackgroundColor:[self colorWithHexString:@"#347AEB"]];
[confirmBtn setTitle:@"立即更新" forState:UIControlStateNormal];
[confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
[confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[mainView addSubview:confirmBtn];
[confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
// if (![updateType isEqualToString:@"force"]) {
UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(mainView.bounds.size.width/2-118, mainView.bounds.size.height-40-20, 113, 40)];
[cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
[cancelBtn setTitleColor:[self colorWithHexString:@"#666666"] forState:UIControlStateNormal];
[cancelBtn setTitle:@"暂不更新" forState:UIControlStateNormal];
cancelBtn.layer.cornerRadius = 5;
cancelBtn.layer.borderColor = [[self colorWithHexString:@"#ECECEC"] CGColor];
cancelBtn.layer.borderWidth = 1;
[mainView addSubview:cancelBtn];
[cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[confirmBtn setFrame:CGRectMake(mainView.bounds.size.width/2+5, mainView.bounds.size.height-40-20, 113, 40)];
confirmBtn.layer.cornerRadius = 5;
// }
[self addSubview:mainView];
}
return self;
}
- (UIColor *) colorWithHexString: (NSString *)color{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// 判断前缀
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor];
// 从六位数值中找到RGB对应的位数并转换
NSRange range;
range.location = 0;
range.length = 2;
//R、G、B
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
- (void)show{
[[UIApplication sharedApplication].keyWindow addSubview:self];
}
//去苹果商店更新
- (void)confirm{
NSURL *url = [NSURL URLWithString:self.updateUrl];
[[UIApplication sharedApplication] openURL:url];
}
- (void)cancel{
[self removeFromSuperview];
}
@end