Skip to content

Commit

Permalink
Merge pull request #109 from erkanyildiz/master
Browse files Browse the repository at this point in the history
For 17.05
  • Loading branch information
erkanyildiz committed May 15, 2017
2 parents fa2bae1 + 52dc64d commit 5f8aea2
Show file tree
Hide file tree
Showing 26 changed files with 1,187 additions and 539 deletions.
26 changes: 23 additions & 3 deletions Countly.h
Expand Up @@ -7,7 +7,6 @@
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "CountlyUserDetails.h"
#import "CountlyCrashReporter.h"
#import "CountlyConfig.h"
#if TARGET_OS_IOS
#import <UserNotifications/UserNotifications.h>
Expand Down Expand Up @@ -44,7 +43,27 @@
- (void)setCustomHeaderFieldValue:(NSString *)customHeaderFieldValue;

/**
* Suspends Countly, add recorded events to request queue and ends current session.
* Starts session and sends @c begin_session request with default metrics for manual session handling.
* @discussion This method needs to be called for starting a session only if @c manualSessionHandling flag is set on initial configuration. Otherwise; sessions will be handled automatically by default, and calling this method will have no effect.
*/
- (void)beginSession;

/**
* Updates session and sends unsent session duration for manual session handling.
* @discussion This method needs to be called for updating a session only if @c manualSessionHandling flag is set on initial configuration. Otherwise; sessions will be handled automatically by default, and calling this method will have no effect.
*/
- (void)updateSession;

/**
* Ends session and sends @c end_session request for manual session handling.
* @discussion This method needs to be called for ending a session only if @c manualSessionHandling flag is set on initial configuration. Otherwise; sessions will be handled automatically by default, and calling this method will have no effect.
*/
- (void)endSession;


#if TARGET_OS_WATCH
/**
* Suspends Countly, adds recorded events to request queue and ends current session.
* @discussion This method needs to be called manually only on @c watchOS, on other platforms it will be called automatically.
*/
- (void)suspend;
Expand All @@ -54,6 +73,7 @@
* @discussion This method needs to be called manually only on @c watchOS, on other platforms it will be called automatically.
*/
- (void)resume;
#endif



Expand Down Expand Up @@ -218,7 +238,7 @@

/**
* Reports a visited view with given name manually.
* @discussion If auto ViewTracking feature is activated on start configuration, this method does not need to be called manually.
* @discussion If auto ViewTracking feature is activated on initial configuration, this method does not need to be called manually.
* @param viewName Name of the view visited
*/
- (void)reportView:(NSString *)viewName;
Expand Down
90 changes: 59 additions & 31 deletions Countly.m
Expand Up @@ -64,14 +64,12 @@ - (void)startWithConfig:(CountlyConfig *)config

COUNTLY_LOG(@"Initializing with %@ SDK v%@", kCountlySDKName, kCountlySDKVersion);

if(!CountlyDeviceInfo.sharedInstance.deviceID || config.forceDeviceIDInitialization)
{
if (!CountlyDeviceInfo.sharedInstance.deviceID || config.forceDeviceIDInitialization)
[CountlyDeviceInfo.sharedInstance initializeDeviceID:config.deviceID];
}

CountlyConnectionManager.sharedInstance.appKey = config.appKey;
CountlyConnectionManager.sharedInstance.host = config.host;
CountlyConnectionManager.sharedInstance.updateSessionPeriod = config.updateSessionPeriod;
BOOL hostHasExtraSlash = [[config.host substringFromIndex:config.host.length-1] isEqualToString:@"/"];
CountlyConnectionManager.sharedInstance.host = hostHasExtraSlash ? [config.host substringToIndex:config.host.length-1] : config.host;
CountlyConnectionManager.sharedInstance.alwaysUsePOST = config.alwaysUsePOST;
CountlyConnectionManager.sharedInstance.pinnedCertificates = config.pinnedCertificates;
CountlyConnectionManager.sharedInstance.customHeaderFieldName = config.customHeaderFieldName;
Expand All @@ -81,51 +79,51 @@ - (void)startWithConfig:(CountlyConfig *)config
CountlyPersistency.sharedInstance.eventSendThreshold = config.eventSendThreshold;
CountlyPersistency.sharedInstance.storedRequestsLimit = config.storedRequestsLimit;

