I have been trying to load the url http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true into a webview but it shows up an error 'Entity nbsp not found'.
The link works properly over Safari (Machine as well as simulator) but doesnt load properly when loaded through a webview. Can someone point me as to how to do it?
#define kEventsCalenderLink #"http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true"
NSURL *eventsURL = [NSURL URLWithString:kEventsCalenderLink];
eventsWebView.delegate = self;
[eventsWebView loadRequest:[NSURLRequest requestWithURL:eventsURL]];
The server is serving that page with the Content-Type of "application/xhtml+xml" to the Simulator version of Safari. Safari is interpreting the page as strict XML, which does not have an entity.
Unfortunately, UIWebView manipulates its HTTP headers behind the scenes, and it's seemingly impossible to configure it to make a request that will cause your server to serve content as "text/html"
The one workaround I would suggest is to retrieve the content with an NSURLConnection, then feed it to your web view with the loadData:MIMEType:textEncodingName:baseURL: method, being sure to pass "text/html" as the MIME type.
This may also necessitate intercepting future requests via the webView:shouldStartLoadWithRequest:navigationType: delegate method (in order to cancel them and load them in the manner described above to prevent similar problems).
Or, if you have control of the server, you could configure it to serve HTML with the appropriate content type. Or convert the content to use proper XML entities.
Do it in this way:
NSString *kEventsCalenderLink #"http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true"
NSLog(#"%#",kEventsCalenderLink);
eventsWebView.delegate = self;
eventsWebView.userInteractionEnabled = true;
[eventsWebView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:kEventsCalenderLink]]];
You have only defined the URL which you haven't mentioned that it's string type.
Here is the sample code which i implemented.Hope it would help you
- (void)loadView
{
UIView *vew=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.view=vew;
//self.title=#"sanjay";
//self.navigationItem.title=#"afdsfasd";
self.view.backgroundColor=[UIColor redColor];
[vew release];
UIButton *smsbtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
smsbtn.frame=CGRectMake(50, 280, 100, 50);
[smsbtn setTitle:#"SMS APP" forState:UIControlStateNormal];
[smsbtn setTitleEdgeInsets:UIEdgeInsetsZero];
[smsbtn addTarget:self action:#selector(loadsms) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:smsbtn];
}
-(void)loadsms
{
NSString *url = [NSString stringWithFormat:#"http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
}
This code is working dude.Try this out.I have implemented this for you only.
Related
im developing an app, which requires to load some menu's and presentations(pdf, flash etc.)
menu items are loaded from database and each contains ID and Text. Menu items looks like below:
Crocin
Combiflame
above menu items are hyper links, clicking them will open up presentations on the same page.
i need to call a xcode method in this scenario which can query database (located in document folder in app) and return presentation name and some other details.
i got two problems here
1) i came across
**– webView:shouldStartLoadWithRequest:navigationType:**
but this method returns BOOL and i want to return Presentation with other information
2) no idea how can i load presentation (inside Resources Folder of my app ) via javascript on the same page.
Any suggestions will b greatly appreciated. Thanks in advance.
You can do pretty much what you need in webView:shouldStartLoadWithRequest:navigationType.
If you return NO from it, then the web view will not load the current request; this will make your app free of doing whatever it needs:
you can then either load a different request in the same web view (say an HTML string that embeds the presentation resource),
or, you can send a message to some controller of yours to run a different workflow altogether (display a different kind of view or whatever).
EDIT:
elaborating more option 2:
say that you have a view controller which embed the UIWebView and also acts as delegate for it;
you could define a method in the view controller which gets and HTML string and loads it into the web view, like this:
- (void)loadContentInWebView:(NSString*)content {
[_webView loadHTMLString:content baseURL:nil];
}
now, in webView:shouldStartLoadWithRequest:navigationType, you could do something like this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] isEqual:WHATEVER]) {
[self loadContentInWebView:HERE_THE_HTML];
return NO;
}
return YES;
}
What you should define here is:
([[[request URL] absoluteString] isEqual:WHATEVER])
i.e., the logics to understand when the user touched some link that should make a presentation loaded;
and:
[self loadContentInWebView:HERE_THE_HTML];
i.e., define in some way the HTML content that you want to use so to have the presentation displayed.
i got a work around..although it is not a good practice:
in model:
This method gets called whenever a request comes for this webview even when the page loads in webview
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// check the request, if it's ajax callback, just return NO; other wise YES;
// NSURLRequest *request = [self request];
NSURL *url = [request URL];
NSString *file = [url lastPathComponent];
if ([file isEqualToString:#"Presentation.html"] == YES) {
return YES;
}
if ([file isEqualToString:#"ajaxcallback.htm"] == YES) {
// that means the ajax call is done, so you can call this
NSString *result = [web stringByEvaluatingJavaScriptFromString:#"getResponse();"];
// don't forget to stop loading this fake page
return NO;
}
}
and in javascript:
function sendRequestToApp(){
window.location="http://www.domain.com/ajaxcallback.htm";
}
function getResponse(src){
alert("response");
document.getElementById("sample").src=src;
}
I'm making an iPad browser, and I have a little problem. When I go to google, for example, I write the google direction in a textField. The problem is that when I change the web page, it still says http://www.google.com/. Here's my code:
-(IBAction)buttonpressed2:(id)sender {
url = [NSURL URLWithString:[textField text]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView isEqual:textField.text];
}
How can I make the textField show the page that I'm watching right now, and not the first url typed in the field?
First, make sure when you set up the webView, you assign the UIWebViewDelegate to self
Then, implement the delegate method: webViewDidFinishLoad, as such:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSURLRequest *currentRequest = [webView request];
NSURL *currentURL = [currentRequest URL];
//This will log the current url
NSLog(#"Current URL is %#", currentURL.absoluteString);
//Then to display it in the textField
textfield.text = currentURL.absoluteString;
}
Edit:
A delegate is a special set of methods that wait for an event to happen. When that event happens, the appropiate method is called. The method above is one of the delegate methods for a UIWebView - When a specific event happens with the webView, in this case when the webViewDidFinishLoad, that method is automtically called and we can respond as such. In order to set the delegate, we can proceed one of two ways:
If you created the webView in code, this is with something like this:
UIWebView *webView = [[UIWebView alloc] init...]
then all you need to do is add the following line of code:
webView.delegate = self;
However, you might have also created the webView in the interface builder - If that's the case, click on the webView and the linker tab, and drag the delegate option over to "File's Owner".
and thats it.
Your class should implement the UIWebViewDelegate protocol. Then in the method webView:shouldStartLoadWithRequest:navigationType: you can do this:
[textField setText:[[request URL] absoluteString]];
This way the value in the textField will be updated whenever you navigate to a new url.
I'm trying to get this control to work with a hypertext link with not much success. I have looked at TTCalaog and tried to replecate but does not work.
I have this working as far as displaying the hypertext link but it does not fire.
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:CGRectMake(5, 0, 315, 175)] autorelease];
NSString* labelText = #"This should work";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[self.view addSubview:label];
I thing I am missing the point here perhaps with the placement of the google url? I have seen a post on this forum that makes use of custom-uri://some/url that is then set up in TTURLMap and TTNavigator, but I need to open a url from the hypertext in a webview so I need the url to run a method in my class that creates my webview controller etc.
I have tried to cusomise TTURLMap to work without a TTNavigator but completely pickled?
Any help gratefullt appreciated ;-)
Thanks
I've just found myself a solution to catch the clicked URL on a TTStyledTextLabel. I hope this could help in your case too.
This's what I have done.
1. Create TTNavigator
TTNavigator *navigator = [TTNavigator navigator];
navigator.persistenceMode = TTNavigatorPersistenceModeNone;
navigator.delegate = self;
2. Create TTNavigatorDelegate
As you assigned self as delegate of the navigator object. Therefore, please remember to add protocol in the header file .h before you continue.
In the implementation, create this method
- (BOOL) navigator:(TTBaseNavigator *)navigator shouldOpenURL:(NSURL *)URL {
// Now you can catch the clicked URL, and can do whatever with it
// For Example: In my case, I take the query of the URL
// If no query is available, let the app open the URL in Safari
// If there's query, get its value and process within the app
NSString *query = URL.query;
if (query == nil) {
return YES;
} else {
// process the query
}
}
I hope this helps! Please vote for me if this helps to solve your issue!
Best Regards,
Thang
I am trying to inject JQuery into a UIWebView of a page that I can't control, say Google, for example. When a UITextField gets focus, I am executing the following lines of code:
NSString* scriptInject = #"var headElement = document.getElementsByTagName('head')[0]; var script = document.createElement('script');
script.setAttribute('src','http://jquery.com/src/jquery-latest.js');
script.setAttribute('type','text/javascript'); headElement.appendChild(script);";
[myWebView stringByEvaluatingJavaScriptFromString:scriptInject];
[myWebView stringByEvaluatingJavaScriptFromString:#"$(document).ready(function() {$('body').css('background', '#FF0000');}"];
I am executing the background change to validate that I am able to run a JQuery script. I can verify that the Objective-C method is being called, and I even planted an alert for the onload of the newly created <script> tag, so I know that it is being executed in the UIWebView. How do I inject it correctly that I can execute it?
EDIT:
I have event tried this after calling the function to change the color:
[myWebView stringByEvaluatingJavaScriptFromString:#"alert($('body').css());"];
No alert shows.
This has worked for me:
NSString *jqueryCDN = #"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
NSData *jquery = [NSData dataWithContentsOfURL:[NSURL URLWithString:jqueryCDN]];
NSString *jqueryString = [[NSMutableString alloc] initWithData:jquery encoding:NSUTF8StringEncoding];
[webView stringByEvaluatingJavaScriptFromString:jqueryString];
[webView stringByEvaluatingJavaScriptFromString:/*jquery commands here*/];
Taken from this article.
If jQuery is not in the DOM already you can use stringByEvaluatingJavaScriptFromString: to inject it. I've had similar problem, I wrote a blog post about it: HTML parsing/screen scraping in iOS.
All I want is to display some simple text in my viewController and have the hyperlinks autoparsed. When the user clicks on the link I want the control to somehow do a callback where I can do something with the URL. How can I achieve this?
I've already looked through the TTCatalog for hours. I have also tried looking into the source code of three20 as well as looking at the stack trace. No help. I just can't figure out how my app can react to the click of the URL. Any hints please?
Hard to help without seeing what you've already tried, but you should be able to do something like the following:
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc]
initWithFrame:someFrame] autorelease];
NSString* labelText = #"This should work";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];
You can then use TTNavigator and TTURLMap to map custom-uri://some/url to a particular controller in your application, or handle it yourself in your application delegate. The best place to find out how to do that is by looking at the TTNavigatorDemo sample application included in the Three20 source. Specifically, look at AppDelegate.m which is where all the URL mapping gets performed.
In addition to what Nathan says about URL mapping and links, you can also use CSS styles!
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:someFrame] autorelease];
NSString* labelText = #"This should work and
<span class=\"redText\">this should be red</span>";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];
Then in your StyleSheet.m implement
- (TTStyle*) redText {
return [TTTextStyle styleWithFont:[UIFont systemFontOfSize:12] color:RGBCOLOR(255,0,0) next:nil];
}