閱讀782 返回首頁    go 阿裏雲 go 技術社區[雲棲]


NSObject擴展

//
//  NSObject+Extension.h
//  CloudShopping
//
//  Created by sixiaobo on 14-7-8.
//  Copyright (c) 2014年 com.Uni2uni. All rights reserved.
//

#import <Foundation/Foundation.h>

// 應用在app store上的ID
#define kAppIDInAppStore    @"" // 發布以後才有APP ID
#define kAppStoreVersionKey @"AppStoreVersionKey"

/*!
 * @brief 通用輔助擴展類
 * @author huangyibiao
 */
@interface NSObject (Extension)

// 把對象轉換成JSON格式數據,如果轉換失敗,返回nil
+ (NSMutableData *)JSONDataWithObject:(id)object;

//! 保存應用在AppStore上版本到本地
+ (void)saveAppStoreVersionToUserDefaults;

//! 是否需要連網更新
+ (BOOL)isAppNeedToUpdate:(BOOL)needNetwork;

// 參數是要判斷的應用的URLSchemes
+ (BOOL)hadInstallApp:(NSString *)urlSchemes;
// 能否打開應用
+ (BOOL)canOpenApp:(NSString *)itunesUrlString;
// 打開自己開發的應用
+ (void)openApp:(NSString *)urlSchemes;
//! 進入AppStore
+ (void)goToAppStoreWithURLString:(NSString *)itunesUrlString;

@end


//
//  NSObject+Extension.m
//  CloudShopping
//
//  Created by sixiaobo on 14-7-8.
//  Copyright (c) 2014年 com.Uni2uni. All rights reserved.
//

#import "NSObject+Extension.h"

@implementation NSObject (Extension)

#pragma mark - 獲取JSON數據
// 把對象轉換成JSON格式數據,如果轉換失敗,返回nil
+ (NSMutableData *)JSONDataWithObject:(id)object {
    NSMutableData *postBodyData = nil;
    if ([NSJSONSerialization isValidJSONObject:object]) {
        NSError *error = nil;
        NSData *postData = [NSJSONSerialization dataWithJSONObject:object
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&error];
        if (error) {
            NSLog(@"error: %@", error.description);
        } else {
            postBodyData = [[NSMutableData alloc] initWithData:postData];
        }
    }
    return postBodyData;
}

#pragma mark - 獲取最新版本
+ (NSString *)obtainLatestAppVersion {
   // NSString *urlPath = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@", kAppIDInAppStore];
    NSString *latestVersion = nil;
    NSDictionary *jsonData = nil; // 這裏需要從網絡請求到,這裏隻是寫成nil,在發布後再實現
    NSArray *infoArray = [jsonData objectForKey:@"results"];
    if([infoArray count] > 0) {
        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
        latestVersion = [releaseInfo objectForKey:@"version"];
        
        // 在以前返回的值是如下格式:"4.0",後來變成了:"V4.0",所以需要去掉非數值字符。
        latestVersion = [latestVersion stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]];
    }
    
    return latestVersion;
}

#pragma mark - 保存應用在AppStore上的版本號到本地
+ (void)saveAppStoreVersionToUserDefaults {
    NSString *storeVersion = [kUserDefaults stringForKey:kAppStoreVersionKey];
    NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    // 應用當前的version,應該小於等於store上的version。如果不是,則說明應用升級後,
    // UserDefault中保存的store version未更新,需重新設。
    if(nil == storeVersion || [self version:bundleVersion isBiggerThan:storeVersion]) {
        storeVersion = [self obtainLatestAppVersion]; // 獲取最新的版本
        if (storeVersion) {
            [kUserDefaults setObject:storeVersion forKey:kAppStoreVersionKey];
        }
    }
    return;
}

#pragma mark - 是否需要更新應用
+ (BOOL)isAppNeedToUpdate:(BOOL)needNetwork {
    NSString *version = nil;
    if (needNetwork) { // 獲取應用在appStore上的版本
        version = [self obtainLatestAppVersion];
        if (version) { // 保存到本地
            [kUserDefaults setObject:version forKey:kAppStoreVersionKey];
        }
    } else { // 直接從本地獲取
        version = [kUserDefaults stringForKey:kAppStoreVersionKey];
    }
    
    if (!version) {
        return NO;
    }
    
    NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    if ([self version:version isBiggerThan:bundleVersion]) {
        return YES;
    }
    return NO;
}

+ (BOOL)version:(NSString *)versionA isBiggerThan:(NSString *)versionB {
    NSArray *a = [versionA componentsSeparatedByString:@"."];
    NSArray *b = [versionB componentsSeparatedByString:@"."];
    
    unsigned aa = [[a objectAtIndex:0] intValue];
    unsigned ab = [a count] > 1 ? [[a objectAtIndex:1] intValue] : 0;
    unsigned cc = [a count] > 2 ? [[a objectAtIndex:2] intValue] : 0;
    
    unsigned ba = [[b objectAtIndex:0] intValue];
    unsigned bb = [b count] > 1 ? [[b objectAtIndex:1] intValue] : 0;
    unsigned bc = [b count] > 2 ? [[b objectAtIndex:2] intValue] : 0;
    
    return ((aa > ba) || (aa == ba && ab > bb) || (aa == ba && ab == bb && cc > bc));
}

// 參數是要判斷的應用的URLSchemes
+ (BOOL)hadInstallApp:(NSString *)urlSchemes {
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlSchemes]]) {
        return YES;
    }
    return NO;
}

// 能否打開應用
+ (BOOL)canOpenApp:(NSString *)itunesUrlString {
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:itunesUrlString]]) {
        return YES;
    }
    return NO;
}

+ (void)openApp:(NSString *)urlSchemes {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlSchemes]];
    return;
}

#pragma mark - 進入AppStore應用
+ (void)goToAppStoreWithURLString:(NSString *)itunesUrlString {
#if TARGET_IPHONE_SIMULATOR
    NSLog(@"虛擬機不支持APP Store.打開iTunes不會有效果。");
#else
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:itunesUrlString]];
#endif
    return;
}



@end


最後更新:2017-04-03 05:39:42

  上一篇:go 給label添加超鏈接等處理
  下一篇:go Linux內核剖析 之 進程地址空間(三)