Created
October 9, 2012 14:35
-
-
Save wellle/3859223 to your computer and use it in GitHub Desktop.
A simple pseudo advertising banner view that illustrate how tracking could be handled in combination with the SKProductViewController in iOS6.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| // We need store kit to use the product view controller. | |
| #import <StoreKit/StoreKit.h> | |
| // This is a minimal example of a pseudo advertising banner view. | |
| // It is meant to illustrate how tracking could be handled in | |
| // combination with the new product view controller in iOS6. The main | |
| // mechanism is the callback to a callback URL with an advertising ID. | |
| @interface AdView : UIView <SKStoreProductViewControllerDelegate> | |
| // The view controller that presents the product view controller. | |
| @property (nonatomic, retain) UIViewController *parentViewController; | |
| // The product ID needed for the product view controller. This might be an app ID. | |
| @property (nonatomic, copy) NSString *productId; | |
| // The URL that gets called when the product view controller is presented. | |
| // This could be used for tracking and possibly for affiliate programs. | |
| @property (nonatomic, copy) NSString *callbackUrl; | |
| // To enable tracking we send the identifierForAdvertising as a callback parameter. | |
| @property (nonatomic, copy) NSString *advertisingId; | |
| // This initializer sets the parentViewController. | |
| - (id)initWithFrame:(CGRect)frame viewController:(UIViewController *)viewController; | |
| // This method loads an ad and populates the corresponding productId and callbackUrl. | |
| - (void)loadAd; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "Adview.h" | |
| // In this example we use AFNetworking to make the callback. | |
| #import "AFNetworking.h" | |
| // Used to retrieve the identifier for advertising. | |
| #import <AdSupport/AdSupport.h> | |
| // The most important part of the implementation is the first method which handles a click. | |
| // The rest of the methods illustrate how this pseudo banner view works. | |
| @implementation AdView | |
| // This method gets called when somebody touched the adView. In that case we'll present the | |
| // product view controller and make the callback. | |
| - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { | |
| // Prepare the product view controller by providing the product ID. | |
| SKStoreProductViewController *productViewController = [[[SKStoreProductViewController alloc] init] autorelease]; | |
| productViewController.delegate = self; | |
| NSDictionary *storeParameters = [NSDictionary dictionaryWithObject:self.productId forKey:SKStoreProductParameterITunesItemIdentifier]; | |
| // Try to load the product and present the product view controller in case of success. | |
| [productViewController loadProductWithParameters:storeParameters completionBlock:^(BOOL result, NSError *error) { | |
| if (!result) { | |
| NSLog(@"Failed to load product: %@", error); | |
| } else { | |
| [self.parentViewController presentViewController:productViewController animated:YES completion:^(void) { | |
| NSLog(@"Presented product with ID: %@", self.productId); | |
| }]; | |
| } | |
| }]; | |
| // Prepare the callback request using the callback URL and the advertising ID. | |
| NSURL *baseUrl = [NSURL URLWithString:self.callbackUrl]; | |
| AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:baseUrl]; | |
| NSDictionary *httpParameters = [NSDictionary dictionaryWithObject:self.advertisingId forKey:@"advertising_id"]; | |
| // Actually make the callback. | |
| [httpClient getPath:@"" parameters:httpParameters success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
| NSLog(@"Finished callback: %@", operation.request.URL); | |
| } failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
| NSLog(@"Failed callback: %@", error); | |
| }]; | |
| } | |
| // SKStoreProductViewControllerDelegate method that dismisses the product view controller | |
| - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController { | |
| [self.parentViewController dismissViewControllerAnimated:YES completion:^{ | |
| NSLog(@"Dismissed product view controller."); | |
| }]; | |
| } | |
| // This initialiser sets the parentViewController. | |
| - (id)initWithFrame:(CGRect)frame viewController:(UIViewController *)viewController { | |
| if ((self = [super initWithFrame:frame])) { | |
| self.parentViewController = viewController; | |
| } | |
| return self; | |
| } | |
| - (void)loadAd { | |
| // This method should load and display an actual ad and set the productId and callback URL. | |
| // For the purpose of this demonstration we will mock this by setting hardcoded values. | |
| self.productId = @"315215396"; | |
| self.callbackUrl = @"http://www.example.com/callback"; | |
| self.advertisingId = [ASIdentifierManager sharedManager].advertisingIdentifier.UUIDString; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment