3
我正在爲我的大學廣播電臺開發一個iPhone應用程序,並且我想插入廣告(外觀和感覺就像iAds),但使用我自己的定製設計/內容/鏈接,因此我可以將此廣告空間出售給可能的贊助商。
有人會知道我該如何做到這一點,或指出我在正確的方向嗎?我正在構建一個「實用」風格的應用程序。如何在我的iPhone應用程序中創建自定義廣告?
我正在爲我的大學廣播電臺開發一個iPhone應用程序,並且我想插入廣告(外觀和感覺就像iAds),但使用我自己的定製設計/內容/鏈接,因此我可以將此廣告空間出售給可能的贊助商。
有人會知道我該如何做到這一點,或指出我在正確的方向嗎?我正在構建一個「實用」風格的應用程序。如何在我的iPhone應用程序中創建自定義廣告?
我的服務器上有一個關於廣告的特定數據的JSON文件(我的設置只適用於一個,但您可以以同樣的方式容納多個)。
{"promo":"yes","imageURL":"http://somedomain/testAd.png","image2xURL":"http://somedomain/[email protected]","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];
}
您可能需要改變過去的方法取決於你希望廣告反應的方式(在應用中加載webview權利?)
非常感謝你的回答。 我遇到了JSON部分的問題。我如何把這個文件放在我的服務器上? 另存爲.json?或者我把.json文件放在PhP中,然後保存爲.php? – 2012-01-15 02:24:19
這取決於你的數據是如何保存的......如果你從數據庫中提取(也許是因爲你有很多廣告),我會用php來查詢數據庫,然後回顯JSON編碼的數據。如果它只有一個很少更改的廣告,則可以使用靜態文本文件 – 2012-01-15 12:43:29