Thursday, May 23, 2013

Play YouTube Embedded Video in WebView on UiButton Click

In this Example I will Show you how to play YouTube Video embedded in WebView on Button Click.
Just we have to find Button within WebView and Trigger it.

Here is the Code.
.h

#import <UIKit/UIKit.h>
@interface RSSDetailViewController : UIViewController <UIWebViewDelegate,UIScrollViewDelegate> 
@property (nonatomic, retain) IBOutlet UIWebView *webViewLink; 
- (IBAction)btnPlay:(id)sender; 
@end

.m
// self.link is link of youtube video.
- (void)viewDidLoad
{
 [self.webViewLink loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.link]]];
}

- (UIButton *)findButtonInView:(UIView *)view {
    UIButton *button = nil;
    
    if ([view isMemberOfClass:[UIButton class]]) {
        return (UIButton *)view;
    }
    
    if (view.subviews && [view.subviews count] > 0) {
        for (UIView *subview in view.subviews) {
            button = [self findButtonInView:subview];
            if (button) return button;
        }
    }
    
    return button;
}
- (IBAction)btnPlay:(id)sender {
    UIButton *btnTouch=[self findButtonInView:webViewLink];
    [btnTouch sendActionsForControlEvents:UIControlEventTouchUpInside];
}

Reference
:http://stackoverflow.com/questions/3010708/youtube-video-autoplay-on-iphones-safari-or-uiwebview

No comments:

Post a Comment