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];
Related
I am working on uiwebview and it is supposed to load dynamic urls coming from the server.
i want to show it fitting to the iPhone screen size in my iPhone application and also it should zoom in and out.
Code i am using for it is:
m_pView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
m_pView.backgroundColor = [UIColor blackColor];
self.view = m_pView;
UIWebView *aWebView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 1, 320, 400)] autorelease];
aWebView.dataDetectorTypes = UIDataDetectorTypeNone;
aWebView.delegate = self;
NSString* url = [NSString stringWithFormat: #"http://%#",[m_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.frame = self.view.bounds;
NSURL *aURL = [NSURL URLWithString: url];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
//load the index.html file into the web view.
[aWebView loadRequest:aRequest];
[m_pView addSubview:aWebView];
self.m_webView = aWebView;
Please let me know how to achieve webview fitting to the iphone screen size without loosing zoom in/out feature.
Thanks in advance.
Use following code
yourWEbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
yourWEbView.contentMode = UIViewContentModeScaleAspectFit;
problem might be in this line:
aWebView.frame = self.view.bounds;
Why you're setting bounds of self.view while adding webview to m_pView?
I am working on an app where my task is to get url-links from the database that i created a scroll view and placed images on the scroll. I have written some code that when i click on the image i have to show the url page in a webview which i placed above the scroll view. For this i wrote touch code for getting the url when i click on the image but i got problem like my click is not working on the image and scrollview also but it is working on the down part of the image and scrollview which is a normalview. U can find my touch code below
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [[event allTouches] anyObject];
for(int index=0;index<[array count];index++)
{
UIImageView *imgView = [array objectAtIndex:index];
if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollView]))
{
NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrayvideoFile objectAtIndex:index]];
NSURL *url = [NSURL URLWithString:strgetcontenturl];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}}}
Here array is where i stored all the images init and arrayvideoFile is where I stored all the urls.
Please help.
How did you placed your images to your scollview? ImageView?
I would place them with a UIButton (CustomType), then you can add target to that button and no need to detect touches. Because an UIButton already detects the touch events.
UIButton *button = [UIButton butttonWithType:UIButtonTypeCustom];
UIImage * buttonImage = ....
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:button];
Edit: Added code
Use below code.
for(int Tag =0; Tag < [array count]; Tag++) {
UIButton *btn_URL = [UIButton butttonWithType:UIButtonTypeCustom];
UIImage * btn_URL = [btn_URL setImage:buttonImage forState:UIControlStateNormal];
btn_URL.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height);
[btn_URL addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
btn_URL.tag = Tag;
[scrollview addSubview:btn_URL];
}
-(void)buttonAction:(id)sender {
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"%#",[array objectAtIndex:[sender Tag]]]];
}
Here you can redirect user to URL.
If you need any help then let me know.
Thanks,
Hemang.
just set imgView.userInteractionEnabled=YES; it may help you.
otherwise use this code
UIScrollView *scrollview4preview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44)];
scrollview4preview.backgroundColor=[UIColor clearColor];
[self.view addSubview:scrollview4preview];
int yval=0;
for (int i=0; i<[arrary count]; i++) {
UIButton *btn4preview=[[UIButton alloc]init];
btn4preview.tag=i;
btn4preview.frame=CGRectMake(scrollview4preview.frame.origin.x+10, i*yval, 150, 150);
[btn4preview setImage:[UIImage imageNamed:#"YourImageName.png"] forState:UIControlStateNormal];
[btn4preview addTarget:self action:#selector(btnPreviewSelect:) forControlEvents:UIControlEventTouchUpInside];
btn4preview.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
[scrollview4preview addSubview:btn4preview];
}
scrollview4preview.contentSize=CGSizeMake(scrollview4preview.frame.size.width, yval+160);
-(void)btnPreviewSelect:(id)sender
{
UIButton *btn=(UIButton *)sender;
NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrary objectAtIndex:btn.tag]];
NSURL *url = [NSURL URLWithString:strgetcontenturl];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}
I have to implement a button with rectangular border and it should be like this
and I am using the following code to do that:
-(void)createDynamicView:(CGRect)frame{
UIButton *drugimgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
drugimgBtn.frame = frame;//CGRectMake(0, 0, 155, 130);
NSString *myurl = responseDrugInfo.ThumbImg; //by rajesh
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl]];
UIImage *tbImage = [[UIImage alloc] initWithData:imageData];
[drugimgBtn setBackgroundImage:tbImage forState:UIControlStateNormal];
[drugimgBtn addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
UIView * prescView = [[UIView alloc]init];
prescView.frame = frame;
prescView.layer.borderColor = [UIColor lightGrayColor].CGColor;
prescView.layer.borderWidth = 1.0f;
[prescView addSubview:drugimgBtn];
[scrollview addSubview:drugimgBtn];
//[prescView release];
[myurl release];
[tbImage release];
}
The output is as follow:
How do I implement a rectangular border so it can appeared like the same in the first image?
Hi all
I have a tabbar based application. In one view I have a TTTabStrip with different TTTabItems. Everything works (loading normal UIViewControllers) except loading UIWebViews.
In the app delegate I have set up the url mapping:
TTNavigator* navigator = [TTNavigator navigator];
navigator.supportsShakeToReload = YES;
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]]; // default klasse wenn nichts anderes gewählt ist.
[map from:#"tt://test" toViewController:[TestController class]];
The test controller contains one UIWebView. I can see (NSLog) that the loadView-Method gets called, but the URL is not being opened:
- (void) loadView {
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
self.view = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
//self.view.backgroundColor = [UIColor blackColor];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
// support zooming
webView.scalesPageToFit = YES;
}
For instance, I can change the background of the UIWebView to black.
How can I load google in the web view below the TTTabStrip?
Thanks a lot.
Try the following and add the method viewWillAppear:
- (void)loadView
{
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
// support zooming
webView.scalesPageToFit = YES;
[newView addSubview:webView];
self.view = newView;
[newView release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}
I'm trying to get my webView to load correctly in the CGRect frame I set up, with the activityIndicator running.
It kind of loads correctly when I have it set up with [self.view addSubview:webView]; but the activityindicator doesnt appear. When I set it up with webView.delegate = self;the activityindicator does appear, but the webview covers up the whole screen when it loads up. I have a segmentedbar up top that gets covered up then.
- (void)viewDidLoad {
[super viewDidLoad];
CGRect webFrame = CGRectMake(0.0, 50.0, 320.0, 318.0);
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(250, 250, 30.0, 30.0);
self.view.center = CGPointMake(160.0f, 190.0f);
activityIndicator.center = self.view.center;
webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
webView.delegate = self;
// [self.view addSubview:webView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"activity started");
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"activity stopped");
[activityIndicator stopAnimating];
self.view = webView;
}
It should be
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"activity stopped");
[activityIndicator stopAnimating];
[[self view] addSubView:webView];
}
The views weren't being covered, they were being removed from the hierarchy by resetting the view property.