chance
chance

Reputation: 21

Youtube - iPhone - displaying UIWebView of video

I have a script in my app that loads a Youtube link in a section of the UIView, the link is provided by my server (mySQL/using php) and from that it is loaded... this script below works fine..... however...

Here is what I have right now, I was looking to remove the programmatically part (where it gives the dimensions and possibly replace it with a UIWebView) to it, so I could improvise using IB, this will help when my application launches, a quick animation occurs and then I want this youtube link to load and display after viewDidLoad is completed.

.h file:

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame;

.m file

//view did load function...

[self embedYouTube:youtubestring frame:CGRectMake(69,72,184,138)];

//after the view did load is closed I have..

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}

any help would greatly be appreciated!! thanks

Upvotes: 2

Views: 5928

Answers (3)

Adam Roberts
Adam Roberts

Reputation: 78

iOS has a safari plugin which plays youtube videos if they embedded as flash.

This uiwebview category I've put together will embed videos for you without haven't to leave the application or a tutorial :-)

https://github.com/enigmaticflare/UIWebView-YouTube--iOS-Category--ARC-compliant-code

Upvotes: 0

StratFan
StratFan

Reputation: 361

This works using UIWebViews created in IB:

The .h File:

@property (nonatomic, retain) IBOutlet UIWebView *infoVideo1;

-(void)embedYouTubeInWebView:(NSString*)url theWebView: (UIWebView *)aWebView;

The .m File:

in the viewDidLoad:

[self embedYouTubeInWebView:youTubeString theWebView:infoVideo1];

the Method:

- (void)embedYouTubeInWebView:(NSString*)url theWebView:(UIWebView *)aWebView {     

NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

NSString* html = [NSString stringWithFormat:embedHTML, url, aWebView.frame.size.width, aWebView.frame.size.height]; 

[aWebView loadHTMLString:html baseURL:nil];
}

You can even have multiple webViews on a single screen. Hope this helps.

Upvotes: 7

Blitz
Blitz

Reputation: 5671

Aehm, the iPhone doesn't have Flash. It never had and probably never will have. The only way to open youtube videos on the iPhone is via the youtube app, which plays them back in another format. An example for this is [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com /v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM"]];
You can find more detail here: http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html

Upvotes: 1

Related Questions