Skip to content

Commit

Permalink
Version 2.0.0-beta3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ville Orkas committed Jun 21, 2016
1 parent bd3695e commit 03190d2
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 42 deletions.
8 changes: 6 additions & 2 deletions UnityAds/AdUnit/UADSViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ - (instancetype)initWithViews:(NSArray *)views supportedOrientations:(NSNumber *
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];

[self setViews:self.currentViews];

[[UADSWebViewApp getCurrentApp] sendEvent:NSStringFromAdUnitEvent(kUnityAdsViewControllerDidLoad) category:NSStringFromWebViewEventCategory(kUnityAdsWebViewEventCategoryAdunit) param1:nil];
}
Expand All @@ -40,6 +38,12 @@ - (void)viewDidAppear:(BOOL)animated {
[[UADSWebViewApp getCurrentApp] sendEvent:NSStringFromAdUnitEvent(kUnityAdsViewControllerDidAppear) category:NSStringFromWebViewEventCategory(kUnityAdsWebViewEventCategoryAdunit) param1:nil];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self setViews:self.currentViews];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

Expand Down
24 changes: 15 additions & 9 deletions UnityAds/AppSheet/UADSAppSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ @interface UADSAppSheet ()
@implementation UADSAppSheet

+ (instancetype)instance {
static UADSAppSheet *sharedInstance = nil;
static UADSAppSheet *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[UADSAppSheet alloc] init];
});

return sharedInstance;
}

