iOS 10 get file size before download - ios

I need to get file size from url to decide whether i will download it or not. I tried it with NSMutableURLRequest but in the delegate, it returns -1 always. And the NSMutableURLRequest method is deprecated now. I added all the delegate (NSURLConnectionDelegate,NSURLConnectionDownloadDelegate,NSURLConnectionDataDelegate) in .h file but it does not work. My code is :
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://download.quranicaudio.com/quran/abdullaah_3awwaad_al-juhaynee/001.mp3"]];
[req setHTTPMethod:#"Head"];
NSURLConnection * con = [[NSURLConnection alloc] initWithRequest:req
delegate:self];
[con start];
[con release];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(#"didReceiveResponse (%lld) ", response.expectedContentLength);
}
Already tried with : How to find size of a file before downloading it in iOS 7?
Any solution for iOS 10+ ?

If you run the code after commenting [con release] it gives the exact size of your file.

Don't use NSURLConnection, instead use NSURLSession along with the NSURLSessionDownloadTask in your case. You can get the downloading file length by using property 'countOfBytesExpectedToReceive' of NSURLSessionTask which is the super class of NSURLSessionDownloadTask. There are various beautiful libraries available for networking. You can use AFNetworking for example. Or you can write your own code. Here are some links may be useful to you:
http://sweettutos.com/2014/09/16/how-you-would-use-nsurlsession-to-download-files/
https://www.infragistics.com/community/blogs/stevez/archive/2014/05/21/ios-objective-c-downloading-data-using-nsurlsession.aspx
PS: You can handle your requirements at a more granular level by using NSURLSessionDelegate methods for download task.

Related

How to send image to server with url in ios

I need to send a image to server with Url the url looks as below
http://somewebsite.com:8080/app/api/fappname/newUser/1/Delhi481/cct/12 dk/11588/kmkr/72579/Los/TM/8:00/13:00/28.6100/77.2300/foto1.jpg/null
they asked me to send image in place of foto1.jpg, the code that now i'm using is as below i am sending null inplace of image i'm getting response. can any one send me how to send the image in this url.
NSString *str = [NSString stringWithFormat:#"http://someurl.com:8080/app/api/kkknnmm/newUser/1/%#/%#/%#/%#/%#/%#/%#/TM/%#/%#/%#/%#/%#/%#/%#",[inSchoolName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],#"kktt",[inAddressStreet stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],inAddressNo,inAddressState,inZipCode,[inNeighbourhood stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[arrivalHR stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[endingHr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],inlat,inlong,inImg1,inImg2,inImg3];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
NSLog(#"Login Url:%#",str);
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
can any one please help me.
Well your Api Is expecting binary data, I would suggest you to go through these links.
I hope they Will Help you.
Have A look at this And this

Box.Net iOS SDK Moving Files and Folders

I am currently developing an iOS application and am implementing the Box.Net SDK. I have gotten everything to work except the ability to move files around, which is not a native feature of the SDK. I am trying to twist my way through it.
If you are familiar with the structure of Box.Net, each file/folder has an ID number for itself, and an ID its parent. From what I understand if I want to move a file, I am supposed to change the parent ID number on the file which will point it to the new location. I can't seem to get it to work properly though. My application seems to keep crashing.
This is what I have so far, generalized.
BoxObject *boxObject = [[[Box objectWithID:(ID#ofParent)] children] objectAtIndex:i];
[boxObject parent].boxID = 0; // <-- Problem (causes crash)
I also tried this.
[boxObject setParent:[Box folderWithID:[BoxID numberWithInt:i]]];
The boxObject variable is the file that I want to move. I am setting its parent ID equal to 0, which is supposed to be the root folder. However, my application crashes when I try to reassign the parent ID for the file. Any ideas on how to successfully move files/folders? Any help is much appreciated! Thanks in advance!
Okay. I suppose there wasn't a way to do fix this in-house with the SDK. So, I had to send out an external PUT request. I used the following code to handle moving files.
- (void)moveItem:(BoxObject *)object toParentFolderWithID:(BoxID *)parentID
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.box.com/2.0/files/%#", object.boxID]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"PUT"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *auth = [NSString stringWithFormat:#"BoxAuth api_key=%#&auth_token=%#",[Box boxAPIKey],[defaults objectForKey:#"box-api-auth-token"]];
NSString *payload = [NSString stringWithFormat:#"{\"parent\": {\"id\": %#}}", parentID];
[request setHTTPBody:[NSMutableData dataWithData:[payload dataUsingEncoding:NSUTF8StringEncoding]]];
[request setValue:auth forHTTPHeaderField:#"Authorization"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}
Just as a reminder if you are new to the Box SDK, you will most likely need to update/refresh the data after moving the files. If not handled, your application could crash if the file doesn't exist. Hope this helps to anyone was not sure about this.

NSURL Request? Registration and Login

Here is my code:
//login method
- (int) authenticateClient {
NSString *loginWeb = [NSString stringWithFormat:(#"http://192.168.118.1/login.php?uname=%#&pass=%#&submit=Log%%20In"), user, pass];
NSURL *login = [NSURL URLWithString:loginWeb];
NSData *loginData = [NSData dataWithContentsOfURL: login];
NSString *result = [[NSString alloc] initWithData:loginData encoding:NSUTF8StringEncoding];
NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] init];
[loginRequest setURL:login];
[loginRequest setTimeoutInterval:1];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:loginRequest delegate:self startImmediately:YES];
[connection start];
return [result integerValue];
}
My question is, when simulating having no network connection, my app freezes up and doesn't load or time out in the specified interval (1 second just to see if it works).
I read something about start immediately forces in to run on the current threat causing everything else to "pause" until the action is complete.
I have two questions:
1.) What is a better method to have the URL basically run in the background instead of pausing everything?
2.) How can I get a timeout method that actually works?
Much appreciation! Thanks!
Well, one second as timeout is really low. If you want to simulate different network conditions, you can use something like this. You don't need to this dance:
NSData *loginData = [NSData dataWithContentsOfURL: login];
NSString *result = [[NSString alloc] initWithData:loginData encoding:NSUTF8StringEncoding];
You don't need this as well, since the connection has started already:
[connection start];
Also from the documentation:
NSURLConnection’s delegate methods—defined by the
NSURLConnectionDelegate Protocol protocol—allow an object to receive
informational callbacks about the asynchronous load of a URL request.
Other delegate methods provide facilities that allow the delegate to
customize the process of performing an asynchronous URL load. These
delegate methods are called on the thread that started the
asynchronous load operation for the associated NSURLConnection object.
So for your questions:
Implement the NSURLConnection’s delegate
Check this & this.

How to Upload files created in the iPad?

I am trying to go on with a project but maybe i am asking the wrong questions to google.
I have been using NSURLConnection to download files
NSURLRequest * XMLRequested = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://kilkadg.com/cursos/images/imagenes.xml"]];
NSURLConnection * conexion = [[NSURLConnection alloc] initWithRequest:XMLRequested delegate:self];
and implement the next methods
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[imagenBytes appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[imagenBytes writeToFile:[DataImage imagenSource:self.name] atomically:YES];
}
Now I want to do it in the other way. I want a file created in the iPad, as a photo or a sound, save it in my server.
I haven´t find which class to use, neither how to receive that data in my server.
Thanks
For the creation of sound in the iPad I used http://www.techotopia.com/index.php/Recording_Audio_on_an_iPhone_with_AVAudioRecorder_%28iOS_4%29
To send Post data and files I used this link http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/#comment-8145
So instead of save a .png I saved a .caf

How can i get piped string from server and parse it in xcode?

I have data on a server and I am trying getting this data as a piped string. I wrote the sever-side code but I don't know how can I retrieve this data and parse it in iOS. Here is an example of my data. Is there any article or code that can help me?
A few starting points: For retrieving a stream of data from a server, have a look at Apple's Stream Programming Guide. To parse your data, you can use a NSScanner (String Programming Guide: Scanners) or a CFXMLParser (XML Programming Topics for Core Foundation) if you have valid XML data.
Your output doesn't seem to be in a valid format like xml or json. But if your glat with the content as a string inside your app use this code snippet
- (void)viewDidLoad{
[super viewDidLoad];
NSURLRequest *site_request =
[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://...."]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLConnection *site_connection =
[[NSURLConnection alloc] initWithRequest:site_request
delegate:self];
}
- (void)connection:(NSURLConnection *)site_connection didReceiveData:(NSData *)data{
NSString *site_response =
[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"OUTPUT: %#", site_response);
}
The log will put out the string inside the console.

Resources