2012-06-21 25 views
5

我已經編寫了用於管理iAd的單身人員類。用戶不活動5秒後,iAds彈出。 idleTimerExceeded調用生成通知以顯示iAd。此代碼適用於我的需求,但由於我是iOS開發新手,我的應用程序在集成此代碼後有時會意外掛起。此代碼導致許多警告等。我想優化我的代碼在內存和性能方面。用於顯示iPhone的iAds的單身類

我會非常感謝您的建議和意見。

下面是我的代碼:

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實例。 – muneikh

回答

3

這是你所需要的。此代碼是線程安全的,並且不會有任何內存問題和警告。

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

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

    return __sharedInstance; 
} 
+0

謝謝,一個簡單的問題。在Tab Bar應用程序中管理iAds的最佳方法是什麼? – muneikh

+0

你正在接近管理的意義? – Kuldeep

+2

因爲在集成iAds之後,我體驗到我的應用程序性能已經放慢了一點,並且在標籤頁之間切換需要一點時間。 – muneikh