我该如何在我的iPhone应用中创建自定义广告?

3

我正在为我的大学广播电台开发一款iPhone应用程序,并希望插入广告(外观和感觉与iAds完全相同),但使用自己的定制设计/内容/链接,以便将此广告空间出售给潜在赞助商。
有谁知道我该如何做或指导我正确的方向?我正在构建一个“实用”风格的应用程序。

1个回答

2

我在服务器上有一个JSON文件,其中包含广告的特定数据(我的是为一个广告设置的,但你可以通过相同的方式适应多个广告)。

{"promo":"yes","imageURL":"http://somedomain/testAd.png","image2xURL":"http://somedomain/testAd@2x.png","link":"http://www.whereTheAdShouldDirect.com"}

然后,在应用程序中,我在其他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];
}

还有这个方法:

-(void)ad{
    [[UIApplication sharedApplication] openURL:self.linkURL];
}

根据您对广告的反应方式(是否在应用内加载Web视图?),您可能需要更改最后一种方法。


非常感谢你的回答。我在JSON部分出现了问题。我该如何将这个文件放到我的服务器上呢?是保存为.json文件吗?还是将.json文件放在PhP中,然后保存为.php文件? - Jonathan Safa
这取决于你的数据是如何保存的...如果你正在从数据库中提取数据(可能是因为你有很多广告),那么我会使用PHP来查询数据库,然后输出JSON编码的数据。如果只有一个广告很少更改,你可以使用静态文本文件。 - Robot Woods

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接