From 0586af9c9ccf8d37a02dea8c843eeda51882a7f9 Mon Sep 17 00:00:00 2001 From: finscn Date: Fri, 7 Mar 2014 17:34:21 +0800 Subject: [PATCH 1/8] add some features --- Source/Ejecta/EJUtils/EJBindingAdBanner.h | 4 +- Source/Ejecta/EJUtils/EJBindingAdBanner.m | 155 ++++++++++++++++++---- 2 files changed, 133 insertions(+), 26 deletions(-) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.h b/Source/Ejecta/EJUtils/EJBindingAdBanner.h index 6f6557a7..3356f929 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.h +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.h @@ -5,7 +5,9 @@ @interface EJBindingAdBanner : EJBindingEventedBase { ADBannerView *banner; - BOOL isAtBottom, wantsToShow, isReady; + BOOL wantsToShow, isReady; + BOOL isAtBottom, isAtRight, alwaysPortrait; + short x, y; } @end diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.m b/Source/Ejecta/EJUtils/EJBindingAdBanner.m index ab2f57dc..04e88d6d 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.m +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.m @@ -5,28 +5,60 @@ @implementation EJBindingAdBanner - (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view { [super createWithJSObject:obj scriptView:view]; - + isAtBottom = NO; + isAtRight = NO; wantsToShow = NO; isReady = NO; - + alwaysPortrait = NO; + x = 0; + y = 0; + banner = [[ADBannerView alloc] initWithFrame:CGRectZero]; banner.delegate = self; banner.hidden = YES; - - BOOL landscape = [[[NSBundle mainBundle] infoDictionary][@"UIInterfaceOrientation"] - hasPrefix:@"UIInterfaceOrientationLandscape"]; - - banner.requiredContentSizeIdentifiers = [NSSet setWithObjects: - (landscape - ? ADBannerContentSizeIdentifierLandscape - : ADBannerContentSizeIdentifierPortrait), - nil]; - + + [self doLayout]; [scriptView addSubview:banner]; NSLog(@"AdBanner: init at y %f", banner.frame.origin.y); } +- (void)doLayout { + short w = 0, h = 0; + BOOL landscape = false; + CGRect screenRect = [[UIScreen mainScreen] bounds]; + if (alwaysPortrait) { + landscape = false; + w = screenRect.size.width; + h = screenRect.size.height; + } + else { + landscape = [[[NSBundle mainBundle] infoDictionary][@"UIInterfaceOrientation"] + hasPrefix:@"UIInterfaceOrientationLandscape"]; + w = scriptView.bounds.size.width; + h = scriptView.bounds.size.height; + } + + banner.requiredContentSizeIdentifiers = [NSSet setWithObjects: + (landscape + ? ADBannerContentSizeIdentifierPortrait + : ADBannerContentSizeIdentifierLandscape), + nil]; + banner.currentContentSizeIdentifier = (landscape + ? ADBannerContentSizeIdentifierPortrait + : ADBannerContentSizeIdentifierLandscape); + CGRect rect = CGRectMake(x, y, w, h); + CGSize adSize = [banner sizeThatFits:rect.size]; + [banner setFrame:CGRectMake(x, y, adSize.width, adSize.height)]; +} + +- (void)doLocate { + CGRect frame = banner.frame; + frame.origin.x = x; + frame.origin.y = y; + banner.frame = frame; +} + - (void)dealloc { [banner removeFromSuperview]; [banner release]; @@ -36,7 +68,7 @@ - (void)dealloc { - (void)bannerViewDidLoadAd:(ADBannerView *)theBanner { NSLog(@"AdBanner: Ad loaded"); isReady = YES; - if( wantsToShow ) { + if (wantsToShow) { [scriptView bringSubviewToFront:banner]; banner.hidden = NO; } @@ -44,42 +76,115 @@ - (void)bannerViewDidLoadAd:(ADBannerView *)theBanner { } - (void)bannerView:(ADBannerView *)theBanner didFailToReceiveAdWithError:(NSError *)error { - NSLog(@"AdBanner: Failed to receive Ad. Error: %d - %@", error.code, error.localizedDescription); + NSLog(@"AdBanner: Failed to receive Ad. Error: %ld - %@", (long)error.code, error.localizedDescription); [self triggerEvent:@"error"]; banner.hidden = YES; } +EJ_BIND_GET(isReady, ctx) +{ + return JSValueMakeBoolean(ctx, isReady); +} -EJ_BIND_GET( isAtBottom, ctx ) { +EJ_BIND_GET(isAtBottom, ctx) +{ return JSValueMakeBoolean(ctx, isAtBottom); } -EJ_BIND_SET( isAtBottom, ctx, value ) { +EJ_BIND_SET(isAtBottom, ctx, value) +{ isAtBottom = JSValueToBoolean(ctx, value); - - CGRect frame = banner.frame; - frame.origin.y = isAtBottom - ? scriptView.bounds.size.height - frame.size.height + y = isAtBottom + ? scriptView.bounds.size.height - banner.frame.size.height : 0; - - banner.frame = frame; + [self doLocate]; } -EJ_BIND_FUNCTION(hide, ctx, argc, argv ) { + + +EJ_BIND_GET(isAtRight, ctx) +{ + return JSValueMakeBoolean(ctx, isAtRight); +} + +EJ_BIND_SET(isAtRight, ctx, value) +{ + isAtRight = JSValueToBoolean(ctx, value); + x = isAtRight + ? scriptView.bounds.size.width - banner.frame.size.width + : 0; + [self doLocate]; +} + +EJ_BIND_FUNCTION(hide, ctx, argc, argv) +{ banner.hidden = YES; wantsToShow = NO; return NULL; } -EJ_BIND_FUNCTION(show, ctx, argc, argv ) { +EJ_BIND_FUNCTION(show, ctx, argc, argv) +{ wantsToShow = YES; - if( isReady ) { + if (isReady) { [scriptView bringSubviewToFront:banner]; banner.hidden = NO; } return NULL; } +EJ_BIND_GET(alwaysPortrait, ctx) +{ + return JSValueMakeBoolean(ctx, alwaysPortrait); +} + +EJ_BIND_SET(alwaysPortrait, ctx, value) +{ + alwaysPortrait = JSValueToBoolean(ctx, value); + + [self doLayout]; +} + + +EJ_BIND_GET(x, ctx) +{ + return JSValueMakeNumber(ctx, x); +} + +EJ_BIND_SET(x, ctx, value) +{ + short newX = JSValueToNumberFast(ctx, value); + if (newX != x) { + x = newX; + CGRect frame = banner.frame; + frame.origin.x = x; + } +} + +EJ_BIND_GET(y, ctx) +{ + return JSValueMakeNumber(ctx, y); +} + +EJ_BIND_SET(y, ctx, value) +{ + short newY = JSValueToNumberFast(ctx, value); + if (newY != y) { + y = newY; + CGRect frame = banner.frame; + frame.origin.y = y; + } +} + +EJ_BIND_GET(width, ctx) +{ + return JSValueMakeNumber(ctx, banner.frame.size.width); +} +EJ_BIND_GET(height, ctx) +{ + return JSValueMakeNumber(ctx, banner.frame.size.height); +} + EJ_BIND_EVENT(load); EJ_BIND_EVENT(error); From e03902b34d8d1445766c8cbd33c8bf48b4e3fad2 Mon Sep 17 00:00:00 2001 From: finscn Date: Fri, 7 Mar 2014 17:42:51 +0800 Subject: [PATCH 2/8] Update EJBindingAdBanner.m change false to NO for Ejecta's style --- Source/Ejecta/EJUtils/EJBindingAdBanner.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.m b/Source/Ejecta/EJUtils/EJBindingAdBanner.m index 04e88d6d..f40e2a13 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.m +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.m @@ -25,10 +25,10 @@ - (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view - (void)doLayout { short w = 0, h = 0; - BOOL landscape = false; + BOOL landscape = NO; CGRect screenRect = [[UIScreen mainScreen] bounds]; if (alwaysPortrait) { - landscape = false; + landscape = NO; w = screenRect.size.width; h = screenRect.size.height; } From 661ca81902f39853ede43a812d23ee5fec0f6bb0 Mon Sep 17 00:00:00 2001 From: finscn Date: Fri, 7 Mar 2014 18:35:43 +0800 Subject: [PATCH 3/8] Update EJBindingAdBanner.h supports "ADAdTypeMediumRectangle" --- Source/Ejecta/EJUtils/EJBindingAdBanner.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.h b/Source/Ejecta/EJUtils/EJBindingAdBanner.h index 3356f929..d84362db 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.h +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.h @@ -8,6 +8,8 @@ BOOL wantsToShow, isReady; BOOL isAtBottom, isAtRight, alwaysPortrait; short x, y; + BOOL isRectangle; + NSString *type; } @end From 43cb0b71d39420225a3ae98d080c9b35ed15dfd2 Mon Sep 17 00:00:00 2001 From: finscn Date: Fri, 7 Mar 2014 18:37:05 +0800 Subject: [PATCH 4/8] Update EJBindingAdBanner.m supports "ADAdTypeMediumRectangle" --- Source/Ejecta/EJUtils/EJBindingAdBanner.m | 55 ++++++++++++++++++----- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.m b/Source/Ejecta/EJUtils/EJBindingAdBanner.m index f40e2a13..986278a2 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.m +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.m @@ -3,6 +3,18 @@ @implementation EJBindingAdBanner +- (id)initWithContext:(JSContextRef)ctx argc:(size_t)argc argv:(const JSValueRef[])argv { + if (self = [super initWithContext:ctx argc:argc argv:argv]) { + if (argc > 0) { + type = [JSValueToNSString(ctx, argv[0]) retain]; + if (type) { + type = [type lowercaseString]; + } + } + } + return self; +} + - (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view { [super createWithJSObject:obj scriptView:view]; @@ -13,14 +25,25 @@ - (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view alwaysPortrait = NO; x = 0; y = 0; + banner = nil; + isRectangle = NO; + + if ([type isEqualToString:@"rect"] || [type isEqualToString:@"rectangle"] || [type isEqualToString:@"mediumrectangle"]) { + banner = [[ADBannerView alloc] initWithAdType:ADAdTypeMediumRectangle]; + } + if (!banner) { + // Not ADAdTypeMediumRectangle or iOS can't support ADAdTypeMediumRectangle + banner = [[ADBannerView alloc] initWithFrame:CGRectZero]; + } + else { + isRectangle = YES; + } - banner = [[ADBannerView alloc] initWithFrame:CGRectZero]; banner.delegate = self; banner.hidden = YES; [self doLayout]; [scriptView addSubview:banner]; - NSLog(@"AdBanner: init at y %f", banner.frame.origin.y); } - (void)doLayout { @@ -39,14 +62,16 @@ - (void)doLayout { h = scriptView.bounds.size.height; } - banner.requiredContentSizeIdentifiers = [NSSet setWithObjects: - (landscape - ? ADBannerContentSizeIdentifierPortrait - : ADBannerContentSizeIdentifierLandscape), - nil]; - banner.currentContentSizeIdentifier = (landscape - ? ADBannerContentSizeIdentifierPortrait - : ADBannerContentSizeIdentifierLandscape); + if (!isRectangle) { + banner.requiredContentSizeIdentifiers = [NSSet setWithObjects: + (landscape + ? ADBannerContentSizeIdentifierPortrait + : ADBannerContentSizeIdentifierLandscape), + nil]; + banner.currentContentSizeIdentifier = (landscape + ? ADBannerContentSizeIdentifierPortrait + : ADBannerContentSizeIdentifierLandscape); + } CGRect rect = CGRectMake(x, y, w, h); CGSize adSize = [banner sizeThatFits:rect.size]; [banner setFrame:CGRectMake(x, y, adSize.width, adSize.height)]; @@ -116,6 +141,12 @@ - (void)bannerView:(ADBannerView *)theBanner didFailToReceiveAdWithError:(NSErro [self doLocate]; } + +EJ_BIND_GET(isRectangle, ctx) +{ + return JSValueMakeBoolean(ctx, isRectangle); +} + EJ_BIND_FUNCTION(hide, ctx, argc, argv) { banner.hidden = YES; @@ -184,6 +215,10 @@ - (void)bannerView:(ADBannerView *)theBanner didFailToReceiveAdWithError:(NSErro { return JSValueMakeNumber(ctx, banner.frame.size.height); } +EJ_BIND_GET(type, ctx) +{ + return NSStringToJSValue(ctx, type); +} EJ_BIND_EVENT(load); EJ_BIND_EVENT(error); From 4a7297df964d23c2d844512a62234f20e177f81c Mon Sep 17 00:00:00 2001 From: finscn Date: Fri, 7 Mar 2014 18:39:39 +0800 Subject: [PATCH 5/8] Update EJBindingAdBanner.m --- Source/Ejecta/EJUtils/EJBindingAdBanner.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.m b/Source/Ejecta/EJUtils/EJBindingAdBanner.m index 986278a2..f469e5c4 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.m +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.m @@ -37,6 +37,7 @@ - (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view } else { isRectangle = YES; + alwaysPortrait = NO; } banner.delegate = self; @@ -126,7 +127,6 @@ - (void)bannerView:(ADBannerView *)theBanner didFailToReceiveAdWithError:(NSErro } - EJ_BIND_GET(isAtRight, ctx) { return JSValueMakeBoolean(ctx, isAtRight); @@ -171,6 +171,9 @@ - (void)bannerView:(ADBannerView *)theBanner didFailToReceiveAdWithError:(NSErro EJ_BIND_SET(alwaysPortrait, ctx, value) { + if (isRectangle) { + return; + } alwaysPortrait = JSValueToBoolean(ctx, value); [self doLayout]; From 7cfcddec37e4ee8fd289e27713eef772d0f3c6ec Mon Sep 17 00:00:00 2001 From: finscn Date: Fri, 7 Mar 2014 18:53:50 +0800 Subject: [PATCH 6/8] Update EJBindingAdBanner.m add try...catch for rect-iAd --- Source/Ejecta/EJUtils/EJBindingAdBanner.m | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.m b/Source/Ejecta/EJUtils/EJBindingAdBanner.m index f469e5c4..541ba81b 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.m +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.m @@ -29,10 +29,15 @@ - (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view isRectangle = NO; if ([type isEqualToString:@"rect"] || [type isEqualToString:@"rectangle"] || [type isEqualToString:@"mediumrectangle"]) { - banner = [[ADBannerView alloc] initWithAdType:ADAdTypeMediumRectangle]; + @try { + banner = [[ADBannerView alloc] initWithAdType:ADAdTypeMediumRectangle]; + } + @catch (NSException *exception) + { + NSLog(@"Current iOS version doesn't supports iAd with ADAdTypeMediumRectangle"); + } } if (!banner) { - // Not ADAdTypeMediumRectangle or iOS can't support ADAdTypeMediumRectangle banner = [[ADBannerView alloc] initWithFrame:CGRectZero]; } else { From 8deffb52183324e4c4ff2723cb3d8cd02e1ead71 Mon Sep 17 00:00:00 2001 From: finscn Date: Fri, 14 Mar 2014 14:25:23 +0800 Subject: [PATCH 7/8] Update EJBindingAdBanner.m --- Source/Ejecta/EJUtils/EJBindingAdBanner.m | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.m b/Source/Ejecta/EJUtils/EJBindingAdBanner.m index 541ba81b..e2066aba 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.m +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.m @@ -68,16 +68,17 @@ - (void)doLayout { h = scriptView.bounds.size.height; } + if (!isRectangle) { if (!isRectangle) { banner.requiredContentSizeIdentifiers = [NSSet setWithObjects: (landscape - ? ADBannerContentSizeIdentifierPortrait - : ADBannerContentSizeIdentifierLandscape), + ? ADBannerContentSizeIdentifierLandscape + : ADBannerContentSizeIdentifierPortrait), nil]; banner.currentContentSizeIdentifier = (landscape - ? ADBannerContentSizeIdentifierPortrait - : ADBannerContentSizeIdentifierLandscape); - } + ? ADBannerContentSizeIdentifierLandscape + : ADBannerContentSizeIdentifierPortrait); + } } CGRect rect = CGRectMake(x, y, w, h); CGSize adSize = [banner sizeThatFits:rect.size]; [banner setFrame:CGRectMake(x, y, adSize.width, adSize.height)]; From c48c462babeb1c0daa65b2845d1b21cf6f3a4e49 Mon Sep 17 00:00:00 2001 From: finscn Date: Wed, 26 Mar 2014 01:53:05 +0800 Subject: [PATCH 8/8] Update EJBindingAdBanner.m add click and finsih event, for tracking user's click-life --- Source/Ejecta/EJUtils/EJBindingAdBanner.m | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Source/Ejecta/EJUtils/EJBindingAdBanner.m b/Source/Ejecta/EJUtils/EJBindingAdBanner.m index e2066aba..e44a391f 100644 --- a/Source/Ejecta/EJUtils/EJBindingAdBanner.m +++ b/Source/Ejecta/EJUtils/EJBindingAdBanner.m @@ -47,7 +47,6 @@ - (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view banner.delegate = self; banner.hidden = YES; - [self doLayout]; [scriptView addSubview:banner]; } @@ -68,17 +67,17 @@ - (void)doLayout { h = scriptView.bounds.size.height; } - if (!isRectangle) { if (!isRectangle) { banner.requiredContentSizeIdentifiers = [NSSet setWithObjects: (landscape ? ADBannerContentSizeIdentifierLandscape - : ADBannerContentSizeIdentifierPortrait), + : ADBannerContentSizeIdentifierPortrait), nil]; banner.currentContentSizeIdentifier = (landscape ? ADBannerContentSizeIdentifierLandscape - : ADBannerContentSizeIdentifierPortrait); - } } + : ADBannerContentSizeIdentifierPortrait); + } + CGRect rect = CGRectMake(x, y, w, h); CGSize adSize = [banner sizeThatFits:rect.size]; [banner setFrame:CGRectMake(x, y, adSize.width, adSize.height)]; @@ -113,6 +112,15 @@ - (void)bannerView:(ADBannerView *)theBanner didFailToReceiveAdWithError:(NSErro banner.hidden = YES; } +- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { + [self triggerEvent:@"click"]; + return YES; +} + +- (void)bannerViewActionDidFinish:(ADBannerView *)banner { + [self triggerEvent:@"finish"]; +} + EJ_BIND_GET(isReady, ctx) { return JSValueMakeBoolean(ctx, isReady); @@ -231,5 +239,7 @@ - (void)bannerView:(ADBannerView *)theBanner didFailToReceiveAdWithError:(NSErro EJ_BIND_EVENT(load); EJ_BIND_EVENT(error); +EJ_BIND_EVENT(click); +EJ_BIND_EVENT(finish); @end