YouTube In WebVIew - iPhone - iphone

This is very strange to me...
I have a simple WebView that loads and interacts with the user exactly like safari mobile (iPhone). Now when you visit m.youtube.com in safari, the url changes when you click on a link to something like this...
http://m.youtube.com/watch?gl=US&hl=en&client=mv-google&v=HX6SyoZ5kw8
The problem with this is I don't think that url is being used in my webview... What do I mean? The following code is used to load a url every time the user try's to click on a link, and it works, but I have a problem with Youtube...
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(#"%#",urlString);
VideoURLTextBox.text = urlString;
return YES;
}
When I first start up the webview it loads m.youtube.com and NSLogs() it into my console, but when I decide to click on a video it fails to NSLog() therefore I don't think a new url is being loaded, but when you load m.youtube.com in safari and click on a video you load a url like above, so why does this not NSLog() in my iPhone application?

What your probably seeing is that the youtube video is running javascript to play the video, you'd have to intercept the javascript callbacks to see that. Here's a link to the youtube video player api, detailing the callbacks, https://developers.google.com/youtube/js_api_reference#SubscribingEvents
If you just want to embed a youtube video in an iOS application then use this.
Using the mobile YouTube site isn't great, I had lots of problems with it in the past and ended up making a UIWebView category. I came across your question and decided to throw it on github :-)
https://github.com/enigmaticflare/UIWebView-YouTube--iOS-Category--ARC-compliant-code
UIWebView+YouTube iOS category to simplify loading youtube videos.
Instructions
1) Add these files to your project
UIWebView+YouTube.h
UIWebView+YouTube.m
2) Initiate UIWebView for example
#property (strong, nonatomic) UIWebView *webView;
#synthesize webView = _webView
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,212,172];
//set the X,Y origin of CGRect to where you want the webView to appear.
_webView.delegate = self;
[_webView loadYouTubeVideoID:#"gR8dO7Cln6M"];
[self.view addSubView:_webView];
Good luck, hope this is what your looking for :)
Adam

m.youtube.com is one giant java web app, delegate methods won't get called, presumably because any loading is written directly to the DOM in the UIWebView. Ran into this problem at work, not sure of the fix but there may be a way to inject the source with a JS click handler.

Related

iOS 6.1 UIWebview crashes when clicked on play youtube video inside a webpage

