Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Catalyst support to Core #2058

Merged
merged 22 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions AppCenter/AppCenter/Internals/Context/Device/MSDeviceTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ - (instancetype)init {

// Restore past sessions from NSUserDefaults.
NSData *devices = [MS_APP_CENTER_USER_DEFAULTS objectForKey:kMSPastDevicesKey];
NSArray *arrayFromData;
if (devices != nil) {
NSArray *arrayFromData = [NSKeyedUnarchiver unarchiveObjectWithData:devices];
arrayFromData = (NSArray *)[MS_KEYED_UNARCHIVER_DATA(devices) mutableCopy];

// If array is not nil, create a mutable version.
if (arrayFromData)
Expand Down Expand Up @@ -130,7 +131,7 @@ - (MSDevice *)device {
}

// Persist the device history in NSData format.
[MS_APP_CENTER_USER_DEFAULTS setObject:[NSKeyedArchiver archivedDataWithRootObject:self.deviceHistory] forKey:kMSPastDevicesKey];
[MS_APP_CENTER_USER_DEFAULTS setObject:MS_KEYED_ARCHIVER_DATA(self.deviceHistory) forKey:kMSPastDevicesKey];
}
return _device;
}
Expand All @@ -145,15 +146,15 @@ - (MSDevice *)updatedDevice {
#if TARGET_OS_IOS
CTTelephonyNetworkInfo *telephonyNetworkInfo = [CTTelephonyNetworkInfo new];
CTCarrier *carrier;

// The CTTelephonyNetworkInfo.serviceSubscriberCellularProviders method crash because of an issue in iOS 12.0
// It was fixed in iOS 12.1
if (@available(iOS 12.1, *)) {
NSDictionary<NSString *, CTCarrier *> *carriers = [telephonyNetworkInfo serviceSubscriberCellularProviders];
carrier = [self firstCarrier:carriers];
} else if (@available(iOS 12, *)) {
NSDictionary<NSString *, CTCarrier *> *carriers = [telephonyNetworkInfo valueForKey:@"serviceSubscriberCellularProvider"];
carrier = [self firstCarrier:carriers];
NSDictionary<NSString *, CTCarrier *> *carriers = [telephonyNetworkInfo valueForKey:@"serviceSubscriberCellularProvider"];
carrier = [self firstCarrier:carriers];
}

// Use the old API as fallback if new one doesn't work.
Expand All @@ -170,11 +171,14 @@ - (MSDevice *)updatedDevice {
newDevice.sdkVersion = [MSUtility sdkVersion];
newDevice.model = [self deviceModel];
newDevice.oemName = kMSDeviceManufacturer;
#if TARGET_OS_OSX
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
newDevice.osName = [self osName];
newDevice.osVersion = [self osVersion];
#else
newDevice.osName = [self osName:MS_DEVICE];
#endif
#if TARGET_OS_OSX
newDevice.osVersion = [self osVersion];
#else
newDevice.osVersion = [self osVersion:MS_DEVICE];
#endif
newDevice.osBuild = [self osBuild];
Expand Down Expand Up @@ -264,15 +268,15 @@ - (void)clearDevices {
}

// Clear persistence, but keep the latest information about the device.
[MS_APP_CENTER_USER_DEFAULTS setObject:[NSKeyedArchiver archivedDataWithRootObject:self.deviceHistory] forKey:kMSPastDevicesKey];
[MS_APP_CENTER_USER_DEFAULTS setObject:MS_KEYED_ARCHIVER_DATA(self.deviceHistory) forKey:kMSPastDevicesKey];
}
}

#pragma mark - Helpers

