I'm trying to migrate our ASIHTTPRequest code to AFNetworking. I'm okay with POST requests but I'm having issues with download requests. I can't seem to set the temporary file path of the content to be downloaded. In ASIHTTPRequest I can have a code like this:
// Create file path
NSString *filePath = [cachePath stringByAppendingPathComponent:package.fileName];
NSString *tempFile = [tempPath stringByAppendingPathComponent:package.fileName];
[downloadRequest setTemporaryFileDownloadPath:tempFile];
[downloadRequest setDownloadDestinationPath:filePath];
How can I do this using AFNetworking?
AFURLSessionManager* urlSessionManager = [AFURLSessionManager.alloc initWithSessionConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
NSProgress* progress = nil;
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://your.remote.file"]];
NSURLSessionDownloadTask* downloadTask = [urlSessionManager downloadTaskWithRequest:request progress:&progress destination:^NSURL* (NSURL* targetPath, NSURLResponse* response) {
NSURL* targetFileUrl = [NSURL fileURLWithPath:#"/your/local/path" isDirectory:NO];
return targetFileUrl;
} completionHandler:^(NSURLResponse* response, NSURL* filePath, NSError* error) {
if (error)
{
// Some error occurred or download programmatically cancelled
}
else
{
// Download completed
}
}];
[downloadTask resume];
The temporary files are managed by AFNetworking, usually they're hidden raw files inside your document dir.
Related
I'm using AFNetworking 3.0 to download a file, and it seems to be doing the downloading part fine, but I can't find the file afterwards.
I'm using the code below. In the download task, if I set breakpoints in the destination block, it seems as though the target path and download destination path are correct, and in fact at the point the targetPath points to a tmp file in the tmp folder which exists and contains the correctly downloaded data. However if I then hit a breakpoint in the completion handler block, the tmp file has disappeared and there is no file where my download destination path pointed.
Am I missing a step? Do I have to move this file myself, or is that something AFNetworking should be taking care of?
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
self.theRequest = [[AFHTTPRequestSerializer serializer]
requestWithMethod:self.RequestMethod //#"POST"
URLString:requestURL.absoluteString //url to my API
parameters:self.Parameters //params being sent to API
error:nil];
//headers in this example:
//"Content-Type" = "application/json"
//"X-Requested-With" = XMLHttpRequest
//token = "<API TOKEN>";
for (id key in headers) {
[self.theRequest setValue:headers[key] forHTTPHeaderField:key];
}
self.theRequest.timeoutInterval = 60 * 100;
NSURLSessionDownloadTask * downloadTask =
[manager downloadTaskWithRequest:self.theRequest
progress:^(NSProgress * _Nonnull downloadProgress) {
if(self.DownloadProgressHandler)
self.DownloadProgressHandler(downloadProgress.fractionCompleted);
}
destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSURL *url = [NSURL URLWithString:self.downloadDestinationPath];
NSLog(#"%#",[targetPath absoluteString]);
NSLog(#"%#",[url absoluteString]);
return url;
}
completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
[self RequestCompleteWithResponse:response responseObject:[[filePath absoluteString] dataUsingEncoding:NSUTF8StringEncoding] error:error];
}];
self.theTask = downloadTask;
[self.theTask resume];
Output from the NSLogs above:
2016-03-04 13:43:44.412 Marq[27505:154492] __23-[MarqAPI BuildRequest]_block_invoke247 line 648 $ file:///Users/aerion/Library/Developer/CoreSimulator/Devices/11594D0A-882C-4E46-9BAC-CEF7148014C7/data/Containers/Data/Application/E8C7D3EE-BB69-461F-BA2F-49EB7C2AE1CF/tmp/CFNetworkDownload_7VGArX.tmp
2016-03-04 13:43:44.425 Marq[27505:154492] __23-[MarqAPI BuildRequest]_block_invoke247 line 649 $ /Users/aerion/Library/Developer/CoreSimulator/Devices/11594D0A-882C-4E46-9BAC-CEF7148014C7/data/Containers/Data/Application/E8C7D3EE-BB69-461F-BA2F-49EB7C2AE1CF/Documents/9dfd86c2-458e-4725-a184-5fcd87f94dbd.inspect
Argh, that was silly of me. The answer is staring me in the face in those logs.
The file path for the temp file begins with file://, whereas my download destination path does not. the answer is to change
NSURL *url = [NSURL URLWithString:self.downloadDestinationPath];
to
NSURL *url = [NSURL fileURLWithPath:self.downloadDestinationPath];
This will give me a valid file path to send the downloaded file to
I was wondering if someone could help me out. I'm trying to use NSURLSessionDownloadTask to display a picture in my UIImageView if I put the image URL into my textfield.
-(IBAction)go:(id)sender {
NSString* str=_urlTxt.text;
NSURL* URL = [NSURL URLWithString:str];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
NSURLSession* session = [NSURLSession sharedSession];
NSURLSessionDownloadTask* downloadTask = [session downloadTaskWithRequest:request];
}
I am not sure where to go after this.
Two options:
Use [NSURLSession sharedSession], with rendition of downloadTaskWithRequest with the completionHandler. For example:
typeof(self) __weak weakSelf = self; // don't have the download retain this view controller
NSURLSessionTask* downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
// if error, handle it and quit
if (error) {
NSLog(#"downloadTaskWithRequest failed: %#", error);
return;
}
// if ok, move file
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsURL = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
NSURL *fileURL = [documentsURL URLByAppendingPathComponent:filename];
NSError *moveError;
if (![fileManager moveItemAtURL:location toURL:fileURL error:&moveError]) {
NSLog(#"moveItemAtURL failed: %#", moveError);
return;
}
// create image and show it im image view (on main queue)
UIImage *image = [UIImage imageWithContentsOfFile:[fileURL path]];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.imageView.image = image;
});
}
}];
[downloadTask resume];
Clearly, do whatever you want with the downloaded file (put it somewhere else if you want), but this might be the basic pattern
Create NSURLSession using session:delegate:queue: and specify your delegate, in which you'll conform to NSURLSessionDownloadDelegate and handle the completion of the download there.
The former is easier, but the latter is richer (e.g. useful if you need special delegate methods, such as authentication, detecting redirects, etc., or if you want to use background session).
By the way, don't forget to [downloadTask resume], or else the download will not start.
I'm practicing how to parse XML data and store it into the sqlite. I have done parsing and storing successfully. But I have one problem with displaying image from an url. The URL is formed by combining two NSStrings. One for the fixed address(which I set value) and other based on the name of the photo(retrieved from Sqlite). I am able to create complete URL by retrieving name of photo from Sqlite. But strange thing happen when I use that URL to display image in UIImageView. It does not work. After some testing, I found out there is something wrong with the portion(photo name) I retrieved from Sqlite. If I combine the fixed address with literal name of the photo, it works. Can somebody explain why it happens like this. Thanks in advance.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"user.db"];
FMDatabase * database = [FMDatabase databaseWithPath:defaultDBPath];
[database open];
results=[database executeQuery:#"SELECT * FROM plant WHERE name=?",self.plantname];
while([results next]) {
name = [results stringForColumn:#"name"];
category = [results stringForColumn:#"category"];
instructions = [results stringForColumn:#"instructions"];
price = [results stringForColumn:#"price"];
photo=[results stringForColumn:#"photo"];
}
NSString * fixedURL = #"http://services.hanselandpetal.com/photos/";
NSString *url=[fixedURL stringByAppendingString:photo];
NSLog(url);
NSURL * imgURL = [NSURL URLWithString:url];
NSData * imageData = [NSData dataWithContentsOfURL:imgURL];
UIImage * image = [UIImage imageWithData:imageData];
self.plantImageView.image=image;
Edit
I've edited based on Rob answer. But there are still problems. I put log below. I've already tried to remove white spaces by using [photo stringByReplacingOccurrencesOfString:#" " withString:#""]. But it does not seem to affect.
2014-10-15 02:12:39.505 SqlitePractice[9256:286525] url string = 'http:/services.hanselandpetal.com/photos/mona_lavender.jpg'
2014-10-15 02:12:39.506 SqlitePractice[9256:286525] imgURL = (null)
2014-10-15 02:12:39.516 SqlitePractice[9256:286525] sendAsynchronousRequest failed: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x7a78ba50 {NSUnderlyingError=0x7a66b5e0 "unsupported URL", NSLocalizedDescription=unsupported URL}
Edit 2
The URL seems to be problem after changing from stringByAppendingString to stringByAppendingPathComponent. But I found out that even without one slash after http: , it works if I change the photo value. And the image is displayed.
NSString * fixedURL = #"http://services.hanselandpetal.com/photos/";
photo=#"bougainvillea.jpg";
NSString *url=[fixedURL stringByAppendingPathComponent:photo];
NSLog(#"url string = '%#'", url);
NSURL * imgURL = [NSURL URLWithString:url];
NSLog(#"imgURL = %#", imgURL);
And the log is below,
2014-10-15 12:07:56.650 SqlitePractice[9690:308022] url string = 'http:/services.hanselandpetal.com/photos/bougainvillea.jpg'
2014-10-15 12:07:56.651 SqlitePractice[9690:308022] imgURL = http:/services.hanselandpetal.com/photos/bougainvillea.jpg
Edit 3
I have changed based on Rob's modified answer.
NSURL *fixedURL = [NSURL URLWithString:#"http://services.hanselandpetal.com/photos/"];
NSURL *url = [fixedURL URLByAppendingPathComponent:[photo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"image URL = '%#'", url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(#"sendAsynchronousRequest failed: %#", error);
return;
}
UIImage *image = [UIImage imageWithData:data];
self.plantImageView.image=image;
}];
And here is the log message:
2014-10-15 13:40:38.977 SqlitePractice[10436:357359] image URL = 'http://services.hanselandpetal.com/photos/camellia.jpg%250A%2520%2520%2520%2520'
A couple of thoughts:
Unrelated to your problem at hand, instead of stringByAppendingString, it's generally better to use URLByAppendingPathComponent with a NSURL. It takes care of adding any necessary / characters for you. Not critical given your example, but it's more robust.
Also, if the string might have spaces or other characters not permitted in a URL, you might percent escape it, too.
Thus, you'd do something like:
NSURL *fixedURL = [NSURL URLWithString:#"http://services.hanselandpetal.com/photos/"];
NSURL *url = [fixedURL URLByAppendingPathComponent:[photo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
You should NSLog the NSURL variable and make sure it's not nil (e.g. which could be caused if there were some spaces in the photo string, for example, unless you percent escape it).
In your revised question, in which you share the log, the original URL string (before the conversion to the NSURL) is being reported as:
http:/services.hanselandpetal.com/photos/mona_lavender.jpg
That should be:
http://services.hanselandpetal.com/photos/mona_lavender.jpg
If imgURL is not nil, but imageData is, you might want to retrieve the image data in a manner that retrieves the error. For example, you could:
NSError *error = nil;
NSData *imageData = [NSData dataWithContentsOfURL:imgURL options:0 error:&error];
if (!imageData) {
NSLog(#"dataWithContentsOfURL failed: %#", error);
}
Bottom line, go through this one line at a time and identify precisely where it is going wrong.
As an aside, you generally would want to avoid using dataWithContentsOfURL, but instead use an asynchronous method:
NSURL *fixedURL = [NSURL URLWithString:#"http://services.hanselandpetal.com/photos/"];
NSURL *url = [fixedURL URLByAppendingPathComponent:[photo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NRURLRequest *request = [NSURLRequest requestWithURL:imgURL];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(#"sendAsynchronousRequest failed: %#", error);
return;
}
UIImage *image = [UIImage imageWithData:data];
self.plantImageView.image=image;
}];
I belive that UIIMAGEVIEW will not display Url as image path. I would sugest to try with UIWebView which would display web content (image or whole web page). This example is writen in objective-c.
self.myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
self.myWebView.scalesPageToFit = YES ;
[self.view addSubview:self.myWebView];
NSURL *url = [NSURL URLWithString:#"http://services.hanselandpetal.com/photos/mona_lavender.jpg"];
NSURLRequest *request = [ NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request]
I'm trying to upload a file from a sharing extension in the Photos app, using a background NSURLSession. Because a background NSURLSession only supports an upload task using the uploadTaskWithRequest:WithFile: API, I first get the URL for the image URL that was retrieved from the extension, write the image content to the shared container, then upload the new file. It seems like NSURLSession is having permission issues, I am getting this error:
"Failed to issue sandbox extension for file file:///private/var/mobile/Containers/Shared/AppGroup/..."
I know there are a few similar posts to this but none of them are loading an url from an extension and does not show where to write the temporary file to.
Here's the code:
- (void)fetchImageURLInExtensionContext:(NSExtensionContext*) context onComplete:(void (^)()) completion
{
NSExtensionItem *item = self.extensionContext.inputItems[0];
NSItemProvider *provider = item.attachments[0];
if ([provider hasItemConformingToTypeIdentifier:#"public.jpeg"]) {
[provider loadItemForTypeIdentifier:#"public.jpeg" options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
NSObject *obj = item;
if ([obj isKindOfClass:[NSURL class]]) {
self.imageURL = obj;
completion();
}
}];
}
}
- (void)postImage
{
// copy file to shared container
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:#"group.com.mytestgroup"];
NSString *writeToPath = [[containerURL path] stringByAppendingPathComponent:#"temp.jpg"];
BOOL success = [[NSData dataWithContentsOfURL:self.imageURL] writeToFile:writeToPath atomically:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"https://api.imgur.com/3/image"]];
NSString *boundary = #"multipartboundary";
[request addValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary] forHTTPHeaderField:#"Content-Type"];
request.HTTPMethod = #"POST";
[request setValue:#"Client-ID my_imgur_client_id" forHTTPHeaderField:#"Authorization"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:#"blah"];
config.sharedContainerIdentifier = #"group.com.mytestgroup";
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURLSessionTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:writeToPath]];
[uploadTask resume];
}
workaround solution: move the file from inbox to temp directory and upload from there.
I am doing a NSURLConnection sendAsynchronousRequest. I give it the correct path to the new content but it always download older content.
aka I have two paths:
path1
path2
I create a requestString from path1.
Then I create the request:
NSURLRequest *requestDownload = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURLString]];
after that I do:
[NSURLConnection sendAsynchronousRequest:requestDownload queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *downloadResponse, NSData *downloadData, NSError *dError) {
if(error)
{
NSLog(#"Error: %#", error);
[self stopIndicator];
}
else{
NSLog(#"Successfully downloaded file to %#", pdfPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) {
[[NSFileManager defaultManager] removeItemAtPath:pdfPath error:NULL];
}
[downloadData writeToFile:pdfPath atomically:YES];
// Set the file modification date to the timestamp from the server
}
I breakpoint and look at the downloadResponse's URL and it is the accurate URL from 'path1', I check the server where the file is being downloaded from and the file is accurate there. But once the file is written to the Documents folder in the simulator, the file is from the 'path2' not from 'path1' where the async request was supposed to get its downloadData from...
TOTALLY confused here. Funny thing it was working an hour ago then this behavior started cropping up.
Thanks for any help.