I'm a noob to iphone development and I am having trouble displaying an embedded youtube video within a UIwebview. Currently, the youtube video sits in the top left hand corner of the uiwebview and it also has an annoying border. I set the youtube frame to the same dimensions as my uiwebview and the webview is set to "scale to fit". How do I make my youtube content fit the entire screen and get rid of the border? Any help is greatly appreciated.
MY CODE
NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"320\" height=\"230\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil];
EDIT
NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil];
Have u tried this......
NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"100%%\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil];
Note That %% is used for % character.
So first you must create a UIWebView and add it to your main view.
In the following I create a webview pointing to www.example.com where I have embedding the YouTube clip I want to play, I set it hidden because I'll later want to launch the video full screen.`
UIWebView *htmlView;
htmlView = [[UIWebView alloc] initWithFrame:CGRectMake( 55.0f, 105.0f, 369.0f, 178.0f) ];
[htmlView setBackgroundColor:[UIColor blackColor]];
htmlView.hidden = YES;
htmlView.delegate = self;
[self addSubview:htmlView];
[htmlView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"www.example,com"]]]; `
So here I scan through all the buttons in the webview and select the first button found using the sendActionsForControlEvents function.
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 = findButtonInView( subview );
if( button )
{
return button;
}
}
}
return button;
}
-(void)webViewDidFinishLoad:(UIWebView*)webView
{
UIButton *button = findButtonInView( webView );
if( button != nil )
{
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
Related
I'm using a small UIWebView, and scalesPageToFit doesn't work correctly for some url. (look like it cannot resize at 100%. (but if i zoom out the webview manually(gesture), it work!
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 400.0f)];
webViewT = [[UIWebView alloc] initWithFrame:view.bounds];
webViewT.delegate = self;
webViewT.scalesPageToFit = YES;
NSURL* url = [NSURL URLWithString:#"http://www.google.com/"];
[webViewT loadRequest:[NSURLRequest requestWithURL:url]];
Please note that i've found a solution:(below work, BUT I have the time to see a "resize"
- (void) webViewDidFinishLoad:(UIWebView *)webViewT {
if ([theWebView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[theWebView scrollView];
float zoom=theWebView.bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:NO];
}
}
Any idea? Thanks!
In Some of cases this types of problem Generated. At that time java script use for control of Zoom of UIWebView, use following code.
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
Other way is
Refer this Answer UIWebView does not scale content to fit
I have set up a subview "popup" in my application and I want to show a navController if the user taps a button on the subview popup. I've set up the button so far, but if I tap the button the navigationController appears under my popup!? I've searched for some solution but I didn't found any. The whole controller is actually displayed in a folder which you can find here: https://github.com/jwilling/JWFolders So the viewDidLoad belong to the folder and the rootview.
I tried to make it as a subview of the popup but that doesn't work too. Does anyone know how to treat that? I've set up the popup programmaticaly and the navigationController too.
Thanks in advance.
My code:
The navController setup:
- (IBAction)dothis:(id)sender {
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = YES;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
[self presentModalViewController:navController animated:YES];
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"star" ofType:#"png"]];
photo.caption = #"The star is soo beateful...";
[photos addObject:photo];
self.photos = photos;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
The popup code:
-(IBAction)mehr:(id)sender {
//the popup size and content
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 440)];
CGRect welcomeLabelRect = contentView.bounds;
welcomeLabelRect.origin.y = 20;
welcomeLabelRect.size.height = 40;
UILabel *welcomeLabel = [[UILabel alloc] initWithFrame:welcomeLabelRect];
//an simple activityindicator
activityindi = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityindi.frame = CGRectMake(120, 200, 40, 40);
[activityindi startAnimating];
[contentView addSubview:activityindi];
//The Imageview
CGRect infoimagerect = CGRectMake(5, 70, 270, 200);
UIImageView *infoimage = [[UIImageView alloc] initWithFrame:infoimagerect];
//and the Button
cubut = [UIButton buttonWithType:UIButtonTypeCustom];
[cubut addTarget:self
action:#selector(dothis:)
forControlEvents:UIControlEventTouchUpInside];
[cubut setTitle:nil forState:UIControlStateNormal];
cubut.frame = CGRectMake(5, 70, 270, 200);
//retrieving data from parse.com
PFQuery *query = [PFQuery queryWithClassName:#"My-Application"];
[query getObjectInBackgroundWithId:#"My-ID"
block:^(PFObject *textdu, NSError *error) {
if (!error) {
//hide the Button if there is no image
cubut.hidden=YES;
//the headline of popup
UIFont *welcomeLabelFont = [UIFont fontWithName:#"copperplate" size:20];
welcomeLabel.text = [textdu objectForKey:#"header"];
welcomeLabel.font = welcomeLabelFont;
welcomeLabel.textColor = [UIColor whiteColor];
welcomeLabel.textAlignment = NSTextAlignmentCenter;
welcomeLabel.backgroundColor = [UIColor clearColor];
welcomeLabel.shadowColor = [UIColor blackColor];
welcomeLabel.shadowOffset = CGSizeMake(0, 1);
welcomeLabel.lineBreakMode = UILineBreakModeWordWrap;
welcomeLabel.numberOfLines = 2;
[contentView addSubview:welcomeLabel];
//the image from parse
if (!error) {
PFFile *imageFile = [textdu objectForKey:#"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
infoimage.image = image;
infoimage.contentMode = UIViewContentModeScaleAspectFit;
//show the button when the image appears
cubut.hidden = NO;
[contentView addSubview:infoimage];
//stop the activityindicator
[activityindi stopAnimating];
}
}];
}
} else {
//show some text
welcomeLabel.text = #"No connection!";
[welcomeLabel sizeToFit];
//hide the button
cubut.hidden = YES;
[contentView addSubview:infoLabel];
//stop the activityindicator
[activityindi stopAnimating];
}
}];
//add the content to the KNGModal view
[[KGModal sharedInstance] showWithContentView:contentView andAnimated:YES];
}
My viewDidLoad
- (void)viewDidLoad
{
but.hidden = YES;
PFQuery *query = [PFQuery queryWithClassName:#"myapp"];
[query getObjectInBackgroundWithId:#"Rgq5vankdf"
block:^(PFObject *textu, NSError *error) {
if (!error) {
but.hidden = NO;
but.color = [UIColor colorWithRed:0.90f green:0.90f blue:0.90f alpha:1.00f];
} else {
//if failure
but.hidden = YES;
mol.text = #"No Connection";
}
}];
[super viewDidLoad];
}
Pictures:
The button to open the folder:
The folder itself:
The popup:
Thanks in advance.
From the so far discussion and debugging the code you want to have the photo browser on the pop-up with a navigation controller.
So here is the sample code which implements this functionality, have a look at it.
I have used the same KGModal sample code and extended as per the requirement. I have used Xib to have a view with navigation bar.
To dismiss the pop-up from any where in the app you can use the below line, as it is shared instance.
[[KGModal sharedInstance] hideAnimated:YES];
Update:
The reason for showing the photo browser with in folderView is, you are trying to present the photoBrowser within the folderView, so it was presenting within the folderView of very small height & not able to see any photo.
So my suggestion is, as the user taps on pop-up to view photoBrowser you just remove pop-up and present the photoBrowser from the viewController class, as other than this class everything is handled through views.
I have made the changes as per above & works fine, to the code given by you, download the code here and have a look at it.
Let me know if it fulfills your needs.
Thanks
I noticed this line of code:
[[KGModal sharedInstance] showWithContentView: contentView andAnimated: YES];
And I can only think that, since it is a singleton, it adds the contentView on the UIApplication's key window. If that is the case, then a modal view controller will always be below the popup. You can solve this by adding a new method to the KGModal class
- (void) showWithContentView: (UIView*) contentView
inViewController: (UIViewController*) controller
andAnimated: (BOOL) animated;
the method should show the popup in the specified controller's view; you should use that method instead.
Edit
After some more digging, I found that KGModal displays the popup on another window. The quickest fix would be to dismiss the popup, then show the nav controller.
Embeded Youtube video in UIWebview. How can i play this you tube video using UIWebview when UIButton is pressed.
- (void)viewDidLoad
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *videoUrl = #"http://http://www.youtube.com/v/oHg5SJYRHA0";
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"480\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
//[window addSubview:webView];
[webView release];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
When user presses this UIButton it should start playing embedded youtube video
UIButton *videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[videoButton addTarget:self action:#selector(VideoAction:) forControlEvents:UIControlEventTouchUpInside];
videoButton.frame = CGRectMake(0, 0, 40, 40);
UIImage *icn = [UIImage imageNamed:#"Video_icon.png"];
[videoButton setImage:icn forState:UIControlStateNormal];
UIBarButtonItem *video = [[UIBarButtonItem alloc] initWithCustomView:videoButton];
How can i play youtube video.
Thanks for help.
Why dont you try this :
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=oHg5SJYRHA0"]]]
[self.view addSubview:webView];
I have an iphone UIWebView with contents that are approx 1000px x 200px. Currently when scrolling with your finger, it'll bounce vertically but not horizontally.
I want it to do the opposite: bounce horizontally but not vertically.
Is there some trickery i need to do in my html to achieve this? Eg setting the width and height on the body explicitly maybe? Or is it a setting of the UIWebView? I can do the following to totally disable bouncing, is there something similar to set bouncing on a per-direction basis?
[[[webView subviews] lastObject] setBounces:NO];
Thanks a lot for the help
In iOS 5, you don't need to access the subviews. UIWebView contains a scrollView property to the nested UIScrollView:
To disable bounce:
webView.scrollView.bounces = NO;
To only bounce vertically:
webView.scrollView.alwaysBounceVertical = NO;
webView.scrollView.alwaysBounceHorizontal = YES;
You can try to add a CSS StyleSheet and set the a container Div with the dimension of your view. and apply the styleSheet to your UIWebView.
You can do it like that -
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
//do base url for css
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *html =[NSString stringWithFormat:#"<!DOCTYPE html><html><head><link rel=\"stylesheet\" href=\"%#\" type=\"text/css\" /></head><body><section></section><br><br></body></html>",,cssPath];
[webView loadHTMLString:html baseURL:baseURL];
You can cycle through all the UIWebViews subviews(using a for loop) checking each one to see if it's a UIscrollView (using isMemberOfClass:) and then change the properties in the UIScrollView to meet your needs.
for (UIView *subview in [myWebView subviews]) {
if ([subview isMemberOfClass:[UIScrollView class]]) {
[(UIScrollView *)subview setAlwaysBounceVertical:NO];
[(UIScrollView *)subview setAlwaysBounceHorizontal:YES];
}
}
You can always do:
for (UIView *subview in [self.webView subviews]) {
SEL sel = #selector(setAlwaysBounceHorizontal:);
if( [subview respondsToSelector:sel] ){
[subview performSelector:sel withObject:(id)YES];
}
}
in your viewDidLoad
In my aplication I need to display an image or images on tableview cells depending on their availability in database. What I have done is I have planted four webviews [to show images from urls] and I'm hiding or showing these depending on their availability.
There might also be a case when there's no image, when I'm hiding webviews entirely.
But after random scrolling my application is crashing. Also, while scrolling it's showing me the newer images over older. So, I'm able to see at first one image, then after scrolling through the same cell, two images are led over one another[some part of earlier photo is visible] and so on. Can this be because of cell-reusability? What is the ideal way to about this issue?
EDIT:
I'm using following code:
noOfPhotos = [photos_array count];
if(noOfPhotos > 0){
commentWhenWebviewNotThere.alpha = 0.0; //Things that are not visible when images are there
noOfCommentsWhenWebviewNotThere.alpha = 0.0;
commentsLblWhenWebviewAbset.alpha = 0.0;
image1.hidden = NO;
image2.hidden = NO;
image3.hidden = NO;
image4.hidden = NO;
commentsLblWhenWebview.alpha = 1.0;
noOfComments.alpha = 1.0;
comment.alpha = 1.0;
for(NSInteger x = 0; x < noOfPhotos; x++){
photoName = [photos_array objectAtIndex:x];
NSString *urlAddress = [NSString stringWithFormat:#"%#",photoName];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
if(x == 0){
image1 = [[UIWebView alloc] initWithFrame:CGRectMake(10, comment.frame.origin.y - 5, 60, 60)];
[image1 loadRequest:requestObj]; //Load the request in the UIWebView.
[self addSubview:image1];
}
else if(x == 1){
image2 = [[UIWebView alloc] initWithFrame:CGRectMake(88, comment.frame.origin.y - 5, 60, 60)];
[image2 loadRequest:requestObj]; //Load the request in the UIWebView.
[self addSubview:image2];
}
else if(x == 2){
image3 = [[UIWebView alloc] initWithFrame:CGRectMake(169, comment.frame.origin.y - 5, 60, 60)];
[image3 loadRequest:requestObj]; //Load the request in the UIWebView.
[self addSubview:image3];
}
else if(x == 3){
image4 = [[UIWebView alloc] initWithFrame:CGRectMake(251, comment.frame.origin.y - 5, 60, 60)];
[image4 loadRequest:requestObj]; //Load the request in the UIWebView.
[self addSubview:image4];
}
}
}
else{
commentWhenWebviewNotThere.alpha = 1.0; //Should be visible when images are not there
noOfCommentsWhenWebviewNotThere.alpha = 1.0;
commentsLblWhenWebviewAbset.alpha = 1.0;
commentsLblWhenWebview.alpha = 0.0;
noOfComments.alpha = 0.0;
comment.alpha = 0.0;
image1.hidden = YES;
image2.hidden = YES;
image3.hidden = YES;
image4.hidden = YES;
}
Thanx in advance.
coupling!!! cells are being reused! what if cell is being reused even before those web views get the results? please pull out the image loading operation from your cell and do it in your controller. Cell must have four image views. And controller should pass the images for them.