Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 130 lines (95 sloc) 2.433 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
//
// DTWebVideoView.m
// CoreTextExtensions
//
// Created by Oliver Drobnik on 8/5/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//

#import "DTWebVideoView.h"
#import "DTTextAttachment.h"


@interface DTWebVideoView ()

- (void)disableScrolling;

@end


@implementation DTWebVideoView
{
DTTextAttachment *_attachment;

__unsafe_unretained id <DTWebVideoViewDelegate> _delegate;

UIWebView *_webView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
{
self.userInteractionEnabled = YES;

_webView = [[UIWebView alloc] initWithFrame:self.bounds];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:_webView];

[self disableScrolling];

_webView.delegate = self;

if ([_webView respondsToSelector:@selector(setAllowsInlineMediaPlayback:)])
{
_webView.allowsInlineMediaPlayback = YES;
}
    }
    return self;
}

- (void)dealloc
{
_webView.delegate = nil;

}


- (void)disableScrolling
{
// find scrollview and disable scrolling
for (UIView *oneView in _webView.subviews)
{
if ([oneView isKindOfClass:[UIScrollView class]])
{
UIScrollView *scrollView = (id)oneView;

scrollView.scrollEnabled = NO;
scrollView.bounces = NO;

return;
}
}
}

#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// only allow the embed request
if ([[[request URL] absoluteString] hasPrefix:@"http://www.youtube.com/embed/"])
{
return YES;
}

BOOL shouldOpenExternalURL = YES;

if ([_delegate respondsToSelector:@selector(videoView:shouldOpenExternalURL:)])
{
shouldOpenExternalURL = [_delegate videoView:self shouldOpenExternalURL:[request URL]];
}

if (shouldOpenExternalURL)
{
[[UIApplication sharedApplication] openURL:[request URL]];
}

return NO;
}



#pragma mark Properties

- (void)setAttachment:(DTTextAttachment *)attachment
{
if (_attachment != attachment)
{

_attachment = attachment;
}

switch (attachment.contentType)
{
case DTTextAttachmentTypeIframe:
{
NSURLRequest *request = [NSURLRequest requestWithURL:attachment.contentURL];
[_webView loadRequest:request];
break;
}

default:
break;
}
}

@synthesize delegate = _delegate;
@synthesize attachment = _attachment;

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