Question about UIWebview when tapping links - ios

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;
}

Related

Parameter in loadWebView

In my app i have this function:
- (void)loadWebView {
NSBundle *bundle=[NSBundle mainBundle];
NSString *filePath = [bundle pathForResource:#"namefile" ofType: #"html"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];
[wv loadRequest:request];
}
which loads an HTML page in UIWebView. Is possible to pass a parameter in the call ?
You can try something like this:
-(void)loadWebView
{
NSURL *relativeURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSURL *URL = [NSURL URLWithString:#"namefile.html?parameter=10" relativeToURL:relativeURL];
//If your HTML file is in a subfolder, add it to the string like 'HTML/namefile.html?parameter=10
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[wv loadRequest:request];
}

Get title of a website silently from a website

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);
}

Opening link in WebView

I have an application at the moment that basically opens a url depending on what the user has typed into a textbox. The logic for this is as such:
Predetermined beginning + User Input + Predetermined End
So basically my URL is 3 concatenated strings. Now I know the link is being formed properly (I've put the same snip of code into a label) but nothing happens when I press the button to load the webview.
It works perfectly fine when I use the below, where I explicitly type https://google.com
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = #"https://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
However nothing happens when I use this code which includes my concatenated url:
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
Any help would be much appreciated.
UPDATE: I'm fairly sure having the stringWithFormat is causing this problem just unsure how to get around it.:
Check if fullURL starts with http:// or https://, if not prefix that.
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
if ( ! ([fullURL hasPrefix:#"http://"] || [ fullURL hasPrefix:#"https://"]) ) {
fullURL = [NSString stringWithFormat:#"http://%#", fullURL ];
}
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}

How to detect the hyperlinks in a local pdf displayed in a UIWebView

I'm loading a pdf file in a UIWebView with the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#", pdfString] ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webview loadRequest:req];
It's working fine. But I want to enable the hyperlinks in the pdf file (just like how UITextView detects links).
Use This:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}

How to load an .rtfd.zip file into an iPad UIWebView

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]]];
}

Resources