Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 44d2f1caa9
Fetching contributors…

Cannot retrieve contributors at this time

file 162 lines (121 sloc) 5.086 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/*
* This project constitutes a work of the United States Government and is
* not subject to domestic copyright protection under 17 USC § 105.
*
* However, because the project utilizes code licensed from contributors
* and other third parties, it therefore is licensed under the MIT
* License. http://opensource.org/licenses/mit-license.php. Under that
* license, permission is granted free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the conditions that any appropriate copyright notices and this
* permission notice are included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//
// WHYouTubePlayerViewController.m
// WhiteHouseApp
//
//

#import "WHYouTubePlayerViewController.h"

@interface WHYouTubePlayerViewController ()
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) UIWebView *webView;

// excuse the awkward name, but this literally means that the view needs to call the JS function: loadVideo
@property (nonatomic, assign) BOOL needsLoadVideo;
@end

@implementation WHYouTubePlayerViewController

@synthesize videoURL = _videoURL;
@synthesize webView;
@synthesize needsLoadVideo;

- (id)initWithVideoURL:(NSURL *)videoURL
{
    if ((self = [super initWithNibName:nil bundle:nil])) {
        self.videoURL = videoURL;
    }
    
    return self;
}


// so we don't get crashes when the UIWebView calls a deallocated delegate...
- (void)unhookWebView
{
    [self.webView stringByEvaluatingJavaScriptFromString:@"stopVideo();"];
    [self.webView stopLoading];
    self.webView.delegate = nil;
}


- (void)dealloc
{
    [self unhookWebView];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    // initalize our webview
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.webView.hidden = YES;
    self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.webView.scrollView.scrollEnabled = NO;
    self.webView.scrollView.bounces = NO;
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    
    NSURL *playerURL = [[NSBundle mainBundle] URLForResource:@"youtube" withExtension:@"html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:playerURL]];
    
    self.needsLoadVideo = YES;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // shut down the web view properly
    [self unhookWebView];
    self.webView = nil;
}


- (void)fixNavBar
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    self.view.window.rootViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}


static NSString *movieNotification = @"UIMoviePlayerControllerDidExitFullscreenNotification";


- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(fixNavBar)
                                                 name:movieNotification
                                               object:nil];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:movieNotification object:nil];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait) || UIInterfaceOrientationIsLandscape(interfaceOrientation);
}


#pragma mark UIWebViewDelegate methods


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (self.needsLoadVideo) {
        self.needsLoadVideo = NO;
        
        self.webView.hidden = NO;
        NSString *loadVideoScript = [NSString stringWithFormat:@"loadVideo(\"%@\")", self.videoURL.absoluteString];
        
        DebugLog(@"Displayed webView; calling script: %@", loadVideoScript);
        [self.webView stringByEvaluatingJavaScriptFromString:loadVideoScript];
    }
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    DebugLog(@"Request: %@", request);
    
    return YES;
}


@end
Something went wrong with that request. Please try again.