I'm trying to load a javascript file but it doesn't work.
My code :
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><script type=\"text/javascript\" src=\"http://mywebsite.com/file.js\"></script></body></html>"];
[_webView loadHTMLString:html baseURL:nil];
This code is supposed to display a video, but nothing happens.
This code displays correctly the video in Desktop browser, my JS is correct.
use url in baseURL
NSURL *base=[NSURL URLWithString:#"website.com"];
Do not use string description. Instead use:
[_webView loadHTMLString:html baseURL:nil];
Write following code in - webViewDidFinishLoad:
NSString * strJavaScript = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://mywebsite.com/file.js"] encoding:NSUTF8StringEncoding error:nil];
[_webView stringByEvaluatingJavaScriptFromString:strJavaScript];
Related
I have been looking at numerous tutorials to play youtube videos via the app. However they are producing a blank screen. According to comments on the tutorials this is common on xocde 5? Is there another way in which videos can be streamed? I am running it on a iphone and have connected the webview.
I am using :
.h
#interface detailViewController : UIViewController {
IBOutlet UIWebView *webView;
}
VIEW DID LOAD
NSURL *url = [NSURL URLWithString:#"<iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/AmMF7hWrYZk\" frameborder=\"0\" allowfullscreen></iframe>"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
UIWebView *web;
NSString *pageHTML = [[NSBundle mainBundle] pathForResource:#"url" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:pageHTML encoding:NSUTF8StringEncoding error:nil];
[web loadHTMLString:htmlString baseURL:nil];
the code in url.html is
<html>
<head></head>
<body>
<iframe src='http://player.vimeo.com/video/73198189?title=1&byline=0&portrait=0&loop=1' width='690' height='500' frameborder='10' webkitAllowFullScreen mozallowfullscreen allowFullScreen>myframe</iframe>
</body>
i have used this code for playing vimeo videos and it worked.. hope this helps you..
I had the same issue happen when I was using the YouTubeHelper Xcode libraries (https://developers.google.com/youtube/v3/guides/ios_youtube_helper). I found that the first time I added the YT* libraries into my project that I didn't select the "Create Folder References for any added folders" when adding the assets folder into my Xcode project. After removing the existing assets folder and following that step I was able to get the helper libraries to work within my project and play YouTube videos as the link above describes. Hope that helps.
I had implemented youtube videos using the HCYoutubeParser. Very nice and easy to use.
Consider letting the web view tell you why its not loading
Implement UIWebViewDelegate methods in your view controller , and attach the delegate to your view controller.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
may give you a very good hint.
Send you NSURLRequest request from
-(void)viewWillAppear:(BOOL)animated {
}
Here is my code, used in my app
NSString *youTubeID = [self extractYoutubeID:url];
NSString *embedHTML =[NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: #666666;\
padding:%f %f %f %f;\
color: blue;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe height=\"%f\" width=\"%f\" title=\"YouTube Video\" class=\"youtube-player\" src=\"http://www.youtube.com/embed/%#\" ></iframe>\
</body></html>",paddingTop,paddingRight,paddingBottom,paddingLeft,videoHeight,videoWidth,youTubeID];
[self.webView loadHTMLString:embedHTML baseURL:nil];
// extractYoutubeID
+ (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
//NSLog(#"youtube %#",youtubeURL);
NSError *error = NULL;
NSString *regexString = #"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])];
if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
{
NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];
return substringForFirstMatch;
}
return nil;
}
I had the same problem. You forgot to put "http://" in your youtube link.
Here's the correct code:
NSURL *url = [NSURL URLWithString:#"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/AmMF7hWrYZk\" frameborder=\"0\" allowfullscreen></iframe>"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
I've start to learning UIWebView and I have next problem. When I'm trying to use next code:
NSString *searchString = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",lookedCity];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:searchString]]];
or:
NSString *searchString = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/%#",lookedCity];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:searchString]]];
my WebView doesn't loading page. But if I use standart URLs (ex. google.com), all works fine.
In case with Wiki, I tried use WikiApiObjectiveC, but even example doesn't work. How can I solve this problem? The best solution will be with Wiki URL.
Problem solved!
The reason, why my URL doesn't work, was in cyrillic symbols in my searchString. Solution: urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I want to parse RTF file in Objective c.Is its possible to parse it? if is possible then how can I use it into IOS.I want all the text and formatting.
I have tried in so many way to get text and formatting for this purpose I have used UIWEBVIEW but its not return any text string
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"rtf"];
NSData *data = [NSData dataWithContentsOfURL:imgPath];
[webView loadData:data MIMEType:#"text/rtf" textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#"/"]];
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
Here html return nil value.
Thanks
Ok, this question is very old but in case someone stumbles across this problem:
The html is nil as the webView is not finished loading. Fetch the html in the UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
NSLog(#"Html: %#",html);
}
You will get the desired html.
I want to write html code locally and show it in UIWebview ,so for this ,I have taken a simple html code ,as below and created a .html extension file..
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
I added this file into my xcode project ,now I want to load this file inside webview ,so I have written this code
[m_websiteView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"]isDirectory:NO]]];
but the output I get when I run this code in UIWebView is same htmlcode, I cant figure out what the problem is..but when I tried using this code
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *testString = #"<h1><i>This is an image using reference in HTML</i></h1>"
"<img src='active.png'></img>";
[m_websiteView loadHTMLString:testString baseURL:baseURL];
this works perfect.also I want to localize the strings in this html code,so in the above code,how can I localize this string "This is an image using reference in HTML"..
Regards
Ranjit
Add localization, use this
NSString *testString = [NSString stringWithFormat:
#"<h1><i>%#</i></h1><img src='active.png'></img>",
NSLocalizedString(#"This is an image using reference in HTML", #"htmlText")];
and specify the localize strings in the file Localizable.strings in the correct language directory.
You can also localized entire (HTML) files with the locale directory scheme.
HTML syntax is
<html>
<head>
</head>
<body>
</body>
</html>
after that you can preview your html file using something like that in your view controller
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = #"http://www.istoselidas.gr";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];
just replace url with your html file
I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com