|
| 1 | +/** |
| 2 | + @file BNCApplication.m |
| 3 | + @package Branch-SDK |
| 4 | + @brief Current application and extension info. |
| 5 | +
|
| 6 | + @author Edward Smith |
| 7 | + @date January 8, 2018 |
| 8 | + @copyright Copyright © 2018 Branch. All rights reserved. |
| 9 | +*/ |
| 10 | + |
| 11 | +#import "BNCApplication.h" |
| 12 | +#import "BNCLog.h" |
| 13 | +#import "BNCKeyChain.h" |
| 14 | + |
| 15 | +static NSString*const kBranchKeychainService = @"BranchKeychainService"; |
| 16 | +static NSString*const kBranchKeychainDevicesKey = @"BranchKeychainDevices"; |
| 17 | +static NSString*const kBranchKeychainFirstBuildKey = @"BranchKeychainFirstBuild"; |
| 18 | +static NSString*const kBranchKeychainFirstInstalldKey = @"BranchKeychainFirstInstall"; |
| 19 | + |
| 20 | +#pragma mark - BNCApplication |
| 21 | + |
| 22 | +@implementation BNCApplication |
| 23 | + |
| 24 | ++ (BNCApplication*) currentApplication { |
| 25 | + static BNCApplication *bnc_currentApplication = nil; |
| 26 | + static dispatch_once_t onceToken = 0; |
| 27 | + dispatch_once(&onceToken, ^{ |
| 28 | + bnc_currentApplication = [BNCApplication createCurrentApplication]; |
| 29 | + }); |
| 30 | + return bnc_currentApplication; |
| 31 | +} |
| 32 | + |
| 33 | ++ (BNCApplication*) createCurrentApplication { |
| 34 | + BNCApplication *application = [[BNCApplication alloc] init]; |
| 35 | + if (!application) return application; |
| 36 | + NSDictionary *info = [NSBundle mainBundle].infoDictionary; |
| 37 | + |
| 38 | + application->_bundleID = [NSBundle mainBundle].bundleIdentifier; |
| 39 | + application->_displayName = info[@"CFBundleDisplayName"]; |
| 40 | + application->_shortDisplayName = info[@"CFBundleName"]; |
| 41 | + |
| 42 | + application->_displayVersionString = info[@"CFBundleShortVersionString"]; |
| 43 | + application->_versionString = info[@"CFBundleVersion"]; |
| 44 | + |
| 45 | + application->_firstInstallBuildDate = [BNCApplication firstInstallBuildDate]; |
| 46 | + application->_currentBuildDate = [BNCApplication currentBuildDate]; |
| 47 | + |
| 48 | + application->_firstInstallDate = [BNCApplication firstInstallDate]; |
| 49 | + application->_currentInstallDate = [BNCApplication currentInstallDate]; |
| 50 | + |
| 51 | + return application; |
| 52 | +} |
| 53 | + |
| 54 | ++ (NSDate*) currentBuildDate { |
| 55 | + NSURL *appURL = nil; |
| 56 | + NSURL *bundleURL = [NSBundle mainBundle].bundleURL; |
| 57 | + NSDictionary *info = [NSBundle mainBundle].infoDictionary; |
| 58 | + NSString *appName = info[(__bridge NSString*)kCFBundleExecutableKey]; |
| 59 | + if (appName.length > 0 && bundleURL) { |
| 60 | + appURL = [bundleURL URLByAppendingPathComponent:appName]; |
| 61 | + } else { |
| 62 | + NSString *path = [[NSProcessInfo processInfo].arguments firstObject]; |
| 63 | + if (path) appURL = [NSURL fileURLWithPath:path]; |
| 64 | + } |
| 65 | + if (appURL == nil) |
| 66 | + return nil; |
| 67 | + |
| 68 | + NSError *error = nil; |
| 69 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 70 | + NSDictionary *attributes = [fileManager attributesOfItemAtPath:appURL.path error:&error]; |
| 71 | + if (error) { |
| 72 | + BNCLogError(@"Can't get build date: %@.", error); |
| 73 | + return nil; |
| 74 | + } |
| 75 | + NSDate * buildDate = [attributes fileCreationDate]; |
| 76 | + if (buildDate == nil || [buildDate timeIntervalSince1970] <= 0.0) { |
| 77 | + BNCLogError(@"Invalid build date: %@.", buildDate); |
| 78 | + } |
| 79 | + return buildDate; |
| 80 | +} |
| 81 | + |
| 82 | ++ (NSDate*) firstInstallBuildDate { |
| 83 | + NSError *error = nil; |
| 84 | + NSDate *firstBuildDate = |
| 85 | + [BNCKeyChain retrieveValueForService:kBranchKeychainService |
| 86 | + key:kBranchKeychainFirstBuildKey |
| 87 | + error:&error]; |
| 88 | + if (firstBuildDate) |
| 89 | + return firstBuildDate; |
| 90 | + |
| 91 | + firstBuildDate = [self currentBuildDate]; |
| 92 | + error = [BNCKeyChain storeValue:firstBuildDate |
| 93 | + forService:kBranchKeychainService |
| 94 | + key:kBranchKeychainFirstBuildKey |
| 95 | + cloudAccessGroup:nil]; |
| 96 | + |
| 97 | + return firstBuildDate; |
| 98 | +} |
| 99 | + |
| 100 | ++ (NSDate*) currentInstallDate { |
| 101 | + NSError *error = nil; |
| 102 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 103 | + NSURL *libraryURL = |
| 104 | + [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] firstObject]; |
| 105 | + NSDictionary *attributes = [fileManager attributesOfItemAtPath:libraryURL.path error:&error]; |
| 106 | + if (error) { |
| 107 | + BNCLogError(@"Can't get library date: %@.", error); |
| 108 | + return nil; |
| 109 | + } |
| 110 | + NSDate *installDate = [attributes fileCreationDate]; |
| 111 | + if (installDate == nil || [installDate timeIntervalSince1970] <= 0.0) { |
| 112 | + BNCLogError(@"Invalid install date."); |
| 113 | + } |
| 114 | + return installDate; |
| 115 | +} |
| 116 | + |
| 117 | ++ (NSDate*) firstInstallDate { |
| 118 | + NSError *error = nil; |
| 119 | + NSDate* firstInstallDate = |
| 120 | + [BNCKeyChain retrieveValueForService:kBranchKeychainService |
| 121 | + key:kBranchKeychainFirstInstalldKey |
| 122 | + error:&error]; |
| 123 | + if (firstInstallDate) |
| 124 | + return firstInstallDate; |
| 125 | + |
| 126 | + firstInstallDate = [self currentInstallDate]; |
| 127 | + error = [BNCKeyChain storeValue:firstInstallDate |
| 128 | + forService:kBranchKeychainService |
| 129 | + key:kBranchKeychainFirstInstalldKey |
| 130 | + cloudAccessGroup:nil]; |
| 131 | + |
| 132 | + return firstInstallDate; |
| 133 | +} |
| 134 | + |
| 135 | +- (NSDictionary*) deviceKeyIdentityValueDictionary { |
| 136 | + @synchronized (self.class) { |
| 137 | + NSError *error = nil; |
| 138 | + NSDictionary *deviceDictionary = |
| 139 | + [BNCKeyChain retrieveValueForService:kBranchKeychainService |
| 140 | + key:kBranchKeychainDevicesKey |
| 141 | + error:&error]; |
| 142 | + if (error) BNCLogWarning(@"While retrieving deviceKeyIdentityValueDictionary: %@.", error); |
| 143 | + if (!deviceDictionary) deviceDictionary = @{}; |
| 144 | + return deviceDictionary; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +@end |
0 commit comments