I have the following scenario: in my DocumentsDirectory there are user-created pdf-files. Now I want to upload them all to a webserver. When successfully done, i want to remove the file from the device.
My first problem is, that I do not know, when the upload is successful for a specific file. Additionally I get errors on some files.
At first some code:
-(void)uploadPDFs
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:docsDir error:nil];
for (NSString *filename in contents) {
if([filename.pathExtension isEqualToString:#"pdf"])
{
NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:filename]];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config setTimeoutIntervalForResource:10];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#/api/putPdf?name=%#&folder=", [defaults objectForKey:#"serverAdresse"], filename]];
NSData *pdfData = [NSData dataWithContentsOfFile:docsDirURL.path];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:#"application/pdf" forHTTPHeaderField:#"Content-Type"];
[request setHTTPMethod:#"POST"];
[request setValue:[NSString stringWithFormat:#"%lu", (unsigned long)[pdfData length]] forHTTPHeaderField:#"Content-Length"];
[request setTimeoutInterval:60];
// UploadTask is a NSURLSessionUploadTask
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:pdfData];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.uploadTask resume];
}
}
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});
if (!error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"NO ERROR");
// CAN I GET HERE THE UPLOADED FILENAME?
});
} else {
NSLog(#"ERROR: %#", error);
}
}
The error I get especially when there are many uploads:
(-[ViewController URLSession:task:didCompleteWithError:]) (ViewController.m:4025) ERROR: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7fa3957028f0 {NSErrorFailingURLKey=http://192.168.100.150/api/putPdf?name=2015-03-26_AN_14.pdf&folder=, NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=http://192.168.100.150/api/putPdf?name=2015-03-26_AN_14.pdf&folder=}
Use the taskDescription property of an NSURLSessionUploadTask. `
For example;
task.taskDescription = filePath;
`
The error was resulting out of an too short timeout. I increased this, and the problem disappeared.
Related
I was successful on getting a reply from the server with my post method however, I have problem with downloading the xml file data.
Here is my Post method (that I've searched in stackoverflow)
//We begin by creating our POST's body as an NSString, and converting it to NSData.
NSString *post = [NSString stringWithFormat:#"device_unique_key=%#&provision_type=c", deviceUniqueKey];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//Next up, we read the postData's length, so we can pass it along in the request.
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[postData length]];
//Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://192.168.1.166/autoprovision/sip_setup/downloadSipSetup"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
//And finally, we can send our request, and read the reply by creating a new NSURLSession:
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
xmlData = data; //This is the data that I try to donwload as xml file.
NSLog(#"requestReply: %#", requestReply);
}] resume];
I have xmlData = data //from the dataTaskRequest
Then here is my code for saving the xmlData to the Document's Directory
// Display the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// Perform the request on a new thread so we don't block the UI
dispatch_queue_t downloadQueue = dispatch_queue_create("Download queue", NULL);
dispatch_async(downloadQueue, ^{
NSError* err = nil;
NSHTTPURLResponse* rsp = nil;
// Perform the request synchronously on this thread
NSData *rspData = [NSURLConnection sendSynchronousRequest:request returningResponse:&rsp error:&err];
// Once a response is received, handle it on the main thread in case we do any UI updates
dispatch_async(dispatch_get_main_queue(), ^{
// Hide the network activity indicator
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (rspData == nil || (err != nil && [err code] != noErr)) {
// If there was a no data received, or an error...
} else {
// Cache the file in the cache directory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"init.xml"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[xmlData writeToFile:path atomically:YES];
// Do whatever else you want with the data...
[self loadDataFromXML];
}
});
});
The problem is that the xmlData is not working when I parse it using NSXMLParser. I tried to use an xml file and put it inside my project and it parses the xml but the xmlData that I downloaded doesn't get parsed (parse delegates are not called). I think my way of downloading the file is wrong. Can someone help me?
I found the problem in my project. My codes were correct after all! I didn't see that the code that I followed uses NSCachesDirectory and when I call the xmlData I used NSDocumentDirectory that's why it doesn't get parsed.
When i call getProfile method from viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self getProfile];
}
it success
-(void)getProfile
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSString *tempurl= [NSString stringWithFormat:#"%#11155/person/#self",baseUrlSecure];
tempurl = [tempurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:tempurl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request addValue:gWCToken forHTTPHeaderField:#"WCToken"];
[request addValue:gWCTrustedToken forHTTPHeaderField:#"WCTrustedToken"];
[request setHTTPMethod:#"GET"];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error)
{
NSLog(#"Error: %#", error);
dispatch_async(dispatch_get_main_queue(), ^{
[Alert showAlertWithTitle:#"M2All" andWithMessage:error.localizedDescription onView:self andErrorCode:[NSString stringWithFormat:#"%ld",error.code]];
});
}
else
{
NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *arrError = [dictResult objectForKey:#"errors"];
if(arrError.count >0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[Alert showAlertWithTitle:#"M2All" andWithMessage:[[arrError objectAtIndex:0] objectForKey:#"errorMessage"] onView:self andErrorCode:[[arrError objectAtIndex:0] objectForKey:#"errorCode"]];
});
return;
}
}
}];
[postDataTask resume];
}
but same calling from webViewDidFinishLoad then getting error
i don't know what is the problem
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self getProfile];
}
Clear the browser cookies and try again. I faced the similar problem. I cleared the browser cache and it's working fine for me
I have a file URL and want to download it in my iPhone in document directory. So that I can use this downloaded file within other application like, share it on whatsApp, in email attachment etc.
I am using ASIHTTPRequest, what Destination path I should set?
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.ImageURL];
[request setDownloadDestinationPath:#"What Path I should Set"];
[request startSynchronous];
Here is a sample url of file which I want to download:
http://www.fourspan.com/apps/agentreview/public/img/ads/1441791507_Muhammad.jpg
Firstly, ASIHTTPRequest is obsolete, please avoid using it.
Here is the code using AFNetworking to download any file and save it to any location you want (savePath)
#define kDocumentsDirPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
- (void)downloadFile:(NSString *)filePath
{
AFURLSessionManager *sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:filePath]];
NSURLSessionDownloadTask *downloadTask = [sessionManager downloadTaskWithRequest:request
progress:nil
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSString *savePath = [kDocumentsDirPath stringByAppendingPathComponent:fileName];
return [NSURL fileURLWithPath:savePath];
}
completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error)
{
NSLog(#"Download failed for %#: %#", fileName, error.localizedDescription);
[[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"Failed to download %#", fileName]
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"Uh Oh"
otherButtonTitles:nil] show];
}
else {
NSLog(#"File downloaded: %#", fileName);
}
}];
[downloadTask resume];
}
im using this code to post data to my server
NSURL *mainurl = [NSURL URLWithString:#"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];
NSString * postdata = [[NSString alloc]initWithFormat:#"UniqueId=%#&jsonProduct=%#&BranchId=%#&OrderToTime=%#",GETUnicidentifire,JsonOrderDetail,BranchId,OrderToTime];
ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];
[requestt setRequestMethod:#"POST"];
[requestt addRequestHeader:#"application/x-www-form-urlencoded" value:#"Content-Type"];
[requestt appendPostData:[postdata dataUsingEncoding:NSUTF8StringEncoding]];
NSString * theurl = [NSString stringWithFormat:#"%#",mainurl];
NSURLRequest *thereqest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:theurl]];
[NSURLConnection connectionWithRequest:thereqest delegate:self];
in the method
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"%#",error);
}
im geting:
{Message:The requested resource does not support http method 'GET'.}
what im doing Wrong ?
You are mixing things up here. You build an ASIFormDataRequest, but you don't actually send it. What you do send is an NSURLConnection.
It's been a long time since I've used ASI, but this might help:
NSURL *mainurl = [NSURL URLWithString:#"http://xxxxxxxxxx/api/PhonePaymentApi/Transaction/"];
ASIFormDataRequest *requestt = [ASIFormDataRequest requestWithURL:mainurl];
[requestt addPostValue:GETUnicidentifire forKey:#"UniqueId";
[requestt addPostValue:JsonOrderDetail forKey:#"jsonProduct";
[requestt addPostValue:BranchId forKey:#"BranchId";
[requestt addPostValue:OrderToTime forKey:#"OrderToTime";
[requestt setCompletionBlock:^{
// Process the response
}];
[requestt setFailedBlock:^{
NSError *error = [requestt error];
NSLog(#"%#",error);
}];
[requestt startAsynchronous];
As a word of advice, replace ASI with something like AFNetworking. ASI is no longer being developed.
I have this url NSURL *url = [NSURL URLWithString:#"http://site.com/ArcGIS/rest/services/POI_Data/MapServer/0/query?&where=ToegangsNr+%3D+5002&returnGeometry=true&outSR=4326&f=json"];
But i want to change the 5002 value in the string with the NSUserdefault _phonenumber.
How can i do this?
The other code of this function is:
ASIHTTPRequest *_request = [ASIHTTPRequest requestWithURL:url];
__weak ASIHTTPRequest *request = _request;
request.requestMethod = #"POST";
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];
// 5
[request setDelegate:self];
[request setCompletionBlock:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSString *responseString = [request responseString];
NSLog(#"Response: %#", responseString);
[self plotCrimePositions:request.responseData];
}];
[request setFailedBlock:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
NSURL *url = [NSURL URLWithString:#"http://site.com/ArcGIS/rest/services/POI_Data/MapServer/0/query?&where=ToegangsNr+%3D+5002&returnGeometry=true&outSR=4326&f=json"];
NSString *urlString = [url absoluteString];
urlString = [urlString stringByReplacingOccurrencesOfString:#"5002" withString:[userDefaults objectForKey:#"_phoneNumber"]];
NSLog(#"%#",urlString);
url = [NSURL URLWithString:urlString];
NSLog(#"%#",url);
Hope it help.
Try this:
NSString *urlStr = [NSString stringWithFormat:#"http://site.com/ArcGIS/rest/services/POI_Data/MapServer/0/query?&where=ToegangsNr+%%3D+%#&returnGeometry=true&outSR=4326&f=json", [[NSUserDefaults standardUserDefaults] objectForKey:#"_phonenumber"]];
NSURL* url = [NSURL URLWithString:urlStr];