2012-06-21 4 views
5

iAds.The iAdsを管理するためのシングルトンクラスを作成しました.5秒間ユーザーがいなくてもポップアップします。 idleTimerExceeded呼び出しは、iAdを表示するための通知を生成します。このコードは私の要件にはうまくいきますが、iOS開発に慣れていないので、このコードを統合した後でアプリケーションが予期せずハングアップすることがあります。このコードは、メモリやパフォーマンスの面で私のコードを最適化したいと警告などの多くが発生します。iAds for iPhoneを表示するためのシングルトンクラス

あなたのご提案やレビューに非常に感謝しています。これは、何が必要です

iAdSingleton.h

#import <Foundation/Foundation.h> 
#import "AppDelegate.h" 
#import "iAd/iAd.h" 

@interface iAdSingleton : UIViewController<ADBannerViewDelegate> { 
    ADBannerView *adView; 
    UIViewController *displayVC; 
    NSTimer *idleTimer; 
    BOOL isItFirstTime; 
} 
@property (nonatomic, retain) ADBannerView *adView; 
@property (nonatomic, retain) UIViewController *displayVC; 
@property (nonatomic) BOOL isItFirstTime; 

+ (id) shareAdSingleton; 
- (void) resetIdleTimer; 
- (void) idleTimerExceeded; 

@end 

iAdSingleton.m

#import "iAdSingleton.h" 

@implementation iAdSingleton 

static iAdSingleton* _sharedAdSingleton = nil; 

BOOL bannerVisible = NO; 
BOOL controlAccessBannerVisibility = NO; 
@synthesize adView, displayVC; 
@synthesize isItFirstTime; 

#define kMaxIdleTimeSeconds 5.0 

+(id)sharedAdSingleton 
{ 
    @synchronized(self) 
    { 
     if(!_sharedAdSingleton) 
      _sharedAdSingleton = [[self alloc] init]; 
     return _sharedAdSingleton; 
    } 
    return nil; 
} 

+(id)alloc 
{ 
    @synchronized([iAdSingleton class]) 
    { 
     NSAssert(_sharedAdSingleton == nil, @"Attempted to allocate a second instance of a singleton."); 
     _sharedAdSingleton = [super alloc]; 
     return _sharedAdSingleton; 
    } 

    return nil; 
} 

-(id)init 
{ 
    self = [super init]; 
    if (self != nil) { 

    /*     Initialize The Parameters Over Here     */ 

     //adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 480, 0, 0)]; 
     adView = [[ADBannerView alloc] init]; 
     adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; 
     self.adView.delegate=self; 
     [self resetIdleTimer]; 
    } 
    return self; 
} 

-(void)dealloc 
{ 
    displayVC = nil; 
    if (adView) { 
     [adView removeFromSuperview]; //Remove ad view from superview 
     [adView setDelegate:nil]; 
     adView = nil; 
    } 
    [super dealloc]; 
} 

-(UIViewController *)viewControllerForPresentingModalView 
{ 
    return displayVC; 
} 

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{ 
    banner.hidden = NO; 
    if(!bannerVisible){ 
     NSLog(@"Banner Changes 1 - Purpose: Visibility"); 
     // [UIView beginAnimations:@"bannerAppear" context:NULL]; 
     // banner.frame = CGRectOffset(banner.frame, 0, -100); 
     // [UIView commitAnimations]; 
     bannerVisible = YES; 
     controlAccessBannerVisibility = YES; 

    } 
} 

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error 
{ 
    //NSLog(@"Unable to receive Ad."); 
    NSLog(@"Banner Changes 2 - Purpose: Unable to Receive Ad."); 
    banner.hidden = YES; 
    if(bannerVisible){ 
     [UIView beginAnimations:@"bannerDisappear" context:NULL]; 
     banner.frame = CGRectOffset(banner.frame, 0, 100); 
     [UIView commitAnimations]; 
     bannerVisible = NO; 
    } 
} 

- (BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave 
{ 
    NSLog(@"Pause anything necessary"); 
    return YES; 
} 

- (void) bannerViewActionDidFinish:(ADBannerView *)banner 
{ 
    NSLog(@"We now resume to normal operations"); 
} 

- (void)resetIdleTimer { 

    if (!idleTimer) { 
     idleTimer = [[NSTimer scheduledTimerWithTimeInterval:kMaxIdleTimeSeconds 
                 target:self 
                selector:@selector(idleTimerExceeded) 
                userInfo:nil 
                repeats:NO] retain]; 
    } 
    else { 
     if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < kMaxIdleTimeSeconds-1.0) { 
      [idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kMaxIdleTimeSeconds]]; 
      /* 
      Notification: HideAd 
      */ 

      NSLog(@"Notification Generated For HideAd"); 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"HideAdBanner" object:nil userInfo:nil]; 

     } 
    } 
} 

- (void)idleTimerExceeded { 

    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (appDel.adVisible == NO) { 
     NSLog(@"Notification Generated For ShowAd"); 

     /* 
     Notification: ShowAd 
     */ 

     if (controlAccessBannerVisibility == YES) { 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"ShowAdBanner" object:nil userInfo:nil]; 
     } 
    } 
} 

@end 
+0

私はiOS 5で開発中で、xcode 4.2を使用しています。 – muneikh

+0

iAdシングルトンが作成されると、それはアプリケーションの残りの期間生き続けますか?または、それをリリースして後でもう一度作成しますか? – Malcolm

+0

はい、アプリケーションライフのためにiAdのインスタンスを1つ作成しようとしています。 – muneikh

答えて

3

以下は私のコードです。このコードはスレッドセーフであり、メモリの問題や警告もありません。

+ (iAdSingleton *) sharedInstance 
{ 
    static dispatch_once_t onceToken; 
    static iAdSingleton * __sharedInstance = nil; 

    dispatch_once(&onceToken, ^{ 
     __sharedInstance = [[self alloc] init]; 
    }); 

    return __sharedInstance; 
} 
+0

ありがとう、1つの簡単な質問。 Tab BarアプリケーションでiAdを管理するための最良の方法は何ですか? – muneikh

+0

あなたはどのような意味で管理していますか? – Kuldeep

+2

iAdを統合した後、アプリケーションのパフォーマンスが少し低下し、タブ間の切り替えに時間がかかることがありました。 – muneikh

関連する問題