The problem is that the website doesn't fully load in a UIWebView, but it loads normally in Safari.
This is the error I get when loading the website in a UIWebView:
Warning:
cos_before_render(/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob)
[function.cos-before-render]: failed
to open stream: No such file or
directory in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 731
Warning: cos_before_render()
[function.include]: Failed opening
'/home/user/ctown/doc_wbn//../sys/swt/www.westspringsec.moe.edu.sg.mob'
for inclusion
(include_path='.:/home/user/ctown/cti_bin/phplot:/usr/local/lib/php')
in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 731
Fatal error: Call to undefined
function: json_encode() in
/home/user/ctown/cti_bin/wbn/cos_init.inc
on line 737
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
[super viewDidLoad];
}
I tried the question linked below, however I can't modify the backend of the website!
UIWebView Xhmtl parse error but safari don't
The error is not within your code! The UIWebView, especially from within the simulator uses a user agent something like this:
Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C134
When you call the url provided it results in the error you are seeing. It's a server side error.
You can alter your user agent by using this code, put it somewhere in the startup phase of your app so it's only set once:
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#"Safari/528.16", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[dictionnary release];
With this I was able to open your page. It looks like the site returns different markup for the iphone, so the result with the code above will show the website, which is a bit large for the small display.
UIWebView and Safari does not have the same user agent.
This answer won't fix your issue (see Nick's answer), but you should put [super viewDidLoad]; before you do anything else. So:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = #"http://www.westspringsec.moe.edu.sg";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[Webview loadRequest:requestObj];
}
Related
I have a webview app tool, which essentially consists of the webview and two buttons on a toolbar. One button to view the source of the page, and another button to view/change the current User Agent.
I have both functions working on iOS 5 (view source, and change User Agent), but I cant seem to grab the User Agent in iOS 4.x.
I'm using the following now:
userAgentViewController.UAText = [self.webView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
This works in iOS5, but in iOS 4.x, it doesnt return anything. Is there a way to achieve the same functionality in iOS 4.x?
Thank you!
to get the useragent, check the HTTP header in the NSURLRequest of your response.
You can retreive this one in the webViewDidFinishLoad delegate method.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"%#", [[webView request] valueForHTTPHeaderField: #"User-Agent"]);
}
to set it, you have to custom an NSMutableURLRequest and give it to your webView
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:#"yourwebsite.com"]];
[request setHTTPMethod:#"POST"];
[request setValue:USERAGENT_STRING forHTTPHeaderField: #"User-Agent"];
[webView loadRequest:request];
[request release];
and that's it !
Try this in the AppDelegate.m
+ (void)initialize
{
// Set user agent (the only problem is that we can’t modify the User-Agent later in the program)
// iOS 5.1
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#”Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3”, #”UserAgent”, nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
}
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 WebView loading a Twitter mobile user's wall:
NSString *urlAddress = [NSString stringWithString:#"http://mobile.twitter.com/gatrecords"];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
When web is loaded it freezes for 10- 15 secs then I can navigate normally. On the iPhone's Safary Browser happens the same. That behavior only happens with loading mobile.twitter plus a user's wall.
On the simulator all is ok.
Any suggostions on what could be wrong ?
I found a workaround for the freezeing. Apparently WebView freezed bacause tries to execute twitter's javascript for 10 seconds, after that time the system will stop trying and the WebView will be scrollable. So I stopLoading only when twitter's web is being loaded:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *twitter = [NSString stringWithFormat:#"%#", [[myWebView request] URL]];
if ([twitter isEqualToString:#"http://mobile.twitter.com/gatrecords"]) {
[myWebView stopLoading];
}
}
Then I get an error alert after the 10 seconds regarding javascript did not load correctly, I fix that as follows:
- (void)webViewDidStartLoad:(UIWebView *)webView {
[myWebView stringByEvaluatingJavaScriptFromString:#"window.alert=null;"];
}
It looks like a few people on stackoverflow get this to work but their code isn't posted. I'm using
[web loadData:data MIMEType:MIMEType textEncodingName:#"UTF-8" baseURL:nil];
where MIMEType is:
#"application/vnd.ms-powerpoint"
#"application/vnd.ms-word"
#"application/vnd.ms-excel"
(BTW, I've seen DOC files use mimetype #"application/msword" but the "vnd" version seems more appropriate. I tried both just in case.)
I verified that my 'data' is correct. PDF and TXT files work. When the UIWebView displays PPT, DOC, or XLS files, it's blank. I put NSLOG statements in my UIWebViewDelegate calls.
shouldStartLoadWithRequest:<NSMutableURLRequest about:blank> navType:5
webViewDidStartLoad:
didFailLoadWithError:Error Domain=NSURLErrorDomain Code=100 UserInfo=0x122503a0 "Operation could not be completed. (NSURLErrorDomain error 100.)"
didFailLoadWithError:Error Domain=WebKitErrorDomain Code=102 UserInfo=0x12253840 "Frame load interrupted"
so obviously the load is failing, but why? If I change my mimetype to #"text/plain" for a PPT file, the UIWebView loads fine and displays unprintable characters, as expected. That's telling me the 'data' passed to loadData: is ok.
Meaning my mimetypes are bad?
And just to make sure my PPT, DOC, and XLS files are indeed ok to display, I created a simple html file with anchor tags to the files. When the html file is displayed in Safari on the iPhone, clicking on the files displays correctly in Safari.
I tried to research the error code displayed in didFailLoadWithError (100) but all the documented error codes are negative and greater than 1000 (as seen in NSURLError.h).
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(#"didFailLoadWithError:%#", error); }
Have you tried using the following documented method?:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
// Calling -loadDocument:inView:
[self loadDocument:#"mydocument.rtfd.zip" inView:self.myWebview];
It works for these in iPhone OS 2.2.1:
Excel (.xls)
Keynote (.key.zip)
Numbers (.numbers.zip)
Pages (.pages.zip)
PDF (.pdf)
Powerpoint (.ppt)
Word (.doc)
iPhone OS 3.0 supports these additional document types:
Rich Text Format (.rtf)
Rich Text Format Directory (.rtfd.zip)
Keynote '09 (.key)
Numbers '09 (.numbers)
Pages '09 (.pages)
The only way i found to read an Office object (tested with .doc or .xls) is to save the NSData object in a temp file, then read it.
-(void)openFileUsingExtension:(NSString*)extension {
NSString *path = [NSString stringWithFormat:#"%#temp.%#",NSTemporaryDirectory(),extension];
NSLog(#"%#",path);
if ([objectFromNSData writeToFile:path atomically:YES]) {
NSLog(#"written");
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:request];
self.webview.scalesPageToFit = YES;
self.webview.delegate = self;
[self.view addSubview:self.webview];
}
}
then you can remove the file inside the UIWebViewDelegate method:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *path = [NSString stringWithFormat:#"%#temp.%#",NSTemporaryDirectory(),extension];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0, 1000, 760)];
[webView setScalesPageToFit:YES];
webView.backgroundColor=[UIColor clearColor];
NSString *ppt = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"ppt"];
NSURL *pptURL = [NSURL fileURLWithPath:ppt];
NSURLRequest *request = [NSURLRequest requestWithURL:pptURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Did you try this on the device or in the simulator only?
I find that I cannot get UIWebView to display .doc and .pages documents in the simulator (but can display .txt and .pdf ones), but both file types load and display just fine on both the iPad and iPhone devices.
Bug in simulator?
Gregor,
Sweden
After searching thru, the solution I found was because when we import xls into Xcode by drag and drop the xls file into Project->Supporting Files, we need to specific the 'Add to targets:' , we need to tick the project to add
I am trying to build an RSS reader for the Tom's Hardware website.
I have an error when I try to load an URL into an UIWebview from an RSS link.
Here'is my code:
- (void)viewDidLoad {
[super viewDidLoad];
if (self.url != nil) {
NSURL *requestUrl = [NSURL URLWithString:self.url];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
[webView loadRequest:requestObj];
}
}
The url is setted by the parent controller.
When the URL comes directly from the RSS, I have an error in log:
[2234:207] Error Domain=WebKitErrorDomain Code=101 UserInfo=0x3a6a240 "Operation could not be completed. (WebKitErrorDomain error 101.)"
When I set manually the URL with the same URL like below, it work !
self.url = #"http://www.presence-pc.com/tests/fraude-financiere-paget-macafee-23198/#xtor=RSS-12";
Here is an URL exemple: http://www.presence-pc.com/tests/fraude-financiere-paget-macafee-23198/#xtor=RSS-12. I have no idea about that problem.
I hope you can help me.
Best regards.
Thanks guy, that'it, I have split the URL like this:
NSArray *split = [url componentsSeparatedByString:#"#"];
NSURL *requestUrl = [NSURL URLWithString:[[split objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
and now it work , thanks for your help !
I have this code which also triggers error code 101
_request = [NSURLRequest requestWithURL:[NSURL URLWithString:redirectUrl]];
[editorWebView loadRequest:_request];
RedirectURL is an NSString which has this format http%3A%2F%2Fwww.linkedin.com%2Fnhome%2F
I tried removing percent escapes using the method of NSString and it now works.
[redirectUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I think the problem can be the anchor in your URL, a friend got the same problem