Permalink
Browse files

Initial commit of AdPlugin

  • Loading branch information...
1 parent 3de1779 commit eeae2990da0512aa0907e07210951e0198bf2650 shazron committed Jul 12, 2010
@@ -0,0 +1,6 @@
+.DS_Store
+*.pbxuser
+*.perspectivev3
+*.mode1v3
+javascripts/phonegap.js
+javascripts/SAiOSAdPlugin.js
View
@@ -0,0 +1,54 @@
+# PhoneGap AdPlugin #
+by Shazron Abdullah
+
+## Adding the Plugin to your project ##
+
+Using this plugin requires [iPhone PhoneGap](http://github.com/phonegap/phonegap-iphone) and iAd. iAd requires the [iOS 4 SDK](http://developer.apple.com/iphone).
+
+1. Make sure your PhoneGap Xcode project has been [updated for the iOS 4 SDK](http://wiki.phonegap.com/Upgrade-your-PhoneGap-Xcode-Template-for-iOS-4)
+2. Add the "iAd" framework to your Frameworks folder, and set it to be weak linked (see "Weak Linking the iAd Framework" section below)
+3. Add the .h and .m files to your Plugins folder in your project
+4. Add the .js files to your "www" folder on disk, and add reference(s) to the .js files as <link> tags in your html file(s)
+5. See the sample project "iAdHost" for an example use (examine the code in www/index.html)
+6. Make sure you check the "RELEASE NOTES" section below!
+
+## Weak Linking the iAd Framework ##
+
+1. In your project, under "Targets", double click on the Target item
+2. Go to the "General" Tab, under "Linked Libraries"
+3. For the iAd Framework, change the value from "Required" to "Weak"
+
+## FUTURE FEATURES ##
+* Landscape mode iAds
+* Showing of other ad network ads in place of iAds, if no iAds are available
+
+## RELEASE NOTES ##
+
+### 20100712 ###
+* Initial release
+* Only supports portrait iAd banners
+* Not tested on a 3.x device
+* WebView initial size being oversized is a PhoneGap Core issue, and is not addressed in this plugin. When an ad is shown, it correctly sizes the WebView, and when the ad is hidden, sets the WebView back to its original size.
+* Not tested with any other native UI plugins, like TabBar, and will not play nice with them (until we get a better layout management system/plugin)
+
+## BUGS AND CONTRIBUTIONS ##
+
+Patches welcome! Send a pull request. Since this is not a part of PhoneGap Core (which requires a CLA), this should be easier.
+
+Post issues in the [PhoneGap Google Groups](http://groups.google.com/group/phonegap), include in the subject heading - "AdPlugin" or on [Github](http://github.com/shazron/PhoneGap-Plugins/issues)
+(preferred)
+
+The latest code will always be [here](http://github.com/shazron/PhoneGap-Plugins/tree/master/AdPlugin/)
+
+## LICENSE ##
+
+The MIT License
+
+Copyright (c) 2010 Shazron Abdullah
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
@@ -0,0 +1,31 @@
+//
+// SAiOSAdPlugin.h
+// ADPlugin for PhoneGap
+//
+// Created by shazron on 10-07-12.
+// Copyright 2010 Shazron Abdullah. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "PhoneGapCommand.h"
+#import <iAd/iAd.h>
+
+@interface SAiOSAdPlugin : PhoneGapCommand <ADBannerViewDelegate> {
+
+ ADBannerView* adView;
+
+ BOOL bannerIsVisible;
+ BOOL bannerIsInitialized;
+ BOOL bannerIsAtBottom;
+}
+
+@property (nonatomic, retain) ADBannerView* adView;
+@property (assign) BOOL bannerIsVisible;
+@property (assign) BOOL bannerIsInitialized;
+@property (assign) BOOL bannerIsAtBottom;
+
+
+- (void) prepare:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
+- (void) showAd:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
+
+@end
@@ -0,0 +1,43 @@
+/**
+ * Constructor
+ */
+function SAiOSAdPlugin()
+{
+}
+
+/**
+ * show - true to show the ad, false to hide the ad
+ */
+SAiOSAdPlugin.prototype.showAd = function(show)
+{
+ PhoneGap.exec("SAiOSAdPlugin.showAd", show);
+}
+
+/**
+ * atBottom - true to put the ad at the bottom, false to put the ad at the top
+ */
+SAiOSAdPlugin.prototype.prepare = function(atBottom)
+{
+ if (!atBottom) {
+ atBottom = false;
+ }
+ PhoneGap.exec("SAiOSAdPlugin.prepare", atBottom);
+}
+
+/**
+ * Install function
+ */
+SAiOSAdPlugin.install = function()
+{
+ if ( !window.plugins )
+ window.plugins = {};
+ if ( !window.plugins.iAdPlugin )
+ window.plugins.iAdPlugin = new SAiOSAdPlugin();
+
+
+}
+
+/**
+ * Add to PhoneGap constructor
+ */
+PhoneGap.addConstructor(SAiOSAdPlugin.install);
@@ -0,0 +1,160 @@
+//
+// SAiOSAdPlugin.m
+// Ad Plugin for PhoneGap
+//
+// Created by shazron on 10-07-12.
+// Copyright 2010 Shazron Abdullah. All rights reserved.
+//
+
+#import "SAiOSAdPlugin.h"
+
+@interface SAiOSAdPlugin(PrivateMethods)
+
+- (void) __prepare:(BOOL)atBottom;
+- (void) __showAd:(BOOL)show;
+
+@end
+
+
+@implementation SAiOSAdPlugin
+
+@synthesize adView;
+@synthesize bannerIsVisible, bannerIsInitialized, bannerIsAtBottom;
+
+#pragma mark -
+#pragma mark Public Methods
+
+- (void) prepare:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+{
+ NSUInteger argc = [arguments count];
+ if (argc > 1) {
+ return;
+ }
+
+ NSString* atBottomValue = [arguments objectAtIndex:0];
+ [self __prepare:[atBottomValue boolValue]];
+}
+
+- (void) showAd:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+{
+ NSUInteger argc = [arguments count];
+ if (argc > 1) {
+ return;
+ }
+
+ NSString* showValue = [arguments objectAtIndex:0];
+ [self __showAd:[showValue boolValue]];
+}
+
+#pragma mark -
+#pragma mark Private Methods
+
+- (void) __prepare:(BOOL)atBottom
+{
+ NSLog(@"SAiOSAdPlugin Prepare Ad At Bottom: %d", atBottom);
+
+ Class adBannerViewClass = NSClassFromString(@"ADBannerView");
+ if (adBannerViewClass && !self.adView)
+ {
+ self.adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
+ self.adView.requiredContentSizeIdentifiers = [NSSet setWithObjects: ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];
+ self.adView.delegate = self;
+
+ CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
+ if (atBottom) {
+ CGRect adViewFrame = self.adView.frame;
+ adViewFrame.origin.y = [UIScreen mainScreen].bounds.size.height - statusBarHeight - adViewFrame.size.height;
+ self.adView.frame = adViewFrame;
+
+ self.bannerIsAtBottom = YES;
+ }
+
+ self.bannerIsVisible = NO;
+ self.bannerIsInitialized = YES;
+ }
+}
+
+- (void) __showAd:(BOOL)show
+{
+ NSLog(@"SAiOSAdPlugin Show Ad: %d", show);
+
+ if (!self.bannerIsInitialized){
+ [self __prepare:NO];
+ }
+
+ if (!(NSClassFromString(@"ADBannerView") && self.adView)) { // ad classes not available
+ return;
+ }
+
+ if (show == self.bannerIsVisible) { // same state, nothing to do
+ return;
+ }
+
+ CGRect adViewFrame = self.adView.frame;
+ CGRect webViewFrame = [super webView].frame;
+ CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
+
+ if (show)
+ {
+ if (self.bannerIsAtBottom)
+ {
+ webViewFrame.size.height -= (adViewFrame.size.height + statusBarHeight);
+ }
+ else
+ {
+ webViewFrame.origin.y += adViewFrame.size.height;
+ webViewFrame.size.height -= (adViewFrame.size.height + statusBarHeight);
+ }
+
+ [UIView beginAnimations:@"blah" context:NULL];
+ [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
+
+ [super webView].frame = webViewFrame;
+ [[[super webView] superview] addSubview:self.adView];
+
+ [UIView commitAnimations];
+
+ self.bannerIsVisible = YES;
+ }
+ else
+ {
+ if (self.bannerIsAtBottom)
+ {
+ webViewFrame.size.height += (adViewFrame.size.height + statusBarHeight);
+ }
+ else
+ {
+ webViewFrame.origin.y -= adViewFrame.size.height;
+ webViewFrame.size.height += (adViewFrame.size.height + statusBarHeight);
+ }
+
+ [UIView beginAnimations:@"blah" context:NULL];
+ [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
+
+ [super webView].frame = webViewFrame;
+ [self.adView removeFromSuperview];
+
+ [UIView commitAnimations];
+
+ self.bannerIsVisible = NO;
+ }
+
+}
+
+#pragma mark -
+#pragma ADBannerViewDelegate
+
+- (void)bannerViewDidLoadAd:(ADBannerView *)banner
+{
+ Class adBannerViewClass = NSClassFromString(@"ADBannerView");
+ if (adBannerViewClass)
+ {
+ NSString* jsString =
+ @"var e = document.createEvent('Events');"
+ "e.initEvent('iAdBannerViewDidLoadAdEvent');"
+ "document.dispatchEvent(e);";
+ [super writeJavascript:jsString];
+ }
+}
+
+@end
@@ -0,0 +1,16 @@
+//
+// iAdHostAppDelegate.h
+// iAdHost
+//
+// Created by shazron on 10-07-12.
+// Copyright __MyCompanyName__ 2010. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "PhoneGapDelegate.h"
+
+@interface iAdHostAppDelegate : PhoneGapDelegate {
+}
+
+@end
+
@@ -0,0 +1,81 @@
+//
+// iAdHostAppDelegate.m
+// iAdHost
+//
+// Created by shazron on 10-07-12.
+// Copyright __MyCompanyName__ 2010. All rights reserved.
+//
+
+#import "iAdHostAppDelegate.h"
+#import "PhoneGapViewController.h"
+
+@implementation iAdHostAppDelegate
+
+- (id) init
+{
+ /** If you need to do any extra app-specific initialization, you can do it here
+ * -jm
+ **/
+ return [super init];
+}
+
+/**
+ * This is main kick off after the app inits, the views and Settings are setup here.
+ */
+- (void)applicationDidFinishLaunching:(UIApplication *)application
+{
+ [ super applicationDidFinishLaunching:application ];
+}
+
+-(id) getCommandInstance:(NSString*)className
+{
+ /** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
+ * own app specific protocol to it. -jm
+ **/
+ return [super getCommandInstance:className];
+}
+
+/**
+ Called when the webview finishes loading. This stops the activity view and closes the imageview
+ */
+- (void)webViewDidFinishLoad:(UIWebView *)theWebView
+{
+ return [ super webViewDidFinishLoad:theWebView ];
+}
+
+- (void)webViewDidStartLoad:(UIWebView *)theWebView
+{
+ return [ super webViewDidStartLoad:theWebView ];
+}
+
+/**
+ * Fail Loading With Error
+ * Error - If the webpage failed to load display an error with the reson.
+ */
+- (void)webView:(UIWebView *)theWebView didFailLoadWithError:(NSError *)error
+{
+ return [ super webView:theWebView didFailLoadWithError:error ];
+}
+
+/**
+ * Start Loading Request
+ * This is where most of the magic happens... We take the request(s) and process the response.
+ * From here we can re direct links and other protocalls to different internal methods.
+ */
+- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
+{
+ return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
+}
+
+
+- (BOOL) execute:(InvokedUrlCommand*)command
+{
+ return [ super execute:command];
+}
+
+- (void)dealloc
+{
+ [ super dealloc ];
+}
+
+@end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit eeae299

Please sign in to comment.