I am trying to show a website inside app. The website which i am trying to load has got a youtube video in it. The website loads properly in UIWebView and also lists the youtube video. But when I click on youtube video, the app crashes.
Crash log just says:
setting movie path: http://r19---sn-aig7knl7.c.youtube.com/videoplayback?...............
The website when loaded in Safari works fine, it even plays youtube video full screen.
Please help me in fixing this issue.
11 Feb 2013 - UPDATE
The issue is observed only on iOS Simulator 6.1. On Device everything works fine.
Detailed Info:
I have a custom UIViewController(TestWebViewController) which has an instance of UIWebView and implements UIWebViewDelegate. I have added a UIViewController in Storyboard and added UIWebView inside it, linked this UIViewController to my custom TestWebViewController and linked IBOutlet and delegate for UIWebView.
On Clicking a button, I am programmatically instantiating TestWebViewController using
TestWebViewController *testWebVC = [self.storyboard instantiateViewControllerWithIdentifier:#"testWebVC"];
testWebVC.view.frame = self.view.frame;
[testWebVC loadWebViewWithUrl:TEST_URL];
[self presentViewController:testWebVC animated:YES completion:nil];
As scott said in his comment, I removed my breakpoint for all exceptions and my error went away.
Hope this will help to others.
Modal view on iOS 5.0 and iOS 5.1 is the problem that causes crash on full screen video, AFAIK. They just changed hierarchy of views in that version of iOS (parentViewController and presentingViewController) and that is the aftermath. I asked about it long time ago here and one more same question is here and still no one knows what to do.
First of all, they fixed it in 6.0, I guess, that's good.
For 5.1 we changed design a little and avoided modal view. Do it, if it is possible in your situation.
TestWebViewController *testWebVC = [self.storyboard instantiateViewControllerWithIdentifier:#"testWebVC"];
[testWebVC LoadWebView:#"YOUR URL STRING"];
[self presentViewController:testWebVC animated:YES completion:nil];
AND in TestWebViewController class
-(void)LoadWebView:(NSString *)address {
addressStr = address;
}
- (void)viewDidLoad
{
[super viewDidLoad];
url = [NSURL URLWithString:addressStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url] ;
[myWebView loadRequest:request] ;
}

YouTube - Play automatically

I have an UIButton. When this button is pressed, I load a WebView with the youtube video:
- (IBAction)buttonAction:(id)sender {
UIWebView *webView = [[UIWebView alloc] initWithFrame:[MyButton frame]];
NSString *htmlString = #"<html> (...) </html>"; // HTML w/ the YouTube iFrame
webView.mediaPlaybackRequiresUserAction = NO;
[webView loadHTMLString:htmlString baseURL:nil];
[self.view addSubView:webView];
}
The problem is that when it happen, I have this result:
I want to start playing the video right after I load the WebView.
Any tip/solution?
Thanks!
I would suggest creating an html page that is a stand alone page that will use the youtube player api. Since your creating an html page already just have the uiwebview use the stand alone site that you have created and then you can use the autoplay features built into the youtube player. I can give more detail but if you can not use a stand alone html site as your option that this will be a worthless answer and thus anything else that I add will not help you.

How to create a button that links to Safari?

For legal reasons, I'm obligated to show Terms of Service in my application, a PDF on an external server. What I believe would be easiest to do would be to create a UIBarButton item and then create an IBAction that launches the link in Safari.
So I create a button:
IBOutlet UIBarButtonItem *legal;
Then I make it into a nonatomic property and synthesize it in my implementation file, right? I go on to create an IBAction:
-(IBAction)legalButtonPressed:(id)sender;
I go into my implementation file, and here's where the issue comes. When it comes to defining those actions, I become confused. As I am new to iOS development, I could use some guidance. I don't know how to force the link into safari in the action. Any help would be greatly appreciated!
Thanks!
IBOutlet and IBActions are used for Interface Builder connections. If you have your UIBarButtonItem on a xib file, you can connect its action to the controller by right clicking in the files owner object. The same with the outlet.
Once you have the action in the controller connected to the button in the xib file, (I see no need to get an outlet here), you just implement the method as follows:
-(IBAction)legalButtonPressed:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://yourdomain.com/legal.pdf"]];
}
this will automatically open safari with the given url
Take a look at UIApplication's documentation, it has a openURL method you might find useful.
Another option is to include your legal text in a UITextView.
Example:
By clicking Join you agree to company's
Terms of Service, found here: http://www.site.com/tos.html and
Privacy Policy, found here: http://www.site.com/privacy.html
Make sure Link Detection is turned on in the UITextView and it will automatically recognize URLs. Any clicks on those URLs will automatically launch Safari.
Although you can do this approach (i.e. launch the link in Safari). I would suggest that you try to keep the user as much as possible in your app. I am guessing this LEGAL TERMS is a HTML doc?
You can do that using UIWebView. Init a webView and do this -
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];
This will open the url in your app only! You can make the UIWebView open up as a modal window or in many other ways...

is it possible to use iframe in UIWebView?

i want to add a facebook like button in my app. in developer.facebook.com i couldn't fine anything about that. is it possible to use iframe created by facebook like button in UIWebView? it think if its possible then i can add a UIWebView in my app which use that iframe. thanx. please write some example code.
Yes it is possible, I am using iFrame to load youtube Video inside the app.
Following simple steps will guide you to achieve it.
Step 1 : Create a UIWebView in xib
Step 2 : Connect it to your ViewController's .h file. (I am referring it as _wevView in my project).
Step 3 : Create a method embedYouTube in your ViewController's .m file.
-(void)embedYouTube {
// Read create a NSString object with iframe
NSString *embedHTML = #"<iframe width=\"300\" height=\"250\" src=\"http://www.youtube.com/embed/rOPI5LDo7mg\" frameborder=\"0\" allowfullscreen></iframe>";
// Initialize the html data to webview
[_webView loadHTMLString:embedHTML baseURL:nil];
// Add webview to your main view
[self.view addSubview:_webView];
}
Step 4: Add the webview to your main view by by making a call to the method embedYouTube inside viewDidLoad
[self embedYouTube];
Step 5: Compile and run the app. You should be able to view the YouTube player embedded in the page.
Yes that is possible you can use the iframe in webview. You need to load the webview with the HTML string. You can also find the related code of HTML for iframe if you search over the net. Moreover you can also use javascript, in webview using the method stringByEvaluatingJavaScript: of UIWebview.

Put a link on home screen of my app and connect it to WebView

In my app i want to put my website's url on home screen and on clicking on it i want it to be open as a WebView.
How should i go for this.
Thanks,
Previous commenter is incorrect. You can open any hyperlink either externally with Safari or internally with a UIWebView.
Add a UIWebViewController to your project. Then, instantiate an instance of a the UIWebViewController that will be shown inside your app--you'll do this by declaring a property & synthesizing it within your main view controller (which will need to be declared as a UIWebViewDelegate), such as:
#interface MyMainViewController: UIViewController <UIWebViewDelegate> {
// Your implementation code here
}
When a user taps the button (assuming you make it a button, rather than just a text hyperlink), you instruct your app to add the UIWebView to the view stack, loading the correct link. You'll want to either do this within a modal view or within a navigation stack so your users can get back out of the web view, of course.
In your MyMainViewController implementation file, something like this:
-(void) showWebView {
// NOTE: I have not tested this, just prototyping
// off the top of my head
UIWebView *myWebView = [[UIWebView alloc] init];
myWebView.delegate = self;
NSURL *homeUrl = [[NSURL alloc] initWithString:#"http://example.com"];
NSURLRequest *homeRequest = [NSURLRequest requestWithURL:homeURL];
[myWebView loadRequest:homeRequest];
[self.presentModalViewController: myWebView animated:YES];
// Don't forget to release objects when you're done
[myWebView release]; // etc.
}
Now, this is off the top of my head from what I know and have done. But I hope you get the general idea. I offer no warranty of any kind here, but do guarantee this is entirely possible with minimal headache. If you get stuck, check out the developer references for UIWebView. Apple's docs are top-notch & show great examples to get you up and running quickly.
Best.