I have a link of a website, and I want to get its title.
I tried to do by this code
UIWebView* hiddenWebView;
NSString* urlString = #"http://www.youtube.com/watch?v=OyORxdjGtlk";
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[hiddenWebView loadRequest:request];
NSString* text = [hiddenWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
But the result is: text = NULL;
I just want to get the name of the video
Set the UIWebView delegate to your ViewController (for example by ctrl-dragging in Interface Builder) and then:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSURL *url = [webView.request mainDocumentURL];
NSString *str = [url absoluteString];
NSString *title = [webView stringByEvaluatingJavaScriptFromString:#"document.title"];
NSLog(#"%s: url=%# str=%# title=%#", __PRETTY_FUNCTION__, url, str, title);
}
Related
I am trying to give my simple browser the ability to recognize that text with spaces should be treated as a google search query. To create a string that can go into a url I need to replace the spaces with "+"s.
I have attempted to do this in the first "if" statement however when I do run the program and put text with spaces into the search bar nothing happens; entering "google.com" or other urls work though.
What is wrong with my code?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSString *URLString = textField.text;
NSURL *URL = [NSURL URLWithString:URLString];
if ([URLString rangeOfString:#" "].location != NSNotFound) {
NSString *plusReplace = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
URL = [NSURL URLWithString:[NSString stringWithFormat:#"google.com/search?q=<%# query", plusReplace]];
}
if (!URL.scheme) {
// The user didn't type http: or https:
URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", URLString]];
}
if (URL) {
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
}
return NO;
}
I think you might have an issue with the 3rd conditional making a 2nd request. What if you change it to this?
if (URL) {
NSString *plusReplace = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
URL = [NSURL URLWithString:[NSString stringWithFormat:#"google.com/search?q=<%# query", plusReplace]];
}
if (!URL.scheme) {
// The user didn't type http: or https:
URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", URLString]];
}
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
In my DetailViewController.h file, I have declared a NSString * property:
#property (copy, nonatomic) NSString *url;
In my DetailViewController.m file, I have:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
}
This loads google.com in my web view but the following code does not:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *url = self.url;
NSLog(#"this is url %#", url);
NSURL *myURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
}
Which prints out: "this is url http://google.com".
Both url and #"http://google.com" are NSString* objects but why is the first coding loading the website but the second is not?
So realized there was whitespace at the end of url that I hadn't noticed until I printed out:
NSLog(#"this is url %#.", url);
Notice the period I added in the end. I had whitespace there, hence rendering my url invalid. I had to trim out the whitespace somewhere else in my code.
In my webview viewdidload i'm implementing the following code. the problem is the webview won't appear when I include the NSString *encodedString. I have searchString with spaces and sometimes &. Any ideas on what's going on here?
NSString *urlString = [NSString stringWithFormat:#"http://google.com?q=%#", searchString];
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,(CFStringRef)urlString,
NULL,(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
NSLog(#"searchString is %#", searchString);
NSURL *myURL =[NSURL URLWithString:encodedString];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[searchWebView loadRequest:myRequest];
What would it take to generate the first page results of google for this searchString?
Try This
NSString *urlString = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",searchString];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL =[NSURL URLWithString:urlString];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[searchWebView loadRequest:myRequest];
Use this to encode Your SearchString
SearchString=[[NSString stringWithFormat:#"%#", SearchString] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
and then append it to your URL
I have an UIWebView which loads an html-file. Once the user clicks a link, i want the url to open in a custom UIWebview.
I tried some things:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"MyiPadHTML"
ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
else {
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"MyHTML"
ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
This is me, loading the files, depending on the device. That works great. I did it in the - (void)viewDidLoadmethod
Shouldn't this work?
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ( navigationType == UIWebViewNavigationTypeLinkClicked ) {
[myOtherCustomWebView loadRequest:request];
return NO;
}
return YES;
}
I have an iPad UIWebView which has to display some rtfd.zip files. I try to do it like this:
-(void) viewWillAppear:(BOOL)animated {
[self loadFile:string];
}
- (void)loadFile:(NSString*)file
{
NSString* resourcePath = [[NSBundle mainBundle] bundlePath];
NSString* sourceFilePath = [resourcePath stringByAppendingPathComponent:file];
NSURL* url = [NSURL fileURLWithPath:sourceFilePath isDirectory:NO];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
[resourcePath release];
}
But I only get a blank page. The outlets are definitely connected properly - the UIWebView can load google main page.
What am I doing wrong?
Thanks in advance!
Try to use this, I got output by using this,
- (void)loadFile:(NSString*)file {
UIWebView *webview=[[UIWebView alloc] init];
NSString *FullstrURL=[[NSBundle mainBundle] pathForResource:file
ofType:#"html" ];
NSLog(#"HTML String = %#",FullstrURL);
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:FullstrURL]]];
}