Expand All @@ -46,10 +47,12 @@ - (void)prepareAppSheet:(NSDictionary *)parameters prepareTimeoutInSeconds:(int)
block(true, nil);
} else if(![self.appSheetLoading containsObject:iTunesId]) {
[self.appSheetLoading addObject:iTunesId];
SKStoreProductViewController *viewController = [[UADSAppSheetViewController alloc] init];
[viewController setDelegate:self];
[viewController setModalPresentationCapturesStatusBarAppearance:true];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
SKStoreProductViewController *viewController = [[UADSAppSheetViewController alloc] init];
[viewController setDelegate:self];
[viewController setModalPresentationCapturesStatusBarAppearance:true];

__block BOOL finished = NO;
__block BOOL cancelled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.prepareTimeoutInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -91,11 +94,14 @@ - (void)presentAppSheet:(NSDictionary *)parameters animated:(BOOL)animated compl
}
self.presentingParameters = parameters;
self.presentingAnimated = animated;
[[UADSApiAdUnit getAdUnit] presentViewController:cachedController animated:animated completion:^{
if ([UADSWebViewApp getCurrentApp]) {
[[UADSWebViewApp getCurrentApp] sendEvent:NSStringFromAppSheetEvent(kAppSheetOpened) category:NSStringFromWebViewEventCategory(kUnityAdsWebViewEventCategoryAppSheet) param1:parameters, nil];
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
[[UADSApiAdUnit getAdUnit] presentViewController:cachedController animated:animated completion:^{
if ([UADSWebViewApp getCurrentApp]) {
[[UADSWebViewApp getCurrentApp] sendEvent:NSStringFromAppSheetEvent(kAppSheetOpened) category:NSStringFromWebViewEventCategory(kUnityAdsWebViewEventCategoryAppSheet) param1:parameters, nil];
}
}];
});

block(true, nil);
} else {
block(false, NSStringFromAppSheetError(kUnityAdsAppSheetErrorNotFound));
Expand Down
1 change: 1 addition & 0 deletions UnityAds/Configuration/UADSConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ - (void)makeRequest {
NSString *urlString = [NSString stringWithFormat:@"%@%@", [self configUrl], [self buildQueryString]];
UADSLogDebug(@"Requesting configuration with: %@", urlString);

[self setError:nil];
NSError *error;
UADSWebRequest *request = [[UADSWebRequest alloc] initWithUrl:urlString requestType:@"GET" headers:NULL connectTimeout:30000];
NSData *responseData = [request makeRequest];
Expand Down
3 changes: 1 addition & 2 deletions UnityAds/Configuration/UADSInitialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@interface UADSInitializeState : NSOperation

@property (nonatomic, assign) UADSConfiguration *configuration;
@property (nonatomic, assign) NSOperationQueue *queue;

- (instancetype)execute;
- (instancetype)initWithConfiguration:(UADSConfiguration *)configuration;
Expand Down Expand Up @@ -61,7 +60,7 @@

@interface UADSInitializeStateCreate : UADSInitializeState

@property (atomic, retain) NSString *webViewData;
@property (atomic, strong) NSString *webViewData;

- (instancetype)initWithConfiguration:(UADSConfiguration *)configuration webViewData:(NSString *)webViewData;

Expand Down
42 changes: 23 additions & 19 deletions UnityAds/Configuration/UADSInitialize.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import "UADSWebRequestQueue.h"
#import "UADSCacheQueue.h"
#import "UADSPlacement.h"
#import "UADSNotificationObserver.h"
#import "NSString+Hash.h"

@implementation UADSInitialize
Expand All @@ -26,7 +27,6 @@ + (void)initialize:(UADSConfiguration *)configuration {
if (initializeQueue && initializeQueue.operationCount == 0) {
currentConfiguration = configuration;
id state = [[UADSInitializeStateReset alloc] initWithConfiguration:currentConfiguration];
[state setQueue:initializeQueue];
[initializeQueue addOperation:state];
}
}
Expand All @@ -41,8 +41,8 @@ @implementation UADSInitializeState

- (void)main {
id nextState = [self execute];
if (nextState && self.queue) {
[self.queue addOperation:nextState];
if (nextState && initializeQueue) {
[initializeQueue addOperation:nextState];
}
}

Expand Down Expand Up @@ -103,11 +103,14 @@ - (instancetype)execute {
[UADSSdkProperties setInitialized:false];
[UADSPlacement reset];
[UADSCacheQueue cancelAllDownloads];
[UADSConnectivityMonitor stopAll];
[UADSWebRequestQueue cancelAllOperations];
dispatch_async(dispatch_get_main_queue(), ^{
[UADSConnectivityMonitor stopAll];
});
[UADSStorageManager init];
[UADSNotificationObserver unregisterNotificationObserver];

id nextState = [[UADSInitializeStateConfig alloc] initWithConfiguration:self.configuration retries:0];
[nextState setQueue:self.queue];
return nextState;
}

Expand Down Expand Up @@ -137,20 +140,17 @@ - (instancetype)execute {

if (!self.configuration.error) {
id nextState = [[UADSInitializeStateLoadCache alloc] initWithConfiguration:self.configuration];
[nextState setQueue:self.queue];
return nextState;
}
else if (self.configuration.error && self.retries < self.maxRetries) {
self.retries++;
id retryState = [[UADSInitializeStateConfig alloc] initWithConfiguration:self.configuration retries:self.retries];
id nextState = [[UADSInitializeStateRetry alloc] initWithConfiguration:self.configuration retryState:retryState retryDelay:self.retryDelay];
[nextState setQueue:self.queue];
return nextState;
}
else {
id erroredState = [[UADSInitializeStateConfig alloc] initWithConfiguration:self.configuration retries:self.retries];
id nextState = [[UADSInitializeStateNetworkError alloc] initWithConfiguration:self.configuration erroredState:erroredState];
[nextState setQueue:self.queue];
return nextState;
}
}
Expand All @@ -173,13 +173,11 @@ - (instancetype)execute {
if (!localWebViewHash || (localWebViewHash && [localWebViewHash isEqualToString:self.configuration.webViewHash])) {
UADSLogInfo(@"Unity Ads init: webapp loaded from local cache");
id nextState = [[UADSInitializeStateCreate alloc] initWithConfiguration:self.configuration webViewData:fileString];
[nextState setQueue:self.queue];
return nextState;
}
}

id nextState = [[UADSInitializeStateLoadWeb alloc] initWithConfiguration:self.configuration retries:0];
[nextState setQueue:self.queue];
return nextState;
}

Expand Down Expand Up @@ -216,19 +214,16 @@ - (instancetype)execute {
self.retries++;
id retryState = [[UADSInitializeStateLoadWeb alloc] initWithConfiguration:self.configuration retries:self.retries];
id nextState = [[UADSInitializeStateRetry alloc] initWithConfiguration:self.configuration retryState:retryState retryDelay:self.retryDelay];
[nextState setQueue:self.queue];
return nextState;
}
else if (webRequest.error) {
id erroredState = [[UADSInitializeStateLoadWeb alloc] initWithConfiguration:self.configuration retries:self.retries];
id nextState = [[UADSInitializeStateNetworkError alloc] initWithConfiguration:self.configuration erroredState:erroredState];
[nextState setQueue:self.queue];
return nextState;
}

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
id nextState = [[UADSInitializeStateCreate alloc] initWithConfiguration:self.configuration webViewData:responseString];
[nextState setQueue:self.queue];
return nextState;
}

Expand All @@ -246,7 +241,6 @@ - (instancetype)execute {
[UADSWebViewApp create:self.configuration];

id nextState = [[UADSInitializeStateComplete alloc] initWithConfiguration:self.configuration];
[nextState setQueue:self.queue];
return nextState;
}

Expand Down Expand Up @@ -313,19 +307,29 @@ - (void)disconnected {

- (instancetype)execute {
UADSLogError(@"Unity Ads init: network error, waiting for connection events");

[UADSConnectivityMonitor startListening:self];


self.blockCondition = [[NSCondition alloc] init];
[self.blockCondition lock];

dispatch_async(dispatch_get_main_queue(), ^{
[UADSConnectivityMonitor startListening:self];
});

BOOL success = [self.blockCondition waitUntilDate:[[NSDate alloc] initWithTimeIntervalSinceNow:10000 * 60]];

if (success) {
[UADSConnectivityMonitor stopListening:self];
dispatch_async(dispatch_get_main_queue(), ^{
[UADSConnectivityMonitor stopListening:self];
});

[self.blockCondition unlock];
return self.erroredState;
}
else {
[UADSConnectivityMonitor stopListening:self];
dispatch_async(dispatch_get_main_queue(), ^{
[UADSConnectivityMonitor stopListening:self];

});
}

[self.blockCondition unlock];
Expand Down
2 changes: 1 addition & 1 deletion UnityAds/Connectivity/UADSConnectivityMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ + (void)sendToWebview:(SCNetworkReachabilityRef)reachabilityRef flags:(SCNetwork
[webViewApp sendEvent: Connected category:NSStringFromWebViewEventCategory(kUnityAdsWebViewEventCategoryConnectivity) param1:[NSNumber numberWithBool:TRUE], [NSNumber numberWithInt:0], nil];
break;
case ReachableViaWWAN:
[webViewApp sendEvent: Connected category:NSStringFromWebViewEventCategory(kUnityAdsWebViewEventCategoryConnectivity) param1:[NSNumber numberWithBool:FALSE], [UADSConnectivityUtils getNetworkType], nil];
[webViewApp sendEvent: Connected category:NSStringFromWebViewEventCategory(kUnityAdsWebViewEventCategoryConnectivity) param1:[NSNumber numberWithBool:FALSE], [NSNumber numberWithInteger:[UADSConnectivityUtils getNetworkType]], nil];
break;
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion UnityAds/Device/UADSStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef NS_ENUM(NSInteger, UnityAdsStorageType) {

@property (nonatomic, assign) NSString *targetFileName;
@property (nonatomic, assign) UnityAdsStorageType type;
@property (nonatomic, retain) NSMutableDictionary *storageContents;
@property (nonatomic, strong) NSMutableDictionary *storageContents;

- (instancetype)initWithLocation:(NSString *)fileLocation type:(UnityAdsStorageType)type;
- (BOOL)setValue:(id)value forKey:(NSString *)key;
Expand Down
15 changes: 13 additions & 2 deletions UnityAds/Properties/UADSSdkProperties.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
NSString * const kUnityAdsCacheDirName = @"UnityAdsCache";
NSString * const kUnityAdsLocalCacheFilePrefix = @"UnityAdsCache-";
NSString * const kUnityAdsLocalStorageFilePrefix = @"UnityAdsStorage-";
NSString * const kUnityAdsVersionName = @"2.0.0-beta2";
NSString * const kUnityAdsVersionName = @"2.0.0-beta3";
NSString * const kUnityAdsFlavorDebug = @"debug";
NSString * const kUnityAdsFlavorRelease = @"release";
int const kUnityAdsVersionCode = 2000;
Expand Down Expand Up @@ -101,7 +101,18 @@ + (NSString *)getLocalWebViewFile {
+ (NSString *)getCacheDirectory {
if (cacheDirectory == NULL) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
cacheDirectory = [paths firstObject];

BOOL cacheLocationIsDirectory = YES;
if (paths.count != 0) {
[[NSFileManager defaultManager] fileExistsAtPath:[paths firstObject] isDirectory:&cacheLocationIsDirectory];
}

if (cacheLocationIsDirectory) {
cacheDirectory = [paths firstObject];
} else {
cacheDirectory = NSTemporaryDirectory();
}

}

return cacheDirectory;
Expand Down
2 changes: 2 additions & 0 deletions UnityAds/Request/UADSResolve.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
@property (nonatomic, strong) NSCondition *blockCondition;
@property (nonatomic, strong) NSString *error;
@property (nonatomic, strong) NSString *errorMessage;
@property (nonatomic, assign) BOOL canceled;

- (instancetype)initWithHostName:(NSString *)hostName;
- (void)resolve;
- (void)cancel;

@end
6 changes: 6 additions & 0 deletions UnityAds/Request/UADSResolve.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ - (instancetype)initWithHostName:(NSString *)hostName {

if (self) {
[self setHostName:hostName];
[self setCanceled:false];
}

return self;
Expand Down Expand Up @@ -52,4 +53,9 @@ - (void)openBlock {
[self.blockCondition unlock];
}

- (void)cancel {
[self setCanceled:true];
[self openBlock];
}

@end
28 changes: 27 additions & 1 deletion UnityAds/Request/UADSResolveOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,37 @@ - (instancetype)initWithHostName:(NSString *)hostName completeBlock:(UnityAdsRes
}

- (void)main {
[self startObserving];
[self.resolve resolve];
[self stopObserving];

if (self.completeBlock) {
if (self.completeBlock && self.resolve && !self.resolve.canceled) {
self.completeBlock(self.resolve.hostName, self.resolve.address, self.resolve.error, self.resolve.errorMessage);
}
}

- (void)startObserving {
@try {
[self addObserver:self forKeyPath:@"isCancelled" options:NSKeyValueObservingOptionNew context:nil];
}
@catch (id exception) {
}
}

- (void)stopObserving {
@try {
[self removeObserver:self forKeyPath:@"isCancelled"];
}
@catch (id exception) {
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"isCancelled"]) {
if (self.resolve) {
[self.resolve cancel];
}
}
}

@end
Loading

0 comments on commit 03190d2

Please sign in to comment.