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
Related
I use a WebViewController to visit some URL's in an app.
Now I found out that the apps size slowly increases every time using the WebViewController.
It is when checking app size under iPhone usage and the Document and Data for that specific app increases for every time it is used. The app can be 2 Mb when installed but after a lot of usage it may easily increase to 25 Mb. How can I release that fingerprint when closing the app??
When calling for the WebViewController this is how it looks.
- (void) showURL:(NSURL *)address withTitle:(NSString *)title
{
MyWebViewController *vc = [[MyWebViewController alloc] initWithNibName:#"MyWebViewController" bundle:nil];//ny
vc.title = title;
vc.location = address;
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
h. file in WebWiewController has this in its content.
NSURL *location;
IBOutlet UIWebView *theWebView;
and the m.file has this
- (void)viewDidLoad
{
[super viewDidLoad];
[theWebView loadRequest:[NSURLRequest requestWithURL:location]];
}
It seems that your Webview is caching the page.
Maybe you should manually create your http request to custom the cache policy.
for example :
NSURL *url = [NSURL URLWithString:URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[webView loadRequest:request];
I did not try the code, it's just to give an idea.
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 a simple iPhone app which will display html content I have placed in the Documents directory, but once it is displayed, the links do not work.
The following code is called from the init method of my app delegate.
Can anyone suggest what I have missed please?
-(void) loadWebView:(NSString*) appDirectory {
CGRect rect = CGRectMake(0, 0, 320, 480);
webView = [[UIWebView alloc] initWithFrame:rect];//init and create the UIWebView
[webView setBounds:rect];
// NSString* webViewFile = [appDirectory stringByAppendingString:#"index.html"];
// NSString* protocol=#"file://";
// NSString* fullUrl=[protocol stringByAppendingString:webViewFile];
fullUrl=#"http://www.google.com";
NSLog(#"Attempting to open url (unencoded) %#", fullUrl);
fullUrl = [fullUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"Attempting to open url (encoded) %#", fullUrl);
//Create a URL object.
NSURL *url = [NSURL URLWithString:fullUrl];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
webView.delegate=self;
[webView loadRequest:requestObj];
window = [[UIWindow alloc] init];
[window addSubview:webView];
[window makeKeyAndVisible];
window.hidden=NO;
}
I don't think you should create your own UIWindow object. This object is already in your xib and it is probably "madeKeyAndVisible" already in the applicationDidFinishLoading method.
Try to remove all lines relating to window, and add the subview to self.view, like
[self.view addSubview:webView];
The answer turns out to be that there are two clipping functions: one for display and one for interaction.
I had set my bounds to the screen size, but not my frame.
Obvious? Not really.
I have trouble accomplishing something that sounds very simple. Using Interface Builder, I created a UIWebView object. Next, I created a new controller - HomeViewController and assigned it to the WebView under "NIB Name" property.
Next I need to load a web site (let's say http://google.com) into that WebView when user accesses that view. I am not able to find any examples on how to actually accomplish this. Please provide a code example. Sorry about the newbie question. I did RTFM, but no luck.
The .h file:
#interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
The .m file:
#import "WebViewController.h"
#implementation WebViewController
// Loads the url when the view loads
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
#end
try this :
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]]; // webview - your Uiwebview object
I am wondering if anyone can advise on the best way to add a FAQ page in iphone app. I would like to display a 'local' file (perhaps HTML) with local images into a web view. Essentialy to enable users access to FAQ within the app.
Any advice on whether to use HTML or any other way of doing this will be very helpful..
Thanks in advance..
mb
I used a UIWebView and loaded a local index.html file into this.
**HelpViewController.h**
#interface HelpViewController : UIViewController {
UIWebView *webView;
}
#property (retain) IBOutlet UIWebView *webView;
#end
Obviously you will need to #sythesize webView.
**HelpViewController.m**
- (void)viewDidLoad
{
self.title = #"Help";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleDone target:self action:#selector(btnReturn)];;
NSString *helpPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:helpPath isDirectory:NO];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(#"Help loaded");
}
Hope this helps. Also, just a note, if you are presenting this as a modal, it will appear blank for a split second until the HTML file loads into the UIWebView. I got around this using:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self performSelector:#selector(showAboutWebView) withObject:nil];
}
- (void)showAboutWebView {
[webView setHidden:NO];
}