Authored by 孟顺

添加引用pod 减少不必要的代码

review by 枪兵
Pod::Spec.new do |s|
s.name = "YH_Analytics"
s.version = "3.2.3"
s.version = "3.2.4"
s.summary = "Analytics Kit"
s.description = <<-DESC
Analytics Kit
... ... @@ -33,22 +33,8 @@ Pod::Spec.new do |s|
ss.requires_arc = true
end
s.subspec 'OpenUDID' do |ss|
ss.source_files = 'YH_Analytics/YH_Analytics/YH_Analytics/YHAnalyticsDependencies/OpenUDID/OpenUDID.{h,m}'
ss.requires_arc = false
end
s.subspec 'KeychainStore' do |ss|
ss.source_files = 'YH_Analytics/YH_Analytics/YH_Analytics/YHAnalyticsDependencies/OpenUDID/YH_KeychainStore.{h,m}'
ss.requires_arc = true
end
s.subspec 'JTSHardwareInfo' do |ss|
ss.source_files = 'YH_Analytics/YH_Analytics/YH_Analytics/YHAnalyticsDependencies/JTSHardwareInfo/*.{h,m}'
ss.requires_arc = true
end
s.frameworks = 'Foundation', 'CoreGraphics', 'UIKit'
s.dependency 'YH_FoundationKit'
s.dependency 'YH_AFNetworking3'
s.dependency 'YH_DeviceUtility'
end
... ...
//
// JTSHardwareInfo.h
// JTSHardwareInfo
//
// Created by Jared Sinclair on 10/1/14.
// Copyright (c) 2014 Nice Boy LLC. All rights reserved.
//
// Originally based on code by InderKumarRathmore at https://github.com/InderKumarRathore/UIDevice-Hardware
//
@import UIKit;
@interface JTSHardwareInfo : NSObject
typedef NS_ENUM(NSInteger, JTSHardwareFamily) {
JTSHardwareFamily_Unknown,
JTSHardwareFamily_iPhone,
JTSHardwareFamily_iPad,
JTSHardwareFamily_iPodTouch,
};
typedef NS_ENUM(NSInteger, JTSHardwareType) {
JTSHardwareType_NotAvailable,
JTSHardwareType_Simulator,
JTSHardwareType_iPhone_Unreleased,
JTSHardwareType_iPodTouch_Unreleased,
JTSHardwareType_iPad_Unreleased,
JTSHardwareType_iPhone,
JTSHardwareType_iPhone_3G,
JTSHardwareType_iPhone_3G_China,
JTSHardwareType_iPhone_3GS,
JTSHardwareType_iPhone_3GS_China,
JTSHardwareType_iPhone_4_GSM,
JTSHardwareType_iPhone_4_GSM_2012,
JTSHardwareType_iPhone_4_CDMA,
JTSHardwareType_iPhone_4S,
JTSHardwareType_iPhone_4S_China,
JTSHardwareType_iPhone_5_GSM,
JTSHardwareType_iPhone_5_Global,
JTSHardwareType_iPhone_5C_GSM,
JTSHardwareType_iPhone_5C_Global,
JTSHardwareType_iPhone_5S_GSM,
JTSHardwareType_iPhone_5S_Global,
JTSHardwareType_iPhone_6,
JTSHardwareType_iPhone_6_China,
JTSHardwareType_iPhone_6Plus,
JTSHardwareType_iPhone_6Plus_China,
JTSHardwareType_iPhone_6S,
JTSHardwareType_iPhone_6S_Plus,
JTSHardwareType_iPhone_7,
JTSHardwareType_iPhone_7_Plus,
JTSHardwareType_iPodTouch_1Gen,
JTSHardwareType_iPodTouch_2Gen,
JTSHardwareType_iPodTouch_3Gen,
JTSHardwareType_iPodTouch_4Gen,
JTSHardwareType_iPodTouch_5Gen,
JTSHardwareType_iPad,
JTSHardwareType_iPad_Cellular,
JTSHardwareType_iPad_2_WiFi,
JTSHardwareType_iPad_2_GSM,
JTSHardwareType_iPad_2_CDMA,
JTSHardwareType_iPad_2_Mid_2012,
JTSHardwareType_iPad_3_WiFi,
JTSHardwareType_iPad_3_GSM,
JTSHardwareType_iPad_3_CDMA,
JTSHardwareType_iPad_4_WiFi,
JTSHardwareType_iPad_4_GSM,
JTSHardwareType_iPad_4_Global,
JTSHardwareType_iPad_Air_WiFi,
JTSHardwareType_iPad_Air_Cellular,
JTSHardwareType_iPad_Air_China,
JTSHardwareType_iPad_Mini_WiFi,
JTSHardwareType_iPad_Mini_GSM,
JTSHardwareType_iPad_Mini_Global,
JTSHardwareType_iPad_Mini_Retina_WiFi,
JTSHardwareType_iPad_Mini_Retina_Cellular,
JTSHardwareType_iPad_Mini_Retina_China,
};
+ (NSString *)hardwareIdentifier;
+ (JTSHardwareType)hardwareType;
+ (JTSHardwareFamily)hardwareFamily;
+ (NSString *)hardwareDisplayName;
+ (BOOL)airDropIsAvailable;
@end
//
// JTSHardwareInfo.m
// JTSHardwareInfo
//
// Created by Jared Sinclair on 10/1/14.
// Copyright (c) 2014 Nice Boy LLC. All rights reserved.
//
#import "JTSHardwareInfo.h"
#include <sys/types.h>
#include <sys/sysctl.h>
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone = @"iPhone1,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3G = @"iPhone1,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3G_China = @"iPhone1,2*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3GS = @"iPhone2,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_3GS_China = @"iPhone2,1*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM = @"iPhone3,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM_2012 = @"iPhone3,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4_CDMA = @"iPhone3,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4S = @"iPhone4,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_4S_China = @"iPhone4,1*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5_GSM = @"iPhone5,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5_Global = @"iPhone5,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5C_GSM = @"iPhone5,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5C_Global = @"iPhone5,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5S_GSM = @"iPhone6,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_5S_Global = @"iPhone6,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus_China = @"iPhone7,1*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus = @"iPhone7,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6_China = @"iPhone7,2*";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6 = @"iPhone7,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6S_Plus = @"iPhone8,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_6S = @"iPhone8,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_7_Plus = @"iPhone9,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPhone_7 = @"iPhone9,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad = @"iPad1,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Cellular = @"iPad1,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_WiFi = @"iPad2,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_GSM = @"iPad2,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_CDMA = @"iPad2,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_2_MID_2012 = @"iPad2,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_WiFi = @"iPad2,5";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_GSM = @"iPad2,6";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Global = @"iPad2,7";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_3_WiFi = @"iPad3,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_3_CDMA = @"iPad3,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_3_GSM = @"iPad3,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_4_WiFi = @"iPad3,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_4_GSM = @"iPad3,5";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_4_Global = @"iPad3,6";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Air_WiFi = @"iPad4,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Air_Cellular = @"iPad4,2";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Air_China = @"iPad4,3";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_WiFi = @"iPad4,4";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_Cellular = @"iPad4,5";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_China = @"iPad4,6";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_1G = @"iPod1,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_2G = @"iPod2,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_3G = @"iPod3,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_4G = @"iPod4,1";
static NSString * JTSHardwareInfo_HardwareIdentifier_iPodTouch_5G = @"iPod5,1";
@implementation JTSHardwareInfo
#pragma mark - Public
+ (NSString *)hardwareIdentifier {
static dispatch_once_t once;
static NSString * hardware;
dispatch_once(&once, ^ {
size_t size = 100;
char *hw_machine = malloc(size);
int name[] = {CTL_HW,HW_MACHINE};
sysctl(name, 2, hw_machine, &size, NULL, 0);
hardware = [NSString stringWithUTF8String:hw_machine];
free(hw_machine);
});
return hardware;
}
+ (JTSHardwareFamily)hardwareFamily {
static dispatch_once_t once;
static JTSHardwareFamily family;
dispatch_once(&once, ^ {
NSString *deviceName = [UIDevice currentDevice].name;
if ([deviceName hasPrefix:@"iPhone"]) {
family = JTSHardwareFamily_iPhone;
}
else if ([deviceName hasPrefix:@"iPad"]) {
family = JTSHardwareFamily_iPad;
}
else if ([deviceName hasPrefix:@"iPod"]) {
family = JTSHardwareFamily_iPodTouch;
}
else {
family = JTSHardwareFamily_Unknown;
}
});
return family;
}
+ (JTSHardwareType)hardwareType {
static dispatch_once_t once;
static JTSHardwareType type;
dispatch_once(&once, ^ {
type = [self getCurrentHardwareType];
});
return type;
}
+ (NSString *)hardwareDisplayName {
NSString *displayName = nil;
switch ([self hardwareType]) {
case JTSHardwareType_Simulator:
displayName = @"iOS Simulator";
break;
case JTSHardwareType_NotAvailable:
case JTSHardwareType_iPhone_Unreleased:
case JTSHardwareType_iPad_Unreleased:
case JTSHardwareType_iPodTouch_Unreleased:
displayName = [[UIDevice currentDevice] name];
break;
case JTSHardwareType_iPhone_7_Plus:
displayName = @"iPhone 7 Plus";
break;
case JTSHardwareType_iPhone_7:
displayName = @"iPhone 7";
break;
case JTSHardwareType_iPhone_6S:
displayName = @"iPhone 6S";
break;
case JTSHardwareType_iPhone_6S_Plus:
displayName = @"iPhone 6S Plus";
break;
case JTSHardwareType_iPhone_6_China:
displayName = @"iPhone 6 (China)";
break;
case JTSHardwareType_iPhone_6:
displayName = @"iPhone 6";
break;
case JTSHardwareType_iPhone_6Plus_China:
displayName = @"iPhone 6 Plus (China)";
break;
case JTSHardwareType_iPhone_6Plus:
displayName = @"iPhone 6 Plus";
break;
case JTSHardwareType_iPhone_5S_Global:
displayName = @"iPhone 5s (Global)";
break;
case JTSHardwareType_iPhone_5S_GSM:
displayName = @"iPhone 5s (GSM)";
break;
case JTSHardwareType_iPhone_5C_Global:
displayName = @"iPhone 5c (Global)";
break;
case JTSHardwareType_iPhone_5C_GSM:
displayName = @"iPhone 5c (GSM)";
break;
case JTSHardwareType_iPhone_5_Global:
displayName = @"iPhone 5 (Global)";
break;
case JTSHardwareType_iPhone_5_GSM:
displayName = @"iPhone 5 (GSM)";
break;
case JTSHardwareType_iPhone_4S_China:
displayName = @"iPhone 4S (China)";
break;
case JTSHardwareType_iPhone_4S:
displayName = @"iPhone 4S";
break;
case JTSHardwareType_iPhone_4_GSM_2012:
displayName = @"iPhone 4 (GSM 2012)";
break;
case JTSHardwareType_iPhone_4_CDMA:
displayName = @"iPhone 4 (CDMA)";
break;
case JTSHardwareType_iPhone_4_GSM:
displayName = @"iPhone 4 (GSM)";
break;
case JTSHardwareType_iPhone_3GS:
displayName = @"iPhone 3GS";
break;
case JTSHardwareType_iPhone_3GS_China:
displayName = @"iPhone 3GS (China/No WiFi)";
break;
case JTSHardwareType_iPhone_3G:
displayName = @"iPhone 3G";
break;
case JTSHardwareType_iPhone_3G_China:
displayName = @"iPhone 3G (China/No WiFi)";
break;
case JTSHardwareType_iPhone:
displayName = @"iPhone";
break;
case JTSHardwareType_iPad_Mini_Retina_China:
displayName = @"iPad Mini Retina (China)";
break;
case JTSHardwareType_iPad_Mini_Retina_Cellular:
displayName = @"iPad Mini Retina (Cellular)";
break;
case JTSHardwareType_iPad_Mini_Retina_WiFi:
displayName = @"iPad Mini Retina (WiFi)";
break;
case JTSHardwareType_iPad_Mini_Global:
displayName = @"iPad Mini (Global)";
break;
case JTSHardwareType_iPad_Mini_GSM:
displayName = @"iPad Mini (GSM)";
break;
case JTSHardwareType_iPad_Mini_WiFi:
displayName = @"iPad Mini (WiFi)";
break;
case JTSHardwareType_iPad_Air_China:
displayName = @"iPad Air (China)";
break;
case JTSHardwareType_iPad_Air_Cellular:
displayName = @"iPad Air (Cellular)";
break;
case JTSHardwareType_iPad_Air_WiFi:
displayName = @"iPad Air (WiFi)";
break;
case JTSHardwareType_iPad_4_Global:
displayName = @"iPad 4 (Global)";
break;
case JTSHardwareType_iPad_4_GSM:
displayName = @"iPad 4 (GSM)";
break;
case JTSHardwareType_iPad_4_WiFi:
displayName = @"iPad 4 (WiFi)";
break;
case JTSHardwareType_iPad_3_CDMA:
displayName = @"iPad 3 (CDMA)";
break;
case JTSHardwareType_iPad_3_GSM:
displayName = @"iPad 3 (GSM)";
break;
case JTSHardwareType_iPad_3_WiFi:
displayName = @"iPad 3 (WiFi)";
break;
case JTSHardwareType_iPad_2_Mid_2012:
displayName = @"iPad 2 (Mid 2012)";
break;
case JTSHardwareType_iPad_2_CDMA:
displayName = @"iPad 2 (CDMA)";
break;
case JTSHardwareType_iPad_2_GSM:
displayName = @"iPad (GSM)";
break;
case JTSHardwareType_iPad_2_WiFi:
displayName = @"iPad 2 (WiFi)";
break;
case JTSHardwareType_iPad_Cellular:
displayName = @"iPad (Cellular)";
break;
case JTSHardwareType_iPad:
displayName = @"iPad";
break;
case JTSHardwareType_iPodTouch_5Gen:
displayName = @"iPod Touch (5th Gen)";
break;
case JTSHardwareType_iPodTouch_4Gen:
displayName = @"iPod Touch (4th Gen)";
break;
case JTSHardwareType_iPodTouch_3Gen:
displayName = @"iPod Touch (3rd Gen)";
break;
case JTSHardwareType_iPodTouch_2Gen:
displayName = @"iPod Touch (2nd Gen)";
break;
case JTSHardwareType_iPodTouch_1Gen:
displayName = @"iPod Touch (1st Gen)";
break;
default:
displayName = @"Not Available";
break;
}
return displayName;
}
+ (BOOL)airDropIsAvailable {
static dispatch_once_t once;
static BOOL isAvailable;
dispatch_once(&once, ^ {
JTSHardwareType hardwareType = [self hardwareType];
if (hardwareType == JTSHardwareType_NotAvailable) {
isAvailable = YES;
}
else if (hardwareType == JTSHardwareType_iPhone_Unreleased) {
isAvailable = YES;
}
else if (hardwareType == JTSHardwareType_iPad_Unreleased) {
isAvailable = YES;
}
else if (hardwareType == JTSHardwareType_iPodTouch_Unreleased) {
isAvailable = YES;
}
else {
NSString *hardwareString = [self hardwareIdentifier];
if ([hardwareString hasPrefix:@"iPhone"]) {
isAvailable = (hardwareType >= JTSHardwareType_iPhone_5_GSM);
}
else if ([hardwareString hasPrefix:@"iPad"]) {
isAvailable = (hardwareType >= JTSHardwareType_iPad_4_WiFi);
}
else if ([hardwareString hasPrefix:@"iPod"]) {
isAvailable = (hardwareType >= JTSHardwareType_iPodTouch_5Gen);
}
}
});
return isAvailable;
}
#pragma mark - Private
+ (JTSHardwareType)getCurrentHardwareType {
NSString *hardware = [self hardwareIdentifier];
if ([hardware hasPrefix:@"iPhone"]) {
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_7]) return JTSHardwareType_iPhone_7;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_7_Plus]) return JTSHardwareType_iPhone_7_Plus;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6S]) return JTSHardwareType_iPhone_6S;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6S_Plus]) return JTSHardwareType_iPhone_6S_Plus;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6_China]) return JTSHardwareType_iPhone_6_China;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6]) return JTSHardwareType_iPhone_6;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus_China]) return JTSHardwareType_iPhone_6Plus_China;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_6Plus]) return JTSHardwareType_iPhone_6Plus;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5S_Global]) return JTSHardwareType_iPhone_5S_Global;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5S_GSM]) return JTSHardwareType_iPhone_5S_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5C_Global]) return JTSHardwareType_iPhone_5C_Global;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5C_GSM]) return JTSHardwareType_iPhone_5C_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5_Global]) return JTSHardwareType_iPhone_5_Global;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_5_GSM]) return JTSHardwareType_iPhone_5_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4S_China]) return JTSHardwareType_iPhone_4S_China;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4S]) return JTSHardwareType_iPhone_4S;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4_CDMA]) return JTSHardwareType_iPhone_4_CDMA;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM_2012]) return JTSHardwareType_iPhone_4_GSM_2012;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_4_GSM]) return JTSHardwareType_iPhone_4_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3GS_China]) return JTSHardwareType_iPhone_3GS_China;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3GS]) return JTSHardwareType_iPhone_3GS;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3G_China]) return JTSHardwareType_iPhone_3G_China;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone_3G]) return JTSHardwareType_iPhone_3G;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPhone]) return JTSHardwareType_iPhone;
return JTSHardwareType_iPhone_Unreleased;
}
else if ([hardware hasPrefix:@"iPad"]) {
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_China]) return JTSHardwareType_iPad_Mini_Retina_China;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_Cellular])return JTSHardwareType_iPad_Mini_Retina_Cellular;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Retina_WiFi]) return JTSHardwareType_iPad_Mini_Retina_WiFi;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_Global]) return JTSHardwareType_iPad_Mini_Global;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_GSM]) return JTSHardwareType_iPad_Mini_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Mini_WiFi]) return JTSHardwareType_iPad_Mini_WiFi;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Air_China]) return JTSHardwareType_iPad_Air_China;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Air_Cellular]) return JTSHardwareType_iPad_Air_Cellular;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Air_WiFi]) return JTSHardwareType_iPad_Air_WiFi;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_4_Global]) return JTSHardwareType_iPad_4_Global;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_4_GSM]) return JTSHardwareType_iPad_4_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_4_WiFi]) return JTSHardwareType_iPad_4_WiFi;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_3_CDMA]) return JTSHardwareType_iPad_3_CDMA;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_3_GSM]) return JTSHardwareType_iPad_3_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_3_WiFi]) return JTSHardwareType_iPad_3_WiFi;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_MID_2012]) return JTSHardwareType_iPad_2_Mid_2012;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_CDMA]) return JTSHardwareType_iPad_2_CDMA;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_GSM]) return JTSHardwareType_iPad_2_GSM;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_2_WiFi]) return JTSHardwareType_iPad_2_WiFi;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad_Cellular]) return JTSHardwareType_iPad_Cellular;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPad]) return JTSHardwareType_iPad;
return JTSHardwareType_iPad_Unreleased;
}
else if ([hardware hasPrefix:@"iPod"]) {
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_5G]) return JTSHardwareType_iPodTouch_5Gen;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_4G]) return JTSHardwareType_iPodTouch_4Gen;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_3G]) return JTSHardwareType_iPodTouch_3Gen;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_2G]) return JTSHardwareType_iPodTouch_2Gen;
if ([hardware isEqualToString:JTSHardwareInfo_HardwareIdentifier_iPodTouch_1G]) return JTSHardwareType_iPodTouch_1Gen;
return JTSHardwareType_iPodTouch_Unreleased;
}
if ([hardware isEqualToString:@"i386"]) return JTSHardwareType_Simulator;
if ([hardware isEqualToString:@"x86_64"]) return JTSHardwareType_Simulator;
return JTSHardwareType_NotAvailable;
}
@end
//
// OpenUDID.h
// openudid
//
// initiated by Yann Lechelle (cofounder @Appsfire) on 8/28/11.
// Copyright 2011 OpenUDID.org
//
// Main branches
// iOS code: https://github.com/ylechelle/OpenUDID
//
/*
!!! IMPORTANT !!!
IF YOU ARE GOING TO INTEGRATE OpenUDID INSIDE A (STATIC) LIBRARY,
PLEASE MAKE SURE YOU REFACTOR THE OpenUDID CLASS WITH A PREFIX OF YOUR OWN,
E.G. ACME_OpenUDID. THIS WILL AVOID CONFUSION BY DEVELOPERS WHO ARE ALSO
USING OpenUDID IN THEIR OWN CODE.
!!! IMPORTANT !!!
*/
/*
http://en.wikipedia.org/wiki/Zlib_License
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#import <Foundation/Foundation.h>
//
// Usage:
// #include "OpenUDID.h"
// NSString* openUDID = [OpenUDID value];
//
#define kOpenUDIDErrorNone 0
#define kOpenUDIDErrorOptedOut 1
#define kOpenUDIDErrorCompromised 2
@interface OpenUDID : NSObject {
}
+ (NSString*) value;
+ (NSString*) valueWithError:(NSError**)error;
+ (void) setOptOut:(BOOL)optOutValue;
+ (void) setUsedDevice;
+ (BOOL) isNewDevice;
+ (void) setFirstLaunch;
+ (BOOL) isFirstLaunch;
+ (NSString*) _generateFreshOpenUDID;
@end
//
// OpenUDID.m
// openudid
//
// initiated by Yann Lechelle (cofounder @Appsfire) on 8/28/11.
// Copyright 2011 OpenUDID.org
//
// Initiators/root branches
// iOS code: https://github.com/ylechelle/OpenUDID
// Android code: https://github.com/vieux/OpenUDID
//
// Contributors:
// https://github.com/ylechelle/OpenUDID/contributors
//
/*
http://en.wikipedia.org/wiki/Zlib_License
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#if __has_feature(objc_arc)
#error This file uses the classic non-ARC retain/release model; hints below...
// to selectively compile this file as non-ARC, do as follows:
// https://img.skitch.com/20120411-bcku69k1uw528cwh9frh5px8ya.png
#endif
#import "OpenUDID.h"
#import "YH_KeychainStore.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
#import <UIKit/UIPasteboard.h>
#import <UIKit/UIKit.h>
#else
#import <AppKit/NSPasteboard.h>
#endif
#define OpenUDIDLog(fmt, ...)
//#define OpenUDIDLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
//#define OpenUDIDLog(fmt, ...) NSLog((@"[Line %d] " fmt), __LINE__, ##__VA_ARGS__);
static NSString * kOpenUDIDSessionCache = nil;
static NSString * const kOpenUDIDKey = @"OpenUDID";
static NSString * const kOpenUDIDSlotKey = @"OpenUDID_slot";
static NSString * const kOpenUDIDAppUIDKey = @"OpenUDID_appUID";
static NSString * const kOpenUDIDTSKey = @"OpenUDID_createdTS";
static NSString * const kOpenUDIDOOTSKey = @"OpenUDID_optOutTS";
static NSString * const kOpenUDIDDomain = @"org.OpenUDID";
static NSString * const kOpenUDIDSlotPBPrefix = @"org.OpenUDID.slot.";
static int const kOpenUDIDRedundancySlots = 100;
static NSString * const kYohoSecretInfo = @"secretinfo";
static NSString * const kYohoBuyUdid = @"udid";
static BOOL isNew = YES;
static NSString * const kYohoDeviceInfo = @"deviceinfo";
static NSString * const kYohoBuyUsedDevice = @"useddevice";
static NSString * const kYohoBuyFirstLaunch = @"firstlaunch";
@interface OpenUDID (Private)
+ (void) _setDict:(id)dict forPasteboard:(id)pboard;
+ (NSMutableDictionary*) _getDictFromPasteboard:(id)pboard;
@end
@implementation OpenUDID
// Archive a NSDictionary inside a pasteboard of a given type
// Convenience method to support iOS & Mac OS X
//
+ (void) _setDict:(id)dict forPasteboard:(id)pboard {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
[pboard setData:[NSKeyedArchiver archivedDataWithRootObject:dict] forPasteboardType:kOpenUDIDDomain];
#else
[pboard setData:[NSKeyedArchiver archivedDataWithRootObject:dict] forType:kOpenUDIDDomain];
#endif
}
// Retrieve an NSDictionary from a pasteboard of a given type
// Convenience method to support iOS & Mac OS X
//
+ (NSMutableDictionary*) _getDictFromPasteboard:(id)pboard {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
id item = [pboard dataForPasteboardType:kOpenUDIDDomain];
#else
id item = [pboard dataForType:kOpenUDIDDomain];
#endif
if (item) {
@try{
item = [NSKeyedUnarchiver unarchiveObjectWithData:item];
} @catch(NSException* e) {
OpenUDIDLog(@"Unable to unarchive item %@ on pasteboard!", [pboard name]);
item = nil;
}
}
// return an instance of a MutableDictionary
return [NSMutableDictionary dictionaryWithDictionary:(item == nil || [item isKindOfClass:[NSDictionary class]]) ? item : nil];
}
// Private method to create and return a new OpenUDID
// Theoretically, this function is called once ever per application when calling [OpenUDID value] for the first time.
// After that, the caching/pasteboard/redundancy mechanism inside [OpenUDID value] returns a persistent and cross application OpenUDID
//
+ (NSString*) _generateFreshOpenUDID {
NSString* _openUDID = nil;
// August 2011: One day, this may no longer be allowed in iOS. When that is, just comment this line out.
// March 25th 2012: this day has come, let's remove this "outlawed" call...
#if TARGET_OS_IPHONE
// if([UIDevice instancesRespondToSelector:@selector(uniqueIdentifier)]){
// _openUDID = [[UIDevice currentDevice] uniqueIdentifier];
// }
#endif
// Next we try to use an alternative method which uses the host name, process ID, and a time stamp
// We then hash it with md5 to get 32 bytes, and then add 4 extra random bytes
// Collision is possible of course, but unlikely and suitable for most industry needs (e.g.. aggregate tracking)
//
if (_openUDID==nil) {
unsigned char result[16];
const char *cStr = [[[NSProcessInfo processInfo] globallyUniqueString] UTF8String];
CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
_openUDID = [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%08lx",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15],
arc4random() % 4294967295];
}
// Call to other developers in the Open Source community:
//
// feel free to suggest better or alternative "UDID" generation code above.
// NOTE that the goal is NOT to find a better hash method, but rather, find a decentralized (i.e. not web-based)
// 160 bits / 20 bytes random string generator with the fewest possible collisions.
//
return _openUDID;
}
// Main public method that returns the OpenUDID
// This method will generate and store the OpenUDID if it doesn't exist, typically the first time it is called
// It will return the null udid (forty zeros) if the user has somehow opted this app out (this is subject to 3rd party implementation)
// Otherwise, it will register the current app and return the OpenUDID
//
+ (NSString*) value {
static NSString * shareUdidString = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey];
NSString *secretInfoKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoSecretInfo];
NSString *appUdidKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoBuyUdid];
NSMutableDictionary *keychainnamepasswordKVPairs = (NSMutableDictionary *)[YH_KeychainStore load:secretInfoKey];
NSString * udidString = [keychainnamepasswordKVPairs objectForKey:appUdidKey];
if (udidString == nil || udidString.length == 0) {
NSString * lvstring = [OpenUDID valueWithError:nil];
NSMutableDictionary * yohoinfoKVPairs = [NSMutableDictionary dictionary];
[yohoinfoKVPairs setObject:lvstring forKey:appUdidKey];
[YH_KeychainStore save:secretInfoKey data:yohoinfoKVPairs];
udidString = lvstring;
}
shareUdidString = [NSString stringWithString:udidString];
});
return [shareUdidString copy];
}
+ (NSString*) valueWithError:(NSError **)error {
if (kOpenUDIDSessionCache!=nil) {
if (error!=nil)
*error = [NSError errorWithDomain:kOpenUDIDDomain
code:kOpenUDIDErrorNone
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"OpenUDID in cache from first call",@"description", nil]];
return kOpenUDIDSessionCache;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// The AppUID will uniquely identify this app within the pastebins
//
NSString * appUID = (NSString *) [defaults objectForKey:kOpenUDIDAppUIDKey];
if(appUID == nil)
{
// generate a new uuid and store it in user defaults
CFUUIDRef uuid = CFUUIDCreate(NULL);
appUID = (NSString *) CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
CFRelease(uuid);
}
NSString* openUDID = nil;
NSString* myRedundancySlotPBid = nil;
NSDate* optedOutDate = nil;
BOOL optedOut = NO;
BOOL saveLocalDictToDefaults = NO;
BOOL isCompromised = NO;
// Do we have a local copy of the OpenUDID dictionary?
// This local copy contains a copy of the openUDID, myRedundancySlotPBid (and unused in this block, the local bundleid, and the timestamp)
//
id localDict = [defaults objectForKey:kOpenUDIDKey];
if ([localDict isKindOfClass:[NSDictionary class]]) {
localDict = [NSMutableDictionary dictionaryWithDictionary:localDict]; // we might need to set/overwrite the redundancy slot
openUDID = [localDict objectForKey:kOpenUDIDKey];
myRedundancySlotPBid = [localDict objectForKey:kOpenUDIDSlotKey];
optedOutDate = [localDict objectForKey:kOpenUDIDOOTSKey];
optedOut = optedOutDate!=nil;
OpenUDIDLog(@"localDict = %@",localDict);
}
// Here we go through a sequence of slots, each of which being a UIPasteboard created by each participating app
// The idea behind this is to both multiple and redundant representations of OpenUDIDs, as well as serve as placeholder for potential opt-out
//
NSString* availableSlotPBid = nil;
NSMutableDictionary* frequencyDict = [NSMutableDictionary dictionaryWithCapacity:kOpenUDIDRedundancySlots];
for (int n=0; n<kOpenUDIDRedundancySlots; n++) {
NSString* slotPBid = [NSString stringWithFormat:@"%@%d",kOpenUDIDSlotPBPrefix,n];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
UIPasteboard* slotPB = [UIPasteboard pasteboardWithName:slotPBid create:NO];
#else
NSPasteboard* slotPB = [NSPasteboard pasteboardWithName:slotPBid];
#endif
OpenUDIDLog(@"SlotPB name = %@",slotPBid);
if (slotPB==nil) {
// assign availableSlotPBid to be the first one available
if (availableSlotPBid==nil) availableSlotPBid = slotPBid;
} else {
NSDictionary* dict = [OpenUDID _getDictFromPasteboard:slotPB];
NSString* oudid = [dict objectForKey:kOpenUDIDKey];
OpenUDIDLog(@"SlotPB dict = %@",dict);
if (oudid==nil) {
// availableSlotPBid could inside a non null slot where no oudid can be found
if (availableSlotPBid==nil) availableSlotPBid = slotPBid;
} else {
// increment the frequency of this oudid key
int count = [[frequencyDict valueForKey:oudid] intValue];
[frequencyDict setObject:[NSNumber numberWithInt:++count] forKey:oudid];
}
// if we have a match with the app unique id,
// then let's look if the external UIPasteboard representation marks this app as OptedOut
NSString* gid = [dict objectForKey:kOpenUDIDAppUIDKey];
if (gid!=nil && [gid isEqualToString:appUID]) {
myRedundancySlotPBid = slotPBid;
// the local dictionary is prime on the opt-out subject, so ignore if already opted-out locally
if (optedOut) {
optedOutDate = [dict objectForKey:kOpenUDIDOOTSKey];
optedOut = optedOutDate!=nil;
}
}
}
}
// sort the Frequency dict with highest occurence count of the same OpenUDID (redundancy, failsafe)
// highest is last in the list
//
NSArray* arrayOfUDIDs = [frequencyDict keysSortedByValueUsingSelector:@selector(compare:)];
NSString* mostReliableOpenUDID = (arrayOfUDIDs!=nil && [arrayOfUDIDs count]>0)? [arrayOfUDIDs lastObject] : nil;
OpenUDIDLog(@"Freq Dict = %@\nMost reliable %@",frequencyDict,mostReliableOpenUDID);
// if openUDID was not retrieved from the local preferences, then let's try to get it from the frequency dictionary above
//
if (openUDID==nil) {
if (mostReliableOpenUDID==nil) {
// this is the case where this app instance is likely to be the first one to use OpenUDID on this device
// we create the OpenUDID, legacy or semi-random (i.e. most certainly unique)
//
openUDID = [OpenUDID _generateFreshOpenUDID];
} else {
// or we leverage the OpenUDID shared by other apps that have already gone through the process
//
openUDID = mostReliableOpenUDID;
}
// then we create a local representation
//
if (localDict==nil) {
localDict = [NSMutableDictionary dictionaryWithCapacity:4];
[localDict setObject:openUDID forKey:kOpenUDIDKey];
[localDict setObject:appUID forKey:kOpenUDIDAppUIDKey];
[localDict setObject:[NSDate date] forKey:kOpenUDIDTSKey];
if (optedOut) [localDict setObject:optedOutDate forKey:kOpenUDIDTSKey];
saveLocalDictToDefaults = YES;
}
}
else {
// Sanity/tampering check
//
if (mostReliableOpenUDID!=nil && ![mostReliableOpenUDID isEqualToString:openUDID])
isCompromised = YES;
}
// Here we store in the available PB slot, if applicable
//
OpenUDIDLog(@"Available Slot %@ Existing Slot %@",availableSlotPBid,myRedundancySlotPBid);
if (availableSlotPBid!=nil && (myRedundancySlotPBid==nil || [availableSlotPBid isEqualToString:myRedundancySlotPBid])) {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
UIPasteboard* slotPB = [UIPasteboard pasteboardWithName:availableSlotPBid create:YES];
[slotPB setPersistent:YES];
#else
NSPasteboard* slotPB = [NSPasteboard pasteboardWithName:availableSlotPBid];
#endif
// save slotPBid to the defaults, and remember to save later
//
if (localDict) {
[localDict setObject:availableSlotPBid forKey:kOpenUDIDSlotKey];
saveLocalDictToDefaults = YES;
}
// Save the local dictionary to the corresponding UIPasteboard slot
//
if (openUDID && localDict)
[OpenUDID _setDict:localDict forPasteboard:slotPB];
}
// Save the dictionary locally if applicable
//
if (localDict && saveLocalDictToDefaults)
[defaults setObject:localDict forKey:kOpenUDIDKey];
// If the UIPasteboard external representation marks this app as opted-out, then to respect privacy, we return the ZERO OpenUDID, a sequence of 40 zeros...
// This is a *new* case that developers have to deal with. Unlikely, statistically low, but still.
// To circumvent this and maintain good tracking (conversion ratios, etc.), developers are invited to calculate how many of their users have opted-out from the full set of users.
// This ratio will let them extrapolate convertion ratios more accurately.
//
if (optedOut) {
if (error!=nil) *error = [NSError errorWithDomain:kOpenUDIDDomain
code:kOpenUDIDErrorOptedOut
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Application with unique id %@ is opted-out from OpenUDID as of %@",appUID,optedOutDate],@"description", nil]];
kOpenUDIDSessionCache = [[NSString stringWithFormat:@"%040x",0] retain];
return kOpenUDIDSessionCache;
}
// return the well earned openUDID!
//
if (error!=nil) {
if (isCompromised)
*error = [NSError errorWithDomain:kOpenUDIDDomain
code:kOpenUDIDErrorCompromised
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Found a discrepancy between stored OpenUDID (reliable) and redundant copies; one of the apps on the device is most likely corrupting the OpenUDID protocol",@"description", nil]];
else
*error = [NSError errorWithDomain:kOpenUDIDDomain
code:kOpenUDIDErrorNone
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"OpenUDID succesfully retrieved",@"description", nil]];
}
kOpenUDIDSessionCache = [openUDID retain];
return kOpenUDIDSessionCache;
}
+ (void) setOptOut:(BOOL)optOutValue {
// init call
[OpenUDID value];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// load the dictionary from local cache or create one
id dict = [defaults objectForKey:kOpenUDIDKey];
if ([dict isKindOfClass:[NSDictionary class]]) {
dict = [NSMutableDictionary dictionaryWithDictionary:dict];
} else {
dict = [NSMutableDictionary dictionaryWithCapacity:2];
}
// set the opt-out date or remove key, according to parameter
if (optOutValue)
[dict setObject:[NSDate date] forKey:kOpenUDIDOOTSKey];
else
[dict removeObjectForKey:kOpenUDIDOOTSKey];
// store the dictionary locally
[defaults setObject:dict forKey:kOpenUDIDKey];
OpenUDIDLog(@"Local dict after opt-out = %@",dict);
// reset memory cache
kOpenUDIDSessionCache = nil;
}
+ (void) setUsedDevice
{
if ([self isNewDevice]) {
isNew = NO;
NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey];
NSString *deviceInfoKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoDeviceInfo];
NSString *usedDeviceKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoBuyUsedDevice];
NSMutableDictionary *yohoinfoKVPairs = (NSMutableDictionary *)[YH_KeychainStore load:deviceInfoKey];
if (yohoinfoKVPairs == nil) {
yohoinfoKVPairs = [NSMutableDictionary dictionary];
}
[yohoinfoKVPairs setObject:@"Y" forKey:usedDeviceKey];
[YH_KeychainStore save:deviceInfoKey data:yohoinfoKVPairs];
}
}
+ (BOOL) isNewDevice
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey];
NSString *deviceInfoKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoDeviceInfo];
NSString *usedDeviceKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoBuyUsedDevice];
NSMutableDictionary *yohoinfoKVPairs = (NSMutableDictionary *)[YH_KeychainStore load:deviceInfoKey];
if (yohoinfoKVPairs && yohoinfoKVPairs[usedDeviceKey]) {
isNew = NO;
}
});
return isNew;
}
+ (void) setFirstLaunch
{
NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey];
NSString *deviceInfoKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoDeviceInfo];
NSString *firstLaunchKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoBuyFirstLaunch];
NSMutableDictionary *yohoinfoKVPairs = (NSMutableDictionary *)[YH_KeychainStore load:deviceInfoKey];
if (yohoinfoKVPairs == nil) {
yohoinfoKVPairs = [NSMutableDictionary dictionary];
}
[yohoinfoKVPairs setObject:@"N" forKey:firstLaunchKey];
[YH_KeychainStore save:deviceInfoKey data:yohoinfoKVPairs];
}
+ (BOOL) isFirstLaunch
{
NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey];
NSString *deviceInfoKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoDeviceInfo];
NSString *firstLaunchKey = [NSString stringWithFormat:@"%@.%@", bundleId, kYohoBuyFirstLaunch];
NSMutableDictionary *yohoinfoKVPairs = (NSMutableDictionary *)[YH_KeychainStore load:deviceInfoKey];
if (yohoinfoKVPairs && yohoinfoKVPairs[firstLaunchKey]) {
return NO;
} else {
return YES;
}
}
@end
//
// YH_KeychainStore.h
// YOHOBuy
//
// Created by Caoliu on 14/12/10.
// Copyright (c) 2014年 NewPower Co. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface YH_KeychainStore : NSObject
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)delete:(NSString *)service;
@end
//
// YH_KeychainStore.m
// YOHOBuy
//
// Created by Caoliu on 14/12/10.
// Copyright (c) 2014年 NewPower Co. All rights reserved.
//
#import "YH_KeychainStore.h"
#import <Security/Security.h>
@implementation YH_KeychainStore
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
service, (__bridge id)kSecAttrService,
service, (__bridge id)kSecAttrAccount,
(__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString *)service data:(id)data {
//Get search dictionary
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Delete old item before add new item
SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
//Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
}
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Configure the search setting
//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
[keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
+ (void)delete:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}
@end
... ... @@ -11,7 +11,7 @@
#import "NSString+YHAnalytics.h"
#import "YHCrashReporter.h"
#import "YHAssemblyAssistant.h"
#import "OpenUDID.h"
#import "YH_SessionUtility.h"
#import "YHError.h"
#import "YHEvent.h"
#import "YHPerformance.h"
... ... @@ -125,15 +125,9 @@
#pragma mark - Properties
- (NSString *)session
{
if (!_session) {
_session = [[[OpenUDID value] stringByAppendingString:[self timestamp]] md5];
_eventIndex = 0; //重启session时event的序数重置
}
return (_session ? : @"");
return [YH_SessionUtility sharedInstance].session;
}
#pragma mark - Private Method
... ... @@ -154,7 +148,7 @@
- (void)resetSession
{
YALog(@"app will enter foreground !!");
self.session = nil;
self.eventIndex = 0;
[[YHAssemblyAssistant sharedInstance] prepareImmediUploadDic:self.appId sessionId:self.session channelId:self.channelId];
}
... ...