- (NSString *)deviceModel {
size_t size;
#if TARGET_OS_OSX
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
const char *name = "hw.model";
#else
const char *name = "hw.machine";
Expand All @@ -288,7 +292,7 @@ - (NSString *)deviceModel {
return model;
}

#if TARGET_OS_OSX
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
- (NSString *)osName {
return @"macOS";
}
Expand Down Expand Up @@ -391,11 +395,11 @@ - (NSString *)carrierCountry:(CTCarrier *)carrier {
return ([carrier.isoCountryCode length] > 0) ? carrier.isoCountryCode : nil;
}

- (CTCarrier *)firstCarrier:(NSDictionary<NSString *, CTCarrier *> *) carriers {
for (NSString *key in carriers) {
return carriers[key];
}
return nil;
- (CTCarrier *)firstCarrier:(NSDictionary<NSString *, CTCarrier *> *)carriers {
for (NSString *key in carriers) {
return carriers[key];
}
return nil;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static NSString *const kMSPastDevicesKey = @"PastDevices";
*/
- (NSString *)deviceModel;

#if TARGET_OS_OSX
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
/**
* Get the OS name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ - (instancetype)init {
if (self) {
NSData *data = [MS_APP_CENTER_USER_DEFAULTS objectForKey:kMSSessionIdHistoryKey];
if (data != nil) {
_sessionHistory = (NSMutableArray *)[(NSObject *)[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy];
_sessionHistory = (NSMutableArray *)[MS_KEYED_UNARCHIVER_DATA(data) mutableCopy];
}
if (!_sessionHistory) {
_sessionHistory = [NSMutableArray<MSSessionHistoryInfo *> new];
Expand Down Expand Up @@ -61,7 +61,7 @@ - (void)setSessionId:(nullable NSString *)sessionId {
self.currentSessionInfo.sessionId = sessionId;
self.currentSessionInfo.timestamp = [NSDate date];
[self.sessionHistory addObject:self.currentSessionInfo];
[MS_APP_CENTER_USER_DEFAULTS setObject:[NSKeyedArchiver archivedDataWithRootObject:self.sessionHistory] forKey:kMSSessionIdHistoryKey];
[MS_APP_CENTER_USER_DEFAULTS setObject:MS_KEYED_ARCHIVER_DATA(self.sessionHistory) forKey:kMSSessionIdHistoryKey];
MSLogVerbose([MSAppCenter logTag], @"Stored new session with id:%@ and timestamp: %@.", self.currentSessionInfo.sessionId,
self.currentSessionInfo.timestamp);
}
Expand All @@ -84,7 +84,7 @@ - (void)clearSessionHistoryAndKeepCurrentSession:(BOOL)keepCurrentSession {
if (keepCurrentSession) {
[self.sessionHistory addObject:self.currentSessionInfo];
}
[MS_APP_CENTER_USER_DEFAULTS setObject:[NSKeyedArchiver archivedDataWithRootObject:self.sessionHistory] forKey:kMSSessionIdHistoryKey];
[MS_APP_CENTER_USER_DEFAULTS setObject:MS_KEYED_ARCHIVER_DATA(self.sessionHistory) forKey:kMSSessionIdHistoryKey];
MSLogVerbose([MSAppCenter logTag], @"Cleared old sessions.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ - (instancetype)init {
if (self) {
NSData *data = [MS_APP_CENTER_USER_DEFAULTS objectForKey:kMSUserIdHistoryKey];
if (data != nil) {
_userIdHistory = (NSMutableArray *)[(NSObject *)[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy];
_userIdHistory = (NSMutableArray *)[MS_KEYED_UNARCHIVER_DATA(data) mutableCopy];
}
if (!_userIdHistory) {
_userIdHistory = [NSMutableArray<MSUserIdHistoryInfo *> new];
Expand All @@ -61,7 +61,7 @@ - (instancetype)init {
* Persist nil userId as a current userId to NSUserDefaults so that Crashes can retrieve a correct userId when apps crash between App
* Center start and setUserId call.
*/
[MS_APP_CENTER_USER_DEFAULTS setObject:[NSKeyedArchiver archivedDataWithRootObject:self.userIdHistory] forKey:kMSUserIdHistoryKey];
[MS_APP_CENTER_USER_DEFAULTS setObject:MS_KEYED_ARCHIVER_DATA(self.userIdHistory) forKey:kMSUserIdHistoryKey];
_delegates = [NSHashTable weakObjectsHashTable];
}
return self;
Expand Down Expand Up @@ -93,7 +93,7 @@ - (void)setUserId:(nullable NSString *)userId {
*/
[self.userIdHistory removeLastObject];
[self.userIdHistory addObject:self.currentUserIdInfo];
[MS_APP_CENTER_USER_DEFAULTS setObject:[NSKeyedArchiver archivedDataWithRootObject:self.userIdHistory] forKey:kMSUserIdHistoryKey];
[MS_APP_CENTER_USER_DEFAULTS setObject:MS_KEYED_ARCHIVER_DATA(self.userIdHistory) forKey:kMSUserIdHistoryKey];
MSLogVerbose([MSAppCenter logTag], @"Stored new userId:%@ and timestamp: %@.", self.currentUserIdInfo.userId,
self.currentUserIdInfo.timestamp);
synchronizedDelegates = [self.delegates allObjects];
Expand All @@ -120,7 +120,7 @@ - (void)clearUserIdHistory {
@synchronized(self) {
[self.userIdHistory removeAllObjects];
[self.userIdHistory addObject:self.currentUserIdInfo];
[MS_APP_CENTER_USER_DEFAULTS setObject:[NSKeyedArchiver archivedDataWithRootObject:self.userIdHistory] forKey:kMSUserIdHistoryKey];
[MS_APP_CENTER_USER_DEFAULTS setObject:MS_KEYED_ARCHIVER_DATA(self.userIdHistory) forKey:kMSUserIdHistoryKey];
MSLogVerbose([MSAppCenter logTag], @"Cleared old userIds while keeping current userId.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ + (void)load {

- (instancetype)init {
if ((self = [super init])) {
#if !TARGET_OS_OSX
#if !TARGET_OS_OSX && !TARGET_OS_MACCATALYST
self.deprecatedSelectors = @{kMSOpenURLOptions : kMSOpenURLSourceApplicationAnnotation};
#endif
}
Expand Down Expand Up @@ -75,7 +75,7 @@ - (void)custom_setDelegate:(id<MSApplicationDelegate>)delegate {

#pragma mark - Custom UIApplicationDelegate

#if !TARGET_OS_OSX
#if !TARGET_OS_OSX && !TARGET_OS_MACCATALYST

/*
* Those methods will never get called but their implementation will be used by swizzling. Those implementations will run within the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ NS_ASSUME_NONNULL_BEGIN

@optional

#if !TARGET_OS_OSX
#if !TARGET_OS_OSX && !TARGET_OS_MACCATALYST

/**
* Asks the delegate to open a resource specified by a URL, and provides a dictionary of launch options.
Expand Down
19 changes: 8 additions & 11 deletions AppCenter/AppCenter/Internals/Storage/MSLogDBStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ - (BOOL)saveLog:(id<MSLog>)log withGroupId:(NSString *)groupId flags:(MSFlags)fl
MSFlags persistenceFlags = flags & kMSPersistenceFlagsMask;

// Insert this log to the DB.
NSData *logData = [NSKeyedArchiver archivedDataWithRootObject:log];
NSString *base64Data = [logData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSString *base64Data = [MS_KEYED_ARCHIVER_DATA(log) base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

MSStorageBindableArray *addLogValues = [MSStorageBindableArray new];
[addLogValues addString:groupId];
[addLogValues addString:base64Data];
[addLogValues addNumber:@(persistenceFlags)];
NSString *addLogQuery =
[NSString stringWithFormat:@"INSERT INTO \"%@\" (\"%@\", \"%@\", \"%@\") VALUES (?, ?, ?)", kMSLogTableName,
kMSGroupIdColumnName, kMSLogColumnName, kMSPriorityColumnName];
NSString *addLogQuery = [NSString stringWithFormat:@"INSERT INTO \"%@\" (\"%@\", \"%@\", \"%@\") VALUES (?, ?, ?)", kMSLogTableName,
kMSGroupIdColumnName, kMSLogColumnName, kMSPriorityColumnName];

// Serialize target token.
if ([(NSObject *)log isKindOfClass:[MSCommonSchemaLog class]]) {
Expand All @@ -76,10 +74,9 @@ - (BOOL)saveLog:(id<MSLog>)log withGroupId:(NSString *)groupId flags:(MSFlags)fl
[addLogValues addString:encryptedToken];
[addLogValues addString:targetKey];
[addLogValues addNumber:@(persistenceFlags)];
addLogQuery =
[NSString stringWithFormat:@"INSERT INTO \"%@\" (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\") VALUES (?, ?, ?, ?, ?)",
kMSLogTableName, kMSGroupIdColumnName, kMSLogColumnName, kMSTargetTokenColumnName,
kMSTargetKeyColumnName, kMSPriorityColumnName];
addLogQuery = [NSString stringWithFormat:@"INSERT INTO \"%@\" (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\") VALUES (?, ?, ?, ?, ?)",
kMSLogTableName, kMSGroupIdColumnName, kMSLogColumnName, kMSTargetTokenColumnName,
kMSTargetKeyColumnName, kMSPriorityColumnName];
}
return [self executeQueryUsingBlock:^int(void *db) {
// Check maximum size.
Expand Down Expand Up @@ -297,7 +294,7 @@ - (void)deleteLogsWithBatchId:(NSString *)batchId groupId:(NSString *)groupId {

// Deserialize the log.
@try {
log = [NSKeyedUnarchiver unarchiveObjectWithData:logData];
log = (id<MSLog>)MS_KEYED_UNARCHIVER_DATA(logData);
} @catch (NSException *e) {
exception = e;
}
Expand Down Expand Up @@ -392,7 +389,7 @@ - (void)customizeDatabase:(void *)db {
* After altering current schema, database version should be bumped and actions for migration should be implemented in this method.
*/
- (void)migrateDatabase:(void *)db fromVersion:(NSUInteger __unused)version {

/*
* With version 3.0 of the SDK we decided to remove timestamp column and as
* it's a major SDK version and SQLite does not support removing column we just start over.
Expand Down
2 changes: 1 addition & 1 deletion AppCenter/AppCenter/Internals/Util/MSUtility+File.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ + (NSURL *)appCenterDirectoryURL {
NSArray<NSURL *> *urls = [fileManager URLsForDirectory:directory inDomains:NSUserDomainMask];
NSURL *baseDirUrl = [urls objectAtIndex:0];

#if TARGET_OS_OSX
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST

// Use the application's bundle identifier for macOS to make sure to use separate directories for each app.
NSString *bundleIdentifier = [NSString stringWithFormat:@"%@/", [MS_APP_MAIN_BUNDLE bundleIdentifier]];
Expand Down
12 changes: 12 additions & 0 deletions AppCenter/AppCenter/Internals/Util/MSUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#define MS_CLASS_NAME_WITHOUT_PREFIX [NSStringFromClass([self class]) substringFromIndex:2]
#define MS_IS_APP_EXTENSION ([[[NSBundle mainBundle] executablePath] rangeOfString:@".appex/"].length > 0)
#define MS_APP_MAIN_BUNDLE [NSBundle mainBundle]
#define MS_KEYED_UNARCHIVER_DATA(data) [MSUtility unarchiveKeyedData:data]
#define MS_KEYED_ARCHIVER_DATA(data) [MSUtility archiveKeyedData:data]
AnastasiaKubova marked this conversation as resolved.
Show resolved Hide resolved

/**
* Utility class that is used throughout the SDK.
Expand All @@ -30,4 +32,14 @@
*/
+ (NSString *)sdkVersion;

/**
* Unarchive data.
*/
AnastasiaKubova marked this conversation as resolved.
Show resolved Hide resolved
+ (NSObject *)unarchiveKeyedData:(NSData *)data;

/**
* Archive data.
*/
+ (NSData *)archiveKeyedData:(id)data;

@end
24 changes: 24 additions & 0 deletions AppCenter/AppCenter/Internals/Util/MSUtility.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,28 @@ + (NSString *)sdkVersion {
return [NSString stringWithUTF8String:appcenter_library_info.ms_version];
}

+ (NSObject *)unarchiveKeyedData:(NSData *)data {
if (@available(iOS 11.0, macOS 10.13, watchOS 4.0, *)) {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:nil];
unarchiver.requiresSecureCoding = NO;
return [unarchiver decodeTopLevelObjectForKey:NSKeyedArchiveRootObjectKey error:nil];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
#pragma clang diagnostic pop
}
}

+ (NSData *)archiveKeyedData:(id)data {
if (@available(macOS 10.13, iOS 11.0, watchOS 4.0, *)) {
return [NSKeyedArchiver archivedDataWithRootObject:data requiringSecureCoding:NO error:nil];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
return [NSKeyedArchiver archivedDataWithRootObject:data];
#pragma clang diagnostic pop
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags {
* This flag indicates that the specified nodename or address can be reached via
* an EDGE, GPRS, or other "cell" connection. Not available on macOS.
*/
#if !TARGET_OS_OSX
#if !TARGET_OS_OSX && !TARGET_OS_MACCATALYST
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) ==
kSCNetworkReachabilityFlagsIsWWAN) {

Expand Down
4 changes: 2 additions & 2 deletions AppCenter/AppCenter/MSAppCenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ - (void)applyPipelineEnabledState:(BOOL)isEnabled {

// Hookup to application life-cycle events.
if (isEnabled) {
#if !TARGET_OS_OSX
#if !TARGET_OS_OSX && !TARGET_OS_MACCATALYST
[MS_NOTIFICATION_CENTER addObserver:self
selector:@selector(applicationDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
Expand Down Expand Up @@ -713,7 +713,7 @@ + (void)resetSharedInstance {

#pragma mark - Application life cycle

#if !TARGET_OS_OSX
#if !TARGET_OS_OSX && !TARGET_OS_MACCATALYST
/**
* The application will go to the foreground.
*/
Expand Down
5 changes: 3 additions & 2 deletions AppCenter/AppCenterTests/MSAbstractLogTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "MSSDKExtension.h"
#import "MSTestFrameworks.h"
#import "MSUserExtension.h"
#import "MSUtility.h"

@interface MSAbstractLogTests : XCTestCase

Expand Down Expand Up @@ -59,8 +60,8 @@ - (void)testSerializingToDictionaryWorks {
- (void)testNSCodingSerializationAndDeserializationWorks {

// When
NSData *serializedLog = [NSKeyedArchiver archivedDataWithRootObject:self.sut];
id actual = [NSKeyedUnarchiver unarchiveObjectWithData:serializedLog];
NSData *serializedLog = MS_KEYED_ARCHIVER_DATA(self.sut);
id actual = MS_KEYED_UNARCHIVER_DATA(serializedLog);

// Then
assertThat(actual, notNilValue());
Expand Down
4 changes: 2 additions & 2 deletions AppCenter/AppCenterTests/MSAppCenterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ - (void)testChannelOneCollectorDelegateSet {
OCMVerify([self.channelGroupMock addDelegate:[OCMArg isKindOfClass:[MSOneCollectorChannelDelegate class]]]);
}

#if !TARGET_OS_OSX
#if !TARGET_OS_OSX && !TARGET_OS_MACCATALYST
- (void)testAppIsBackgrounded {

// If
Expand Down Expand Up @@ -935,7 +935,7 @@ - (void)testNoUserIdWhenSetUserIdIsNotCalledInNextVersion {
NSMutableArray *history = [NSMutableArray new];
[history addObject:[[MSUserIdHistoryInfo alloc] initWithTimestamp:[NSDate dateWithTimeIntervalSince1970:0] andUserId:@"alice"]];
[history addObject:[[MSUserIdHistoryInfo alloc] initWithTimestamp:[NSDate dateWithTimeIntervalSince1970:3000] andUserId:@"bob"]];
[self.settingsMock setObject:[NSKeyedArchiver archivedDataWithRootObject:history] forKey:@"UserIdHistory"];
[self.settingsMock setObject:MS_KEYED_ARCHIVER_DATA(history) forKey:@"UserIdHistory"];
[MSUserIdContext resetSharedInstance];

// When
Expand Down
Loading