As ios documentation says, [webView stopLoading] method should be used in order to stop webview load task.
As far as I see, this methods runs asynchronously, and does NOT stop currently processing load request immediately.
However, I need a method which will force webview to immediately stop ongoing task, because loading part blocks the main thread, which could result in flicks on animations.
So, is there a way to succeed this?
This worked for me.
if (webText && webText.loading){
[webText stopLoading];
}
webText.delegate=nil;
NSURL *url = [NSURL URLWithString:#""];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webText loadRequest:requestObj];
Don't use the UIWebView to download your html document directly.
Use async download mechanism like ASIHTTPRequest to get your html downloaded by a background thread.
When you get the requestFinished with a content of your html then give it to the UIWebView.
Example from the ASIHTTPRequest's page how to create an asynchronous request:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
Use the responseString to your UIWebView's loadHTMLString method's parameter:
UIWebView *webView = [[UIWebView alloc] init];
[webView loadHTMLString:responseString baseURL:[NSURL URLWithString:#"Your original URL string"]];
Related
I'm performing asynchronous request with ASI HTTP Request and I want to update Textbox with new information from the request, so I'm updating it from callback function.
Here is my code so far:
Second Class
- (void)Login {
NSLog(#"Login");
NSURL *url = [NSURL URLWithString:#"http://ts5.travian.sk/login.php"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(loginRequestFinished:)];
[request setDidFailSelector:#selector(loginRequestFailed:)];
[request startAsynchronous];
}
- (void)loginRequestFinished:(ASIHTTPRequest *)request
{
NSLog(#"Completed!");
NSString *response = [request responseString];
AppController *ac = [AppController getInstance];
[ac.textbox performSelectorOnMainThread:#selector(setStringValue:) withObject:response waitUntilDone:NO];
}
AppController is a main class. Setting text from there is working. But this code isnt doing anything. It just wrote 2 log lines to debug window.
Am I missing something?
I am trying to display web pages using UIwebview on iPad.
I used following code :-
NSString *urlAddress=#"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSLog(#"The value of url in screen 2 is %#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
web is an object of type uiwebview .
This works fine with certain links but when i tried to open below link
http://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#infohttp://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#info
It does not display anything .Read few articles and taught this could be related to a proxy issue
How to deal with proxy setting in UIWebView?
so tried to use this but no success . code used is as below :
NSURL *url = [NSURL URLWithString:#"http://google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(#"error is %#",response);
}
But now nothing loads up in webview .how do i tackle this .
I'm looking to create a simple iOS app that displays the current water level of a local lake. The water level is updated daily on a specific URL. Is it possible to pull content from a webpage using objective c?
Sure. Check out the URL Loading System Programming Guide. From that link:
The URL loading system provides support for accessing resources using the following protocols:
File Transfer Protocol (ftp://)
Hypertext Transfer Protocol (http://)
Secure 128-bit Hypertext Transfer Protocol (https://)
Local file URLs (file:///)
Absolutely! Use the NSURLConnection object. Use something like the function below, just pass an empty string for 'data' and then parse the HTML returned to find the value you're looking for.
-(void)sendData:(NSString*)data toServer:(NSString*)url{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn){
//Connection successful
}
else{
//Connection Failed
}
[conn release];
}
Easier way yet using threading:
- (void)viewDidLoad
{
[self contentsOfWebPage:[NSURL URLWithString:#"http://google.com"] callback:^(NSString *contents) {
NSLog(#"Contents of webpage => %#", contents);
}];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void) contentsOfWebPage:(NSURL *) _url callback:(void (^) (NSString *contents)) _callback {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:_url];
dispatch_sync(dispatch_get_main_queue(), ^{
_callback([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
});
});
}
I am trying to find a way to load a website (for server call purposes) but I don t want it to open neither in safari nor in a UIWebView . I usually use this code for opening in safari
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"http://www.google.com"]];
and for the UIWebViews
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thelink]]];
I tried to use the UIWebView without actually having a physical one but the page didn't load (I know that because the server call were not made at all)
thanks
You should use NSURLRequest, for example:
NSString * url = #"http://myapi.mysite.com/api.php";
NSMutableURLRequest * aRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSHTTPURLResponse * httpResponse = nil;
NSError * urlError = nil;
NSData * responseData = [NSURLConnection sendSynchronousRequest:aRequest returningResponse:&httpResponse error:&urlError];
Where responseData is the body of the response, you can use httpResponse to check status codes etc...
NSURLConnection is your friend:
NSURL *URL = [NSURL URLWithString:#"http://www.google.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:URL];
// configure req to have post data and such
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
// do something with data
Just use
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"] encoding:NSStringEncodingConversionAllowLossy error:nil];
if connected is != NULL then everything worked fine
I'm developing an iOS app which gets its data from a json feed of my server. Parsing the string is no problem... However, I am having trouble downloading the string asynchronously and then caching it. I found SDURLCache but do not know how to implement it. What is the best way to do this.
You could download the file to disk (synchronously):
NSURL * url = [NSURL URLWithString:#"www.yourprovider.com/your.json";
NSData *file = [NSData dataWithContentsOfURL:url];
[file writeToFile:<your file path> atomically:YES];
For asynchronous operations, instead, you should use NSURlConnection. After opening the connection,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:<your NSURL>];
[request setHTTPMethod:#"GET"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
you receive data in this call back:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {
//here you could write to a NSFileHandle ivar:
if (file) {
[file seekToEndOfFile];
} [file writeData:_data];
}