CountlyCommon.sharedInstance.manualSessionHandling = config.manualSessionHandling;
CountlyCommon.sharedInstance.enableAppleWatch = config.enableAppleWatch;
CountlyCommon.sharedInstance.ISOCountryCode = config.ISOCountryCode;
CountlyCommon.sharedInstance.city = config.city;
CountlyCommon.sharedInstance.location = CLLocationCoordinate2DIsValid(config.location)?[NSString stringWithFormat:@"%f,%f", config.location.latitude, config.location.longitude]:nil;
CountlyCommon.sharedInstance.IP = config.IP;

#if TARGET_OS_IOS
CountlyStarRating.sharedInstance.message = config.starRatingMessage;
CountlyStarRating.sharedInstance.dismissButtonTitle = config.starRatingDismissButtonTitle;
CountlyStarRating.sharedInstance.sessionCount = config.starRatingSessionCount;
CountlyStarRating.sharedInstance.disableAskingForEachAppVersion = config.starRatingDisableAskingForEachAppVersion;
CountlyStarRating.sharedInstance.ratingCompletionForAutoAsk = config.starRatingCompletion;
[CountlyStarRating.sharedInstance checkForAutoAsk];

[CountlyCommon.sharedInstance transferParentDeviceID];

if([config.features containsObject:CLYPushNotifications])
if ([config.features containsObject:CLYPushNotifications])
{
CountlyPushNotifications.sharedInstance.isTestDevice = config.isTestDevice;
CountlyPushNotifications.sharedInstance.sendPushTokenAlways = config.sendPushTokenAlways;
CountlyPushNotifications.sharedInstance.doNotShowAlertForNotifications = config.doNotShowAlertForNotifications;
[CountlyPushNotifications.sharedInstance startPushNotifications];
}

if([config.features containsObject:CLYCrashReporting])
if ([config.features containsObject:CLYCrashReporting])
{
CountlyCrashReporter.sharedInstance.crashSegmentation = config.crashSegmentation;
[CountlyCrashReporter.sharedInstance startCrashReporting];
}
#endif

#if (TARGET_OS_IOS || TARGET_OS_TV)
if([config.features containsObject:CLYAutoViewTracking])
{
if ([config.features containsObject:CLYAutoViewTracking])
[CountlyViewTracking.sharedInstance startAutoViewTracking];
}
#endif

//NOTE: Disable APM feature until server completely supports it
// if([config.features containsObject:CLYAPM])
// if ([config.features containsObject:CLYAPM])
// [CountlyAPM.sharedInstance startAPM];

