Advertisement
Leonne

Create Notification Center Widget for iOS (Full Code).

Feb 20th, 2012
1,949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "BBWeeAppController-Protocol.h"
  2. #import <Twitter/Twitter.h>
  3.  
  4. /***
  5.  TUTORIAL BY ANDY IBANEZ (@Leonnears).
  6.  This is the tutorial I wrote about writing notification center widgets for iOS. You can find it on my tumblr blog (leonnears.tumblr.com).
  7.  You can also follow me on Twitter (@Twitter) to receive updates when I write anything in my new blog, including when I post new tutorials and so on. I don't only tweet programming related stuff so don't follow me if you expect to only see programming or tech related stuff. ;)
  8.  */
  9.  
  10. static NSBundle *_iNotitweetWeeAppBundle = nil;
  11.  
  12. @interface iNotitweetController: NSObject <BBWeeAppController> {
  13.     UIView *_view;
  14.     UIImageView *_backgroundView;
  15. }
  16. @property (nonatomic, retain) UIView *view;
  17. @end
  18.  
  19. @implementation iNotitweetController
  20. @synthesize view = _view;
  21.  
  22. + (void)initialize {
  23.     _iNotitweetWeeAppBundle = [[NSBundle bundleForClass:[self class]] retain];
  24. }
  25.  
  26. - (id)init {
  27.     if((self = [super init]) != nil) {
  28.        
  29.     } return self;
  30. }
  31.  
  32. - (void)dealloc {
  33.     [_view release];
  34.     [_backgroundView release];
  35.     [super dealloc];
  36. }
  37.  
  38. - (void)loadFullView {
  39.     // Add subviews to _backgroundView (or _view) here.
  40.     UIButton *tweet = [UIButton buttonWithType:UIButtonTypeCustom];
  41.     [tweet setBackgroundImage:[UIImage imageWithContentsOfFile:[_iNotitweetWeeAppBundle pathForResource:@"tweet" ofType:@"png"]] forState:UIControlStateNormal];
  42.     tweet.frame = CGRectMake(2, 0, 79, 33);
  43.     [tweet addTarget:self action:@selector(composeTweet) forControlEvents:UIControlEventTouchDown];
  44.     [_view addSubview:tweet];
  45. }
  46.  
  47. -(void)composeTweet {
  48.     TWTweetComposeViewController *twtComposer = [[TWTweetComposeViewController alloc] init];
  49.     UIViewController *someVc = [[UIViewController alloc] init];
  50.     twtComposer.completionHandler = ^(TWTweetComposeViewControllerResult result)
  51.     {
  52.         if(result == TWTweetComposeViewControllerResultCancelled)
  53.         {
  54.             [someVc dismissModalViewControllerAnimated:YES];
  55.         }
  56.     };
  57.     someVc.view = _view;
  58.     [someVc presentModalViewController:twtComposer animated:YES];
  59.     [someVc release];
  60.     [twtComposer release];
  61. }
  62.  
  63. - (void)loadPlaceholderView {
  64.     // This should only be a placeholder - it should not connect to any servers or perform any intense
  65.     // data loading operations.
  66.     //
  67.     // All widgets are 316 points wide. Image size calculations match those of the Stocks widget.
  68.     _view = [[UIView alloc] initWithFrame:(CGRect){CGPointZero, {316.f, 33.f}}];
  69.     _view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  70.  
  71.     UIImage *bgImg = [UIImage imageWithContentsOfFile:@"/System/Library/WeeAppPlugins/StocksWeeApp.bundle/WeeAppBackground.png"];
  72.     UIImage *stretchableBgImg = [bgImg stretchableImageWithLeftCapWidth:floorf(bgImg.size.width / 2.f) topCapHeight:floorf(bgImg.size.height / 2.f)];
  73.     _backgroundView = [[UIImageView alloc] initWithImage:stretchableBgImg];
  74.     _backgroundView.frame = CGRectInset(_view.bounds, 2.f, 0.f);
  75.     _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  76.     [_view addSubview:_backgroundView];
  77. }
  78.  
  79. - (void)unloadView {
  80.     [_view release];
  81.     _view = nil;
  82.     [_backgroundView release];
  83.     _backgroundView = nil;
  84.     // Destroy any additional subviews you added here. Don't waste memory :(.
  85. }
  86.  
  87. - (float)viewHeight {
  88.     return 71.f;
  89. }
  90.  
  91. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement