Reputation: 181
I am developing an iPhone app for my University radio station, and I would like to insert ads (that look and feel just like iAds) but with my own custom designs/content/links so I can sell this adspace to possible sponsors.
Would anyone know how I can do this or point me in the right direction? I am building a "utility" style app.
Upvotes: 3
Views: 1015
Reputation: 5687
I have a JSON file on my server with certain data about the ad (mine happens to be set up for one, but you could accomodate multiple the same way).
{"promo":"yes","imageURL":"http://somedomain/testAd.png","image2xURL":"http://somedomain/[email protected]","link":"http://www.whereTheAdShouldDirect.com"}
Then, in the app, I have this amongst the rest of viewWillAppear:
NSURL *url = [NSURL URLWithString:@"http://www.mydomain/promo.php"];
NSString *response = [[NSString alloc] initWithContentsOfURL:url];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSDictionary *promo = [responseString JSONValue];
[response release];
if([[promo objectForKey:@"promo"] isEqualToString:@"yes"]){
self.linkURL = [NSURL URLWithString:[promo objectForKey:@"link"]];
NSURL *picURL = [NSURL URLWithString:[promo objectForKey:@"imageURL"]];
if([[[UIDevice currentDevice] systemVersion]intValue]>=4){
if([[UIScreen mainScreen] scale]==2.0){
picURL = [NSURL URLWithString:[promo objectForKey:@"image2xURL"]];
}
}
CGRect imgFrame = CGRectMake(0, 0, 320, 50);
UIButton *adImage=[[UIButton alloc] initWithFrame:imgFrame];
NSData * imageData = [NSData dataWithContentsOfURL:picURL];
UIImage * image = [UIImage imageWithData:imageData];
[adImage setBackgroundImage:image forState:UIControlStateNormal];
[adImage addTarget:self action:@selector(ad) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:adImage];
[adImage release];
}
and this method as well:
-(void)ad{
[[UIApplication sharedApplication] openURL:self.linkURL];
}
You may want to change that last method depending on how you want the ad to react (load a webview right in the app?)
Upvotes: 2