I am making an iPhone app that has a bunch of "articles" with certain words that I want to link to other "articles". I need to display the articles in UITableViewCells.
If there is a way to do this without a web view, that's great.
If I need to use a web view, should I just make <a href> style links? How can I have it jump to the article without creating a bunch of actual html pages, just have it let my program know that they clicked on that link and then the program goes and finds the appropriate article from a plist or something?
I've create a test demo using UIWebView and it works fine.
Create two HTML files:home.html and first.html.
// home.html
Home page. first
// first.html
First page. home
//viewDidLoad
self.webView.delegate = self;
NSString *htmlFilePathString = [[NSBundle mainBundle] pathForResource:#"home" ofType:#"html"];
NSURL *htmlFileURL = [[NSURL alloc] initFileURLWithPath:htmlFilePathString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:htmlFileURL];
[self.webView loadRequest:request];
// webView's delegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
Related
I understand that you can get the current URL of a UIWebview via the following code:
NSString *urlString = [[[webView request] URL] absoluteString];
However, I've found that this only works for a webView that you have actually loaded.
At some point in my application, I'm doing this: [webView goBack] which will show the webView's previous page. However, calling the above line to get the URL doesnt reflect the actual page shown.
Is there any way to accomplish this?
Thank you!
It might be a safe approach to get the URL at the moment the page has been fully loaded :
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSURLRequest *currentRequest = [webView request];
NSURL *currentURL = [currentRequest URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
}
Does this work for you ?
Implement the delegate function
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Before every page is about to start loading in your webview (even the goback) will give a call in the above function and the request object give the url is going to load.
Note: return YES if you allow the webview to load the page else NO.
Hope this help you solve your problem.
I have some local html files in resources.on launch one html,say a.html, is loaded in webview. there are some hyperlinks in that webview(or say the html content) which are links to other html files in my resources.now on click of those links I want to open them in in-app browser.
how to do this.
Any idea????
thnx!!!!
I've not tried this, but it might work!
Create your UIWebView and load the first html file from your bundle
NSString *file = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"html"];
NSString *html = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:[NSString stringWithFormat:#"<html><head></head><body>%#</body></html>", html] baseURL:baseURL];
webView.delegate = self;
Then get when the user taps a link
-(BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
// Find which link is selected, get the file and load it into the UIWebView
}
return YES;
}
I want to open an iTunes link in my webView, but when the webView launches the page, it redirects to the Safari browser. There, the url is getting opened, but I want it to open in my webView.
- (void)viewDidLoad {
NSString *urlAddress = #"http://itunes.apple.com/us/app/foodcheck-traffic-light-nutrition/id386368933?mt=8";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[super viewDidLoad];
}
Please suggest a way to sort out this problem.
You may want to try logging the load requests when you run the app. It may be that apple is automatically changing http:// to itms-apps or http://phobos or something along these lines. If so, then you can block the load when it's call using something like this:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *loadURL = [[request URL] retain];
NSLog(#"%#",loadURL);
if([[loadURL absoluteString] hasPrefix:#"http://"])
{
[loadURL release];
return TRUE;
}
[loadURL release];
return FALSE;
}
Good luck. I'm curious to know what finally works.
A note from the Apple reference documents- Q: How do I launch the App Store from my iPhone application? Also, how do I link to my application on the store?
Note: If you have iTunes links inside
a UIWebView, you can use this
technique after intercepting the links
with the -[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:]
delegate method.
Not sure if you ever got it working, but this works well for me:
NSString *responseString = [NSString stringWithContentsOfURL:myURL];
[self.webView loadHTMLString:responseString baseURL: nil)];
I have the following method in a UIViewController:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType.
My webview has two links in the view; I want to be able to distinguish between the two of them so that I know which one the user clicked on.
Does anyone know how to?
You must extract a url string from NSURLRequest:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *requestUrl = [request URL];
NSString *currentPath = [requestUrl path];
...
}
Then use currentPath to compare with your two URLs. If those URL are unknown you should parse your html file to find them in it. You cannot get access to HTML document structure through UIWebView because it is extremely complicated.
I am working on an assignment which requires me to open a HTML page in a webview in iPhone. I have a hyperlink which is shown as a string eg. "sometext.fileextensiontype". When this text is clicked it redirects to a link eg. http:www.someweblink.com/uyewihwefkbnamsjdfb".
I am able to intercept the above URL but I want to read the hyperlink string i.e. sometext.fileextensiontype.
you require to confirm with the UIWebViewDelegate protocol and provide implementation to the below shouldStartLoadWithRequest
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *myUrl = [request URL];
NSString* urlString = [myUrl absoluteString];
return YES;
}