Run the ASIHTTPRequest in background when View reloads - ios

I am developing a application which uses ASIHTTPRequest to download the content from server. When downloading if I move from Portrait to Landscape mode the downloads process is going to stop. I need the download process still to be continued.
I have searched for the solution in stackoverflow but no useful.
How can I solve this problem...
This is the piece of code i am using to download.
NSURL *downloadURL = [publisher contentURLForIssueWithName:issueTitle];
if (!networkQueue) {
networkQueue = [[ASINetworkQueue alloc] init];
}
if(!downloadURL) return;
[self performSelectorInBackground:#selector(read) withObject:nil];
request = [ASIHTTPRequest requestWithURL:downloadURL];
[request setDelegate:self];
[request setDownloadProgressDelegate:progressView];
[request setShowAccurateProgress:YES];
request.shouldContinueWhenAppEntersBackground=YES;
request.allowResumeForFileDownloads=YES;
[request startAsynchronous];
Any help to be appreciated..

Related

DownLoad Data from server with "Pause" and "Resume" functionality

I want to download a file with pause/resume functionality. I read apple documents, there I got NSUrldownload which supports the same but it is not available for iOS. I was trying with NSUrlconnection, but not working. I don't want to use any third party libraries, I want to implement it by myself, below is the code snippet which I tried.
NSString *fileName = [NSString stringWithFormat:#"~%#",[[url componentsSeparatedByString:#"/"] lastObject]];
int dataLength = [[self checkDocDirectoryforFileName:fileName] length];
//dataLength = 0;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setValue:#"audio/mpeg" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"bytes" forHTTPHeaderField:#"Accept-Ranges"];
[request setValue:#"Keep-Alive" forHTTPHeaderField:#"Connection"];
[request setValue:[NSString stringWithFormat:#"%d",dataLength] forHTTPHeaderField:#"Content-Length"];
NSLog(#"Request header %#", [request allHTTPHeaderFields]);
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
Please check it out this:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html#//apple_ref/doc/uid/20001839-SW2
Hope, May it will help you,
:)
iOS 7.0 and above
NSURLSession especially NSURLSessionDownloadTask provides this functionality.
The NSURLSession API provides status and progress properties, in
addition to delivering this information to delegates. It supports
canceling, restarting or resuming, and suspending tasks, and it
provides the ability to resume suspended, canceled, or failed
downloads where they left off.
Take a look to the docs.
iOS 5.0 and above
I would use AFDownloadRequestOperation for this. Take a look at this thread.
AFDownloadRequestOperation has
additional support to resume a partial download, uses a temporary
directory and has a special block that helps with calculating the
correct download progress.

What does ASIFileManagementError mean?

I am using ASI to download files and I keep seeing ASIFileManagementError. I am guessing that means that there is something wrong with how I'm building the download paths for my requests but it's not consistent. Sometimes the download works just fine and other times it fails. Even when using the same code on the same device to download the same file! Here is my code.
-(ASIHTTPRequest*)buildDownloadLinkForUpdate:(ContentItem*)update
{
NSString *URLString = [[NSString alloc] initWithFormat:#"%#?%#=%#", update.downloadUrl.absoluteString, #"auth_token", database.userAuthToken];
NSURL *url = [NSURL URLWithString:URLString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"GET"];
ContentItem* item = [database getItemWithId:update.uniqueId];
[request setDownloadDestinationPath:item.contentPath];
[request setTemporaryFileDownloadPath:[[AppSettings instance].temporaryPath stringByAppendingPathComponent:item.fileName]];
[request setAllowResumeForFileDownloads:TRUE];
[request setShowAccurateProgress: TRUE];
[request setDelegate:[_currentItem progressDelegate]];
return request;
}
-(void)initalizeNetworkQueue
{
if(!networkQueue) networkQueue = [[ASINetworkQueue alloc] init];
[networkQueue cancelAllOperations];
[networkQueue reset];
if([_currentItem progressDelegate])
{
[networkQueue setDownloadProgressDelegate:[_currentItem progressDelegate]];
}
[networkQueue setDelegate:self];
[networkQueue setRequestDidFinishSelector:#selector(networkQueueComplete:)];
[networkQueue setRequestDidFailSelector:#selector(networkQueueFailed:)];
[networkQueue setShowAccurateProgress:YES];
networkQueue.maxConcurrentOperationCount = 1;
}
And then here is the code that actually invokes this:
[self initalizeNetworkQueue];
[networkQueue addOperation:[self buildDownloadLinkForUpdate:_currentUpdate]];
[networkQueue go];
currentState = ContentUpdaterStateDownloading;
Is there something obviously wrong here?
-= UPDATE=-
It's saying that it's failing because it's unable to move the file from the temporary location to the final location.
Download Failed: Failed to move file from '/var/mobile/Applications/33E1DF3C-17F5-432F-8204-A9B53AB5AAE3/Documents/.temp/FileShare+Gate+4.pptx' to '/var/mobile/Applications/33E1DF3C-17F5-432F-8204-A9B53AB5AAE3/Documents/.content/FileShare+Gate+4.pptx'
I had this error before, it was in the connection, it is of type ASINetworkErrorType, it means that the host cannot be reached.
Anyway ASIHttprequest is dead now, you will not find answers for many questions you have, take my advice and go with AFNetworking, it is great and do the job.

XCode: Stop Animating Activity Indicator after Executing a Method From Another File

Ok, this seems like it should be very simple - All I want to do is call my ServerConnect.m (NSObject), NSURL Connection Request Method, from my SignIn.m (ViewController) and stop the UIActivityIndicatorView after the NSURL Request has completed. Of course, if I do it all on the main thread:
- (IBAction)forgotPassword:(id)sender {
[activityIndicator startAnimating];
connection = [[ServerConnect alloc] init];
[connection sendUserPassword:email withSecurity:securityID];
[activityIndicator stopAnimating];
}
Then, everything will then execute concurrently, and the activity indicator will start and stop before the connection method finishes...
Thus, I attempted to place the connection request on a secondary thread:
- (IBAction)forgotPassword:(id)sender {
[NSThread detachNewThreadSelector: #selector(requestNewPassword:) toTarget:self withObject:userEmail.text];
}
- (void) requestNewPassword:(NSString *)email
{
[self->thinkingIndicator performSelectorOnMainThread:#selector(startAnimating) withObject:nil waitUntilDone:NO];
//Make NSURL Connection to server on secondary thread
NSString *securityID = [[NSString alloc] init];
securityID = #"security";
connection = [[ServerConnect alloc] init];
[connection sendUserPassword:email withSecurity:securityID];
[self->thinkingIndicator performSelectorOnMainThread:#selector(stopAnimating) withObject:nil waitUntilDone:NO];
}
But, I don't see the activity indicators here either, which may be due the NSURL Request not functioning properly on the secondary thread (i.e. for some reason, it does not gather an xml string as it does when requested on the main thread).
What is the proper way to architecture my code to make this work? I am surprised at how much work has been involved in trying to figure out how to get my activity indicator to simply stop after a method from another file has finished executing. Is there a way to run the code in series (one after another) and not concurrently? Any help would be appreciated.
Updated to Show: sendUserPassword:(NSString *)withSecurity:(NSString *)
- (void)sendUserPassword:(NSString *)emailString
withSecurity:(NSString *)passCode;
{
NSLog(#"Making request for user's password");
newUser = NO;
fbUser = NO;
forgotPassword = YES;
NSString *post = [NSString stringWithFormat: #"email=%#&s=%#", emailString, passCode];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
//Construct the web service URL
NSURL *url = [NSURL URLWithString:#"http://www.someurl.php"];
//Create a request object with that URL
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:90];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
//Clear out the existing connection if there is one
if(connectionInProgress) {
[connectionInProgress cancel];
}
//Instantiate the object to hold all incoming data
xmlData = [[NSMutableData alloc] init];
//Create and initiate the conection - non-blocking
connectionInProgress = [[NSURLConnection alloc] initWithRequest: request
delegate:self
startImmediately:YES];
}
One suggestion try like this:
- (IBAction)forgotPassword:(id)sender
{
[self->thinkingIndicator startAnimating];
[NSThread detachNewThreadSelector: #selector(requestNewPassword:) toTarget:self withObject:userEmail.text];
}
- (void) requestNewPassword:(NSString *)email
{
//Make NSURL Connection to server on secondary thread
NSString *securityID = [[NSString alloc] init];
securityID = #"security";
connection = [[ServerConnect alloc] init];
[connection sendUserPassword:email withSecurity:securityID];
[self->thinkingIndicator performSelectorOnMainThread:#selector(stopAnimating) withObject:nil waitUntilDone:NO];
}
I ended up incorporating the NSNotification system (see Multithreading for iOS) to solve my problem. Any reason why this would be frowned upon:
"One easy way to send updates from one part of your code to another is Apple’s built-in NSNotification system.
It’s quite simple. You get the NSNotificationCenter singleton (via [NSNotificationCenter defaultCenter]) and:
1.) If you have an update you want to send, you call postNotificationName. You just give it a unique string you make up (such as “com.razeware.imagegrabber.imageupdated”) and an object (such as the ImageInfo that just finished downloading its image).
2.) If you want to find out when this update happens, you call addObserver:selector:name:object. In our case the ImageListViewController will want to know when this happens so it can reload the appropriate table view cell. A good spot to put this is in viewDidLoad.
3.) Don’t forget to call removeObserver:name:object when the view gets unloaded. Otherwise, the notification system might try to call a method on an unloaded view (or worse an unallocated object), which would be a bad thing!"
You could try something this, it uses a block when it is finished. I had similar thing right here.
// Turn indicator on
// Setup the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setTimeoutInterval: 90.0];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// Its has finished but sort out the result (test for data and HTTP 200 i.e. not 404)
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (data != nil && error == nil && [httpResponse statusCode] == 200)
{
// Connection finished a gooden
// Do whatever you like with data
// Stop indicator
}
else
{
// There was an error, alert the user
// Do whatever you like with data
// Stop indicator
}
}];

Upload big video from PhotoLibrary to server

I have a problem with uploading big video asset to a server from PhotoLibrary.
I get my asset data as described here, export the video to local document, and then upload.
But when I upload a big video (2 minutes and about 300Mb or more in size), this method causes a crash and I got no reason nor any any information.
I use webDAV to upload files just like this:
// Set up credentials
NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:username
password:password
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:host
port:80
protocol:#"http"
realm:#" webDAV"
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials forProtectionSpace:space];
[space release];
// Create the request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"PUT"];
[request setValue:[self mimetypeForFile:self.filepath] forHTTPHeaderField:#"Content-Type"];
NSNumber *contentLength = (NSNumber *) [[[NSFileManager defaultManager]
attributesOfItemAtPath:self.filepath error:NULL]
objectForKey:NSFileSize];
[request setValue:[contentLength description] forHTTPHeaderField:#"Content-Length"];
if (self.useStreaming)
{
if (self.currentFileStream!=nil)
{
[self.currentFileStream close], self.currentFileStream = nil;
}
self.currentFileStream = [NSInputStream inputStreamWithFileAtPath:self.filepath];
if (currentFileStream!=nil)
{
[request setHTTPBodyStream:currentFileStream];
}
else
{
[request setHTTPBody:[NSData dataWithContentsOfFile:self.filepath]];
}
}
else
{
[request setHTTPBody:[NSData dataWithContentsOfFile:self.filepath]];
}
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (self.currentConnection!=nil)
{
self.currentConnection = nil;
}
self.currentConnection = conn;
[conn release];
[self.currentConnection start];
When the code reache this line:
self.currentFileStream = [NSInputStream inputStreamWithFileAtPath:self.filepath];
[request setHTTPBodyStream:currentFileStream];
OR:
[request setHTTPBody:[NSData dataWithContentsOfFile:self.filepath]];
It crashed.
Do you have any suggestion?
Thanks.
========================
Edit: It crash at setHTTPBody: OR setHTTPBodyStream:
So I think it's about memory leak or something.
================
EDIT2: Now I decide to compress video, I get video data by current method is too large(more than 300mb), But I find use UIImagePickerController select the same video, it just 30mb; So compress is help; I'll try UIVideoEditorController, and will post my result soon;
Completed. the solution is try to compress big video to small file;
1.just like before, export video asset to tmp directory;
2.use UIVideoEditorController to compress the video file;
3.just upload compressed file like the code I post.
That's all.
check following post , where i am uploading image on server, instead of image you can post your video Uploading Image via POST in Objective C

How to upload images in a scrollview onto a web server?

I have an iOS app that has a series of images in a scrollview (camera pictures). I want to be able to post these on to a url individually or collectively as a group.
Can someone point me to an example or some sample code that would get me started?
Much appreciated
in that case if you if you set up your server (how do this i don`t now, because i used quickblox - ready-made solution) try send GET-request to the server. You can try this like this:
(source - How to send a Get request in iOS? )
NSString *getString = [NSString stringWithFormat:#"parameter=%#",yourvalue];
NSData *getData = [getString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getLength = [NSString stringWithFormat:#"%d", [getData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"https:yoururl"]];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:getData];
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
NSAssert(self.urlConnection != nil, #"Failure to create URL connection.");
// show in the status bar that network activity is starting
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
For more advanced preferences I would advise you try learn http://allseeing-i.com/ASIHTTPRequest/
For this you can create file server or use other maded. Look this: http://quickblox.com/ - they has blobs and ios api for their server, so download|upload files simple implemented in code.
First, you need register your account and then register your app.( https://admin.quickblox.com/signin ) (it's not difficult, few minuts)
Then, using instructions from site added Quickblox auth to your app and then use QBBlobs for dowload photo. Good luck!

Resources