I have a UIWebView and UITextField in my view but am facing a problem.
The problem is how do I replace the URL of the web page in UITextField automatically, as I surf through different web pages?
Use the following property of the UIWebView instance:
#property(nonatomic, readonly, retain) NSURLRequest *request
So you can get the current NSURLRequest associated with the web view by:
NSURLRequest* urlRequest = myWebView.request;
You can then get the associated instance of NSURL using the following instance method:
- (NSURL *)URL
So you can now get the NSURL object form the UIWebView with the following code:
NSURL* url = [myWebView.request URL];
To convert this to a string you can use either the absoluteString or relativeString instance methods of NSURL. For this example I'll use absoluteString.
NSString* urlString = [[myWebView.request URL] absoluteString];
Now we can set the the text of the UITextField using the simple text property:
myTextField.text = [[myWebView.request URL] absoluteString];
So now all we need to do is ensure that the previous line of code gets called at the correct time in order to update the text field with the url text whenever the page changes. To do this, we need to use a class which is a delegate of the UIWebView (and thus conforms to the UIWebViewDelegate protocol). If you're unsure about how to use delegates I strongly suggest you do some reading of the delegate design patterns and Apple Developer documentation, as it's used throughout iOS development.
In your UIWebViewDelegate class, you need to implement the following method as follows:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
myTextField.text = [[myWebView.request URL] absoluteString];
}
This will ensure the text field always displays the url of the page that last started to load (there are other UIWebViewDelegate methods if you'd rather the text field be updated at a different time).
Hope this helps.
In .H file u declare webview, textfield and IbAction to browse the text u enter and it looks like
{
IBOutlet UIWebView *web;
IBOutlet UITextField *text;
}
-(IBAction)search:(id)sender;
and .M file looks in IBaction
-(IBAction)search:(id)sender{
NSLog(#"%#",text.text);
NSString *urlAddress = [NSString stringWithFormat:#"http://%#",text.text];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
}
surely this works
You should define – webViewDidStartLoad: method of id delegate of your UIWebView:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
yourUITextField.text = [[webView.request URL] path];
}
All from documentation
There is a method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType in the webview delegate.
This method will call for every request load in the web view.
In this you can get the request Object. From that you can get the URL.
You can directly paste the URL in the textField in this method.
Related
I'm trying to load a UIWebView, but I can't understand why doesn't work, I'm doing this:
Loading *load = [[Loading alloc] init];
[load searchName];
then Loading.h
#interface Loading : NSObject <UIWebViewDelegate>
{
UIWebView *myWebView;
}
- (void) searchName;
Loading.m
- (void) searchName{
myWebView = [[UIWebView alloc] init];
myWebView.delegate = self;
NSString *urlAddress = #"www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[myWebView loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
NSString *yourHTMLSourceCodeString = [myWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
NSLog(#"%#",yourHTMLSourceCodeString);
}
why never call webViewDidFinishLoad method?
The reason your webview is not displayed is you have taken NSObject class and NSObject classes dont have an XIB and the way you have done is just calling your -(void)searchName method and is not adding it to self.view.
Did you created this webview with IB? Did you connected the delegate to the webview?
Try to catch errors with webView:didFailLoadWithError: (post them if there are errors)
Try to set the delegate with code [myWebView setDelegate:self]; or self.myWebView.delegate = self;
Check output of delegate if nothing happend:
self.myWebView.delegate = self;
NSLog(#"%#",self);
Yes, take note into what Nathan said. I am just expanding.
If you are using IB, connect the outlet. Then use [myWebView setDelegate:self];
Be sure to use http://. Http on it's own won't do anything.
Can you get other URL's to load?
Finally, I have an open source basic web browser for Cocoa that you can take a look at.
Basic Web Browser
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;
}
Hi I have a url say http://www.foobar.com
[webView loadRequest:[NSURLRequest requestWithURL:appURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0
]];
Now when this url is formed i can set the urlString to have webtype=iphone
But for every single request that comes from there onwards I need to add webType=iphone to back of the string.
I figured there is some way using
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
But didnt get any solution through it yet... any help
just create a custiom method returning the cusom url:
- (NSURL *)customURLWithPramString:(NSString *)pramString{
return [NSURL URLWithString:[NSString stringWithFormat:#"http://www.foobar.com?%#&webtype=iphone",pramString]];
}
Then you can just go: [webView loadRequest:[NSURLRequest requestWithURL:[self customURLWithPramString:#"name=123&age=123"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]];
where you pass "name=123&age=123" in this case
To do this, either subclass UIWebView with a simple wrapper that only implements a custom loadRequest or subclass NSURLRequest to change the customURL as JNK said above. It's probably easier to do the later tho.