NSURLRequest timeOut handling - iphone

I've been reading some issues here with NSURLRequest timeOut, but I see no answer to my problem.
Well, here it is: my NSURLRequest object works fine, but I need to show some alert to the user if the connection time out is higher to 3. And i dun know how to handle that timeout.
Any help is really appreciate it.
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3] ;
[webView setDelegate:self];
[webView loadRequest:requestObj];

Define a delegate for the web view -- add <UIWebViewDelegate> in the #interface definition of your view controller and then link it up in Interface builder
then add a method
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// whatever you want to do if it times out (or some other error)
}

Related

Why is UIWebView not loading data from cache on iOS4?

Hey guys in my APP i have created custom class for cache by inheriting NSURLCache class and implemented required methods to pass my local files data as cached data.I have done it same as this tutorial
In my APP there is one refresh button,on click of it i have to refresh the current page for this i have done following to load locally stored CSS and JS files
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (isGoingToRefresh) {
[NSURLCache setSharedURLCache:cache];
isGoingToRefresh = NO;
return YES;
}
NSString *path = [mainWebView stringByEvaluatingJavaScriptFromString:#"window.location.pathname"];
path = [NSString stringWithFormat:#"%#%#",[[[AppDelegate sharedAppDelegate]configurationFile]objectForKey:#"RootURL"],path];
[NSURLCache setSharedURLCache:cache];
if ([path isEqualToString:[[request URL]absoluteString]]) {
[self showRefreshLoader];
isGoingToRefresh = YES;
NSURLRequest *localRequest = [NSURLRequest requestWithURL:[request URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:240] ;
[webView loadRequest:localRequest];
return NO;
}
return YES;
}
Above code is loading data from cache by calling following NSURLCache method -
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
But on iOS4 webView not calling above NSURLCache method.
I am not getting why please suggest me some good solution for it.
try using to make your request
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLCacheStorageAllowed
timeoutInterval:60];

UIWebView links opening in Safari

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)];

UIWebView - Add an extra parameter to the url request

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.

Making a request to a web service for logging purposes in Objective-C

I have a web service with a URL like this: http://webservice.com/?log=1
I would like to make a request to this URL in Objective-C such that the web service can log that fact. I don't care to pass anything on to the web service and nor do I care how it responds to the request.
What is the most effective way to achieve this?
The simplest way is to use the NSURLConnection sendSynchronousRequest:returningResponse:error: method.
e.g.
NSURLResponse *response;
[NSURLConnection sendSynchronousRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: #"http://webservice.com/?log=1"]] returningResponse: &response error: NULL];
The downside to using this method is that it will hang your app while it's performing the URL request. To do this request asynchronously you need to use the connectionWithRequest:delegate: method. This gets a little more complex as you need to provide a delegate (in your case, one that does nothing).
e.g.
[[NSURLConnection connectionWithRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: #"http://webservice.com/?log=1"]] delegate:self] retain];
...
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
}
See SimpleURLConnections for more examples of using NSURLConnection.
UPDATE: removed optional delagate methods as per David's comment.

NSURLConnectionDelegate connection:didReceiveData not working

I need some help regarding the NSURLConnectionDelegate method.
- (void)startDownload {
NSString *URLString = [NSString stringWithFormat:appRecord.imageURLString];
NSURL *url = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
imageConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(imageConnection) {
activeDownload = [NSMutableData data];
}
}
I am using this method to initiate the NSURLConnection, but the
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
is not calling.. Need Help
Thanks in advance,
Shibin
No single answer but:
1) Put some NSLogs in to display the URL and then validate that it is generated correctly and does return data
2) Check that you have properly declared that you conform to the NSURLConnectionDelegate protocol in the .h
3) Are you threading or messing with the runloops ? " Messages to the delegate will be sent on the thread that calls this method. By default, for the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode."
Sorry but do you do the start in your code ? I don't see it in your extract.
There should be a
[imageConnection start]
somewhere in your code to trigger the start of the connection and get your delegate called asynchronously.