timer = [NSTimer scheduledTimerWithTimeInterval:CountlyConnectionManager.sharedInstance.updateSessionPeriod target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
timer = [NSTimer scheduledTimerWithTimeInterval:config.updateSessionPeriod target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
[NSRunLoop.mainRunLoop addTimer:timer forMode:NSRunLoopCommonModes];

[CountlyConnectionManager.sharedInstance beginSession];
if (!CountlyCommon.sharedInstance.manualSessionHandling)
[CountlyConnectionManager.sharedInstance beginSession];

#if (TARGET_OS_WATCH)
CountlyCommon.sharedInstance.enableAppleWatch = YES;
Expand All @@ -139,23 +137,23 @@ - (void)setNewDeviceID:(NSString *)deviceID onServer:(BOOL)onServer
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#if TARGET_OS_IOS
if([deviceID isEqualToString:CLYIDFA])
if ([deviceID isEqualToString:CLYIDFA])
deviceID = [CountlyDeviceInfo.sharedInstance zeroSafeIDFA];
else if([deviceID isEqualToString:CLYIDFV])
else if ([deviceID isEqualToString:CLYIDFV])
deviceID = UIDevice.currentDevice.identifierForVendor.UUIDString;
else if([deviceID isEqualToString:CLYOpenUDID])
else if ([deviceID isEqualToString:CLYOpenUDID])
deviceID = [Countly_OpenUDID value];
#elif TARGET_OS_OSX
if([deviceID isEqualToString:CLYOpenUDID])
if ([deviceID isEqualToString:CLYOpenUDID])
deviceID = [Countly_OpenUDID value];
#endif

#pragma GCC diagnostic pop

if([deviceID isEqualToString:CountlyDeviceInfo.sharedInstance.deviceID])
if ([deviceID isEqualToString:CountlyDeviceInfo.sharedInstance.deviceID])
return;

if(onServer)
if (onServer)
{
NSString* oldDeviceID = CountlyDeviceInfo.sharedInstance.deviceID;

Expand All @@ -165,11 +163,11 @@ - (void)setNewDeviceID:(NSString *)deviceID onServer:(BOOL)onServer
}
else
{
[Countly.sharedInstance suspend];
[self suspend];

[CountlyDeviceInfo.sharedInstance initializeDeviceID:deviceID];

[Countly.sharedInstance resume];
[self resume];

[CountlyPersistency.sharedInstance clearAllTimedEvents];
}
Expand All @@ -178,7 +176,27 @@ - (void)setNewDeviceID:(NSString *)deviceID onServer:(BOOL)onServer
- (void)setCustomHeaderFieldValue:(NSString *)customHeaderFieldValue
{
CountlyConnectionManager.sharedInstance.customHeaderFieldValue = customHeaderFieldValue;
[CountlyConnectionManager.sharedInstance tick];
[CountlyConnectionManager.sharedInstance proceedOnQueue];
}

#pragma mark ---

- (void)beginSession
{
if (CountlyCommon.sharedInstance.manualSessionHandling)
[CountlyConnectionManager.sharedInstance beginSession];
}

- (void)updateSession
{
if (CountlyCommon.sharedInstance.manualSessionHandling)
[CountlyConnectionManager.sharedInstance updateSession];
}

- (void)endSession
{
if (CountlyCommon.sharedInstance.manualSessionHandling)
[CountlyConnectionManager.sharedInstance endSession];
}

#pragma mark ---
Expand All @@ -188,38 +206,48 @@ - (void)onTimer:(NSTimer *)timer
if (isSuspended)
return;

[CountlyConnectionManager.sharedInstance updateSession];
if (!CountlyCommon.sharedInstance.manualSessionHandling)
[CountlyConnectionManager.sharedInstance updateSession];

[CountlyConnectionManager.sharedInstance sendEvents];
}

- (void)suspend
{
if (isSuspended)
return;

COUNTLY_LOG(@"Suspending...");

isSuspended = YES;

[CountlyConnectionManager.sharedInstance sendEvents];

[CountlyConnectionManager.sharedInstance endSession];
if (!CountlyCommon.sharedInstance.manualSessionHandling)
[CountlyConnectionManager.sharedInstance endSession];

[CountlyViewTracking.sharedInstance pauseView];

[CountlyPersistency.sharedInstance saveToFile];
}

- (void)resume
{
#if TARGET_OS_WATCH
//NOTE: skip first time to prevent double begin session because of applicationDidBecomeActive call on app lunch
//NOTE: skip first time to prevent double begin session because of applicationDidBecomeActive call on launch of watchOS apps
static BOOL isFirstCall = YES;

if(isFirstCall)
if (isFirstCall)
{
isFirstCall = NO;
return;
}
#endif

[CountlyConnectionManager.sharedInstance beginSession];
if (!CountlyCommon.sharedInstance.manualSessionHandling)
[CountlyConnectionManager.sharedInstance beginSession];

[CountlyViewTracking.sharedInstance resumeView];

isSuspended = NO;
}
Expand Down Expand Up @@ -343,14 +371,14 @@ - (void)endEvent:(NSString *)key segmentation:(NSDictionary *)segmentation count
{
CountlyEvent *event = [CountlyPersistency.sharedInstance timedEventForKey:key];

if(!event)
if (!event)
{
COUNTLY_LOG(@"Event with key '%@' not started before!", key);
return;
}

event.segmentation = segmentation;
event.count = MAX(count, 1);;
event.count = MAX(count, 1);
event.sum = sum;
event.duration = NSDate.date.timeIntervalSince1970 - event.timestamp;

Expand All @@ -365,7 +393,7 @@ - (void)endEvent:(NSString *)key segmentation:(NSDictionary *)segmentation count
- (void)askForNotificationPermission
{
UNAuthorizationOptions authorizationOptions = UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert;

[CountlyPushNotifications.sharedInstance askForNotificationPermissionWithOptions:authorizationOptions completionHandler:nil];
}

Expand Down
4 changes: 2 additions & 2 deletions Countly.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Countly'
s.version = '16.12'
s.version = '17.05'
s.license = {
:type => 'COMMUNITY',
:text => <<-LICENSE
Expand Down Expand Up @@ -34,7 +34,7 @@ Pod::Spec.new do |s|
s.author = {'Countly' => 'hello@count.ly'}
s.source = { :git => 'https://github.com/Countly/countly-sdk-ios.git', :tag => s.version.to_s }
s.source_files = '*.{h,m}'
s.public_header_files = 'Countly.h', 'CountlyUserDetails.h', 'CountlyConfig.h', 'CountlyCrashReporter.h'
s.public_header_files = 'Countly.h', 'CountlyUserDetails.h', 'CountlyConfig.h'
s.requires_arc = true
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.9'
Expand Down
2 changes: 1 addition & 1 deletion CountlyAPM.m
Expand Up @@ -86,7 +86,7 @@ - (BOOL)isException:(NSURLRequest *)request

[CountlyAPM.sharedInstance.exceptionURLs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop)
{
if([request.URL.host isEqualToString:obj] || [hostAndPath hasPrefix:obj])
if ([request.URL.host isEqualToString:obj] || [hostAndPath hasPrefix:obj])
{
isException = YES;
*stop = YES;
Expand Down
14 changes: 7 additions & 7 deletions CountlyAPMNetworkLog.m
Expand Up @@ -31,7 +31,7 @@ + (instancetype)logWithRequest:(NSURLRequest *)request andOriginalDelegate:(id)o
nl.originalDelegate = originalDelegate;
nl.sentDataSize = [self.class sentDataSizeForRequest:request];

if(startNow)
if (startNow)
{
nl.connectionType = CountlyDeviceInfo.connectionType;
nl.startTime = NSDate.date.timeIntervalSince1970;
Expand All @@ -52,7 +52,7 @@ - (void)updateWithResponse:(NSURLResponse *)response
self.HTTPStatusCode =((NSHTTPURLResponse*)response).statusCode;
self.receivedDataSize = [response expectedContentLength];

if(self.receivedDataSize == NSURLResponseUnknownLength)
if (self.receivedDataSize == NSURLResponseUnknownLength)
self.receivedDataSize = 0; //NOTE: sometimes expectedContentLength is not available
}

Expand Down Expand Up @@ -131,20 +131,20 @@ - (NSString *)description

- (BOOL)respondsToSelector:(SEL)aSelector
{
if([super respondsToSelector:aSelector])
if ([super respondsToSelector:aSelector])
return YES;

if ([self.originalDelegate respondsToSelector:aSelector])
return YES;

return NO;
}

- (id)forwardingTargetForSelector:(SEL)aSelector
{
{
if ([self.originalDelegate respondsToSelector:aSelector])
return self.originalDelegate;

return [super forwardingTargetForSelector:aSelector];
}

Expand Down

0 comments on commit 5f8aea2

Please sign in to comment.