How to upload video to a server - objective-c

In my app i have to upload the video to the web server using POST method. For this iam using UIImagePickerController. But when i am selecting video from my ImagePickerController it is going on by showing "compressing Video". I have written picker allowsEditing to NO also and the video saved in photo library is of duration just 50 seconds only. But still i am getting the same. Immediate response is greatly appreciated.

Try this:-
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *str = [NSString stringWithFormat:#"%#/uploadVideoIphone.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *filePath = [urlvideo path];
[request addFile:filePath forKey:#"uploadfile"];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:200];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseString %#",[request responseString]);
[self dismissModalViewControllerAnimated:YES];

Related

How to send Array of objects to JSON?

I am hitting a web service with array of objects. But in server side they are getting only null value for all the fields.
Client side code is:
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self];
//************DATA formation
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc]init];
NSMutableDictionary *jsonDict1 = [[NSMutableDictionary alloc]init];
[jsonDict setObject:#"3" forKey:#"rollNo"];
[jsonDict setObject:#"Ezhil" forKey:#"FirstName"];
[jsonDict setObject:#"Arasu" forKey:#"LastName"];
[jsonDict1 setObject:#"4" forKey:#"rollNo"];
[jsonDict1 setObject:#"XYZ" forKey:#"FirstName"];
[jsonDict1 setObject:#"ABC" forKey:#"LastName"];
NSArray *jsonArray=[[NSArray alloc]initWithObjects:jsonDict,jsonDict1, nil];
//Converting to JSON string.
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSString *jsonString = [writer stringWithObject:jsonArray];
NSLog(#"JSON String : %#",jsonString);
//************Setting DATA in URL
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[urlRequest setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[urlRequest setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[connection start];
// Code for response....
I dont know were i went wrong. Can any one help me for this issue.
Thanks in advance.
I'd scrap the SBJson stuff and just use the native controls...
Do everything the same as you are doing but remove the SBJsonWriter stuff and do this to set the body of the request...
[urlRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:jsonArray options:NSJSONWritingPrettyPrinted error:&error]];
That should work.
Thank you.. Application is working fine without any modification. The problem is with server side. But i think we can go with your suggestion instead of using external framework.
In both the case I am getting the json object like this,
JSON String :
[{"rollNo":"3","FirstName":"Ezhil","LastName":"Arasu"},
{"rollNo":"4","FirstName":"XYZ","LastName":"ABC"}]

Reddit API ios modhash/cookie issue

Im' trying to submit to Reddit via my iOS app. I can login fine and am sent back a modhash and a cookie which I save via NSUserDefaults.
The issue is when I post the data I keep getting "USER_REQUIRED" as the response, even though I have included the modhash in the post and set my session cookie. I have even included the following in my app delegate:
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
But it still doesn't work. Here is my code:
-(void) post {
NSString *modhash2 = [[NSUserDefaults standardUserDefaults]
objectForKey:#"modhash"];
NSString *urlString = [NSString
stringWithFormat:#"https://ssl.reddit.com/api/submit"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *contentType = [NSString stringWithFormat:#"application/x-www-form-urlencoded;"];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[request addValue:redditCookie forHTTPHeaderField:#"Cookie"];
[request setHTTPMethod:#"POST"];
NSString *httpBody = [NSString stringWithFormat
:#" ?uh=%#&kind=link&url=%#&sr=%#&title=%#&r=%#&api_type=json",
modhash2,
#"www.google.com",
#"test",
#"Google.com",
#"test"];
[request setHTTPBody:[httpBody dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse* response;
NSError* error = nil;
NSData* result = [NSURLConnection
sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:result
options:NSJSONReadingMutableContainers
error:nil];
NSDictionary *responseJSON = [json valueForKey:#"json"];
NSLog(#"RETURN: %#",responseJSON);
}
Any ideas?

load pages in UIWebview

I am trying to display web pages using UIwebview on iPad.
I used following code :-
NSString *urlAddress=#"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSLog(#"The value of url in screen 2 is %#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
web is an object of type uiwebview .
This works fine with certain links but when i tried to open below link
http://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#infohttp://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#info
It does not display anything .Read few articles and taught this could be related to a proxy issue
How to deal with proxy setting in UIWebView?
so tried to use this but no success . code used is as below :
NSURL *url = [NSURL URLWithString:#"http://google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(#"error is %#",response);
}
But now nothing loads up in webview .how do i tackle this .

Uploading Photo with ASIHTTPRequest

I'm using ASIHTTPRequest to upload a photo with PHP. The PHP side is proven to work(I'm using it for android), and I'm trying to get the iOS component to work.
Here is my upload:
NSString *myurl = #"http://mydomain.tld/php/upload.php?casenum=";
myurl = [myurl stringByAppendingFormat: casenumber];
NSString *fixedURL = [myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSString stringWithFormat:#"%#",fixedURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *imageData = UIImageJPEGRepresentation(image1.image, 90);
[request setData:imageData withFileName:#"myphoto.jpg" andContentType:#"image/jpeg" forKey:#"file"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(#"Response: %#", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request startAsynchronous];
Ive found this on the internet and when I run it, it fails with the error 2012-01-17 10:52:16.939 MyApp[30373:707] Error: NSInvalidArgumentException
From many of the documentation and examples I've found online, this should work, but it isn't, as you can see with the exception. Any help at ironing out the kinks? If you need any other information, I'll gladly post it.
There would appear to be a couple of errors in the way that you're producing the NSURL. Firstly, it's only the parameters that need escaping, not the whole URL, so
NSString *myurl = #"http://mydomain.tld/php/upload.php?casenum=";
myurl = [myurl stringByAppendingFormat: casenumber];
NSString *fixedURL = [myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
should be
NSString *myurl = #"http://mydomain.tld/php/upload.php?casenum=";
NSString *escapedCasenumber = [casenumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
myurl = [myurl stringByAppendingFormat:escapedCasenumber];
Secondly, you're then trying to assign an NSString to an NSURL, so
NSURL *url = [NSString stringWithFormat:#"%#",fixedURL];
should be
NSURL *url = [NSURL urlWithString:myurl];
Finally, the second argument passed to UIImageJPEGRepresentation should be a float value between 0.0 and 1.0, so I'm guessing
NSData *imageData = UIImageJPEGRepresentation(image1.image, 90);
should be
NSData *imageData = UIImageJPEGRepresentation(image1.image, 0.9);
If it still doesn't work after these changes, then do as JosephH suggests and use the debugger to identify exactly which line is causing the exception

HTTP Digest Authentication using asiHttpRequest

I am trying to use asiHttpRequest to handle HTTP Digest Authentication, I have two textfields, one is name and the other is password, when the user login with the correct name and password a label will display the auth and userid response string. Can anyone provide
Here is a sample code of how u can sent request to google for authentication. You can change it for digestion. Hope it helps.
NSString *username = #"my.google.login#gmail.com";
NSString *password = #"mypassword123";
NSString *loginUrl = #"https://www.google.com/accounts/ClientLogin?client=NNW-Mac";
NSString *source = #"NNW-Mac"; //let's fake NetNewsWire
NSString *continueUrl = #"http://www.google.com";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString: loginUrl]]; // log in & get cookies
[request addRequestHeader: #"User-Agent" value: #"NetNewsWire/3.2b25 (Mac OS X; http://www.newsgator.com/Individuals/NetNewsWire/)"];
[request setPostValue: username forKey: #"Email"];
[request setPostValue: password forKey: #"Passwd"];
[request setPostValue: #"reader" forKey: #"service"];
[request setPostValue: source forKey: #"source"];
[request setPostValue: continueUrl forKey: #"continue"];
[request setDelegate: self];
[request setDidFailSelector: #selector(loginRequestFailed:)];
[request setDidFinishSelector: #selector(loginRequestFinished:)];
[request start];
Edit 1:
NSString *username = #"Login info";
NSString *password = #"Password";
NSString *loginUrl = #"http://connect.twangoo.com";
NSString *continueUrl = #"http://www.google.com";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString: loginUrl]]; // log in & get cookies
//Dont need it if u dont want to add any header value for the site
//[request addRequestHeader: #"User-Agent" value: #"NetNewsWire/3.2b25 (Mac OS X; http://www.newsgator.com/Individuals/NetNewsWire/)"];
[request setPostValue: username forKey: #"Login"];
[request setPostValue: password forKey: #"Pwd"];
//Just for google api reader service u dont need it here..
//[request setPostValue: #"reader" forKey: #"service"];
[request setDelegate: self];
[request setDidFailSelector: #selector(loginRequestFailed:)];
[request setDidFinishSelector: #selector(loginRequestFinished:)];
[request start];