How to manage scheduled maintenance in iOS Application? - iphone

I am looking for a way to be able to handle scheduled maintenance in iPhone app. We are using web services(.Net/C#) in our application and recently we have increased number of servers to maintain trafic on website. So whenever web team uploading website, they are displaying website is on scheduled maintenance. for iPhone, i have written below code to handle timeout at the same time.
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
it will notify to user after 60.0 Sec. so users are complaining about Freezes iPhone application. Is there any other way to handle this? or Is there any other way to open any particular screen at the time of scheduled maintenance?
Any help would be greatly appreciated! Thanks.

iOS 7 silent background notifications might work for you in this case.

You need to execute this code on a separate thread. The easiest way to do this is using Grand Central Dispatch, like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURLResponse *response = nil;
NSError *error = nil;
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSData *resultData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
// Add code to handle returningResponse.
// Error handling
if (error != nil) {
NSLog(#"An error occured! %#", error);
}
});

Related

save sign up view controller text values on the server through php web service call using post method in iOS

i have sign up form in which i have the fields like first name,last name,gender,email,password,user image,age. i want to make save this user information on the server through php webservice call.how can i pass these parameters to service url? will i use the http request using post method to save the user info to server and when that user wants to log in iOS app , i may match the credencial for that user. in shore, i need a log in and sign up screen for my iOS app, please suggest me any tutorial
Thanks in advance to all of you
First you need nto create php script and then transfer data through NSURL.
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.0.6/savedevicedata.php?Udid=%#&Flag=%d&DateTime=%#",uniqueIdentifier,Flag,FinalDate]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSLog(#"%#",request);
[request setHTTPMethod:#"POST"];
//[request setHTTPBody:[NSString stringWithFormat:#"%#",rawStr]];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(#"responseData: %#", responseData);
Try this Code just replaces the your field.

Post comment to WordPress Blog from iPhone programmatically

I installed the WordPress blog on my localhost server and also made an iPhone app to browse the blog via rss. I tried to post a comment programmatically using this code.
#define post_url #"http://localhost/web-wp/wp-comments-post.php"
#define post_content #"comment_post_ID=%#&comment_parent=%#&author=%#&email=%#&comment=%#"
NSString *post_str = [NSString stringWithFormat:post_content, #"1", #"0", #"Viet", #"vietnt88#gmail.com", #"test. comment written on mobile"];
NSData *data = [post_str dataUsingEncoding:NSUTF8StringEncoding];
NSURL * url = [NSURL URLWithString:post_url];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:data];
NSURLResponse *response;
NSError *err;
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
I need this code to work when the user is not logged in. How do I achieve that?
How can I post comment from iPhone?
First of all, if you use "localhost" from your code running on your iPhone, then "localhost" will refer to the iPhone not to your web server. Put there the IP of your server, if you have a public IP than that one otherwise connect your iPhone via WiFi to the same LAN as your local server and use the IP of that server (I guess it'll be something like 192.168...).

when is a response code received during a request for an image using ASIHTTPRequest?

When do I actually get my response code 200 for a valid request for an image? Is it after all of the data has been downloaded to my browser or which ever device is requesting the image?
I am using the library http://allseeing-i.com/ASIHTTPRequest/ to download images in my iPad app and using the download directly to a file option and then deleting the file if it was a 404 error or any other status code than 200.
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:#"/Users/ben/Desktop/my_file.jpg"];
Problem is partial responses seem to be getting saved during slow connections so I end up with blank or corrupt images.
I decided instead to save the data stream to disk only after I received a status code of 200:
NSURL *url = [NSURL URLWithString: [[NSString stringWithFormat:kProductImagesURL, fileName] stringByAppendingString:tStamp]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTimeOutSeconds:10];
[request startSynchronous];
int statusCode = [request responseStatusCode];
if (statusCode==200) {
NSData *responseData = [request responseData];
[responseData writeToFile:[savePath stringByReplacingOccurrencesOfString:#"%20" withString:#" "] atomically:YES];
}
I just want to make sure that the response code only comes back after the request has been completed and all of the data has been downloaded. I am 99% sure that is the case but can't afford another app release with an image bug like this in it.
There are two reasons you should probably consider switching to an asynchronous request. The first is, it frees up your main thread to interact with the user (even a modal spinner would be nice--otherwise it looks like your app has frozen).
Second, it gives you callbacks that only happen once the whole request is finished. I can't really explain only having gotten partial data with the code you showed, but I've never once had that problem using ASI's asynchronous methods.

iPhone: How to download a raw .config file via program?

I am trying to download a xyz.config file which is created using iPhone Configuration utlity, via code.
NSString *urlString = #"http://xxxxx:xxx/ms/servlet/ConfigServer?userid=xxx&pwd=xxx&emailid=xxxxx";
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
Then, didReceiveData, didReceiveResponse are written. Finally,
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(#"Response : %#", responseString );
// release the connection, and the data object
[receivedData release];
[connection release];
}
With the above code, i'm getting response as string of the complete configuration file. But my Aim to download this file as raw data, which will automatically launch device profile to ask for installing this profile.
NOTE: I am able to provide the same URL in Safari browser on the device, and download the
raw file directly to install it on the device. I don't want to use Safari browser to download it, it should be done via my communication code.
I also tried with ASIHTTPRequest like below, but unable to download that file directly from the URL via this code as well.
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"GET"];
[request startSynchronous];
Please help!
Thank you.
The Mobile Safari browser app on the iOS has a lot more freedom than your app. Case in point, on the latest iOS 4.3, Mobile Safari has the Nitro JavaScript engine which boost 2x performance increase. But the UIWebView which is available to your app doesn't have that engine. Your app is in a sandbox, therefore, you cannot write outside of that box.

Google Toolbox For Mac : testing [NSURLConnection sendSynchronousRequest:...]

I'm wondering how to test a synchronous request to assert the behavior of an API client depending on the server response.
As it's a good practice to be independent of the server (so the test run fast and don't rely on the internet connection), I'd like to return my own response. I don't know how to do this, since the request is synchronous :
NSURL *url = [self URL];
NSData *postData = [self postData];
NSMutableURLRequest *downloadRequest = [NSMutableURLRequest requestWithURL:url];
[downloadRequest setHTTPMethod:#"POST"];
[downloadRequest setHTTPBody:postData];
[downloadRequest setTimeoutInterval:10.0];
return downloadRequest;
NSURLResponse *response;
NSError *error = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:downloadRequest
returningResponse:&response
error:&error];
Do you have any suggestion on how to do this ?
I don't know if it's possible to override NSURLConnection for example, or if I should change my code, just for testing purposes.
I don't completely understand the first sentence of the question. I can say that you almost certainly want to make it asynchronous. A synchronous request will block the UI and if it takes too long iOS will force-quit your app. Your asynchronous request callbacks can then review the success/failure and then either call a delegate with the same response or a modified one, or post a notification that you've previously installed handlers for, again swapping the response or not as you wish.