send string from iphone [duplicate] - iphone

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sending post data from Iphone
Hello everyone, I'm a new member of the forum and browsing the web I found what was right for me, or rather being a novice I did not understand what poker was.
What I'm trying to do is go to my iphone from a string php server, example: site.com/site.php?val1=1&val2=2 and that the server will recognize it and give me back a xml, but one thing at a time how do I send the string to the server by pressing a button IBAction?

This is what I usually do for that:
NSString *reqURL = [NSString stringWithFormat:#"http://site.com/site.php?val1=%#&val2=%#",var1,var2];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];

Readup in NSURLConnection or use ASIHttpRequest (which is easier to use).

get ASIHTTPRequest from http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
and
import
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
and do following to get response
NSURL *url= [NSURL URLWithString:#"http://site.com/site.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request setPostValue:#"1" forKey:#"val1"];
request setPostValue:#"2" forKey:#"val2"];
[request startSynchronous];
NSString *retVal = nil;
NSError *error = [request error];
if (!error) {
retVal = [request responseString];
}
here you got data on retVal

Related

.Deb Installer for iphone

I am creating an app for jailbroken idevices and need the ability to install .debs into /Library/Themes/ I have looked everywhere for documentation or an example but with not to my surprise I found nothing of great use. I first want to grab the .deb from a URL then just simply install that package into the users folders. If anyone has had any experience with this or might can point me in the right direction that would be greatly appreciated.
Here is a similar question but never seemed to really get answered.
How to install a .deb file on a jailbroken iphone programmatically?
//SYCRONIZED REQUEST
- (IBAction)grabURL:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://freeappl3.com/com.freeapple.quickunlock_0.0.1-
25_iphoneos-arm.deb"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(#"responce String = %#",response);
}
}
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://freeappl3.com/com.freeapple.quickunlock_0.0.1-
25_iphoneos-arm.deb"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
NSString *path = #"/Library/Themes/";
[request setDelegate:self];
[request setDownloadDestinationPath:path];
[request setDownloadProgressDelegate:progress];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(#"responce String = %#",responseString);
// Use when fetching binary data
NSData *responseData = [request responseData];
NSLog(#"responce Data = %#",responseData);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"responce Error = %#",error);
}
Logs
//when I use my method "grabUrl:"
!<arch>
debian-binary 1311198441 0 0 100644 4 `
2.0
control.tar.gz 1311198441 0 0 100644 381 `
/when I use my method "grabUrlInBackground:"
ThemeCatcher2[44212:16a03] responce Error = Error Domain=ASIHTTPRequestErrorDomain Code=8
"Failed to move file from '/var/folders/jw/j5qzb3b51s17ywd9m7vw52y40000gn/T/62973B18-B19A-
47FC-B2FB-A7E7F8C831AA-44212-00042A5738D4841E' to '/Library/Themes/'" UserInfo=0x9151ea0
{NSUnderlyingError=0x9151fa0 "The operation couldn’t be completed. (Cocoa error 4.)",
NSLocalizedDescription=Failed to move file from
'/var/folders/jw/j5qzb3b51s17ywd9m7vw52y40000gn/T/62973B18-B19A-47FC-B2FB-A7E7F8C831AA-
44212-00042A5738D4841E' to '/Library/Themes/'}
try using
system("/usr/bin/dpkg -i <filename_of_deb_including_extension>");
You'll need root privileges for this though.
:)
Use the following code
NSString *appsyncDebPath=#"/var/root/appsync.deb";
NSString *cmdString=[NSString stringWithFormat:#"/usr/bin/dpkg -i %# >/tmp/dpkg.log;",appsyncDebPath];
const char *cmdChar=[cmdString UTF8String];
system(cmdChar);
Before this, you should execute
setuid(0);
setgid(0);

upload image to server

i want to upload an
UIImage
to a server.
the problem i dont know how to add it to my post to the server.
until now i sent post with thia:
NSString *reqURL = [NSString stringWithFormat:#"url"];
reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
there is any different way to do for image?
Use ASIHttpRequest
-(void)uploadImage{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// Upload a file on disk
[request setFile:#"/Users/ben/Desktop/ben.jpg" withFileName:#"myphoto.jpg" andContentType:#"image/jpeg"
forKey:#"photo"];
// Upload an NSData instance
[request setData:UIImageJPEGRepresentation(myUIImage) withFileName:#"myphoto.jpg" andContentType:#"image/jpeg" forKey:#"photo"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
}
This answer points to this wrapper framework that should make it easy to do what you want without dealing with HTTP header strings and so on.
Basically, you'll turn your UIImage into an NSData object using a function like UIImagePNGRepresentation() and then upload it over HTTP.

Post request from iPhone using ASIFormDataRequest not working

Newbie to Rails/iOS here. I have a rails blog application. I'm trying to upload a post from iOS using an HTTP POST method with ASIFormDataRequest. Here's the code that get's called:
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setFile:#"star.png" forKey:#"image"];
[request startSynchronous];
NSString *response = [request responseString];
NSLog(#"response:: %#", response);
When I run this code, nothing happens. My server does not get contacted. I get response:: (null). Any idea why?
EDIT I found out that star.png needed to have its full file address. Now the POST works fine, but neither the name or image get saved into the db. A new post with empty name and image gets created. Any idea why?
why you are using localhost here?
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
use your web server ip instead of localhost
NSURL *url=[[NSURL alloc] initWithString:#"http://yourservername:3000/posts"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
NSURL *url=[[NSURL alloc] initWithString:#"http://localhost:3000/posts"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"name"];
[request setPostValue:imageData forKey:#"image"];
[request startSynchronous];
For the "imagePath" is the path to your image. The full path.

How to read a file from a website using objective-c & xcode

I am trying to write a code that will grab some information provided by my server (remote), for example request a url that will return data that will be represented in the application after parsing it.
I've been trying since 2 days now I googled it i found some incomplete solution but nothing really worked out for me
I am really noob in Xcode and Objective-C
Thanks
Click for the URL-Loading documentation provided by Apple.
Especially Using NSURLConnection looks interesting for you.
Edit:
Another very good and easy-to-use Framework for this task is ASIHTTP:
Click
The easiest way:
- (void)grabURL
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
Asynchronous loading is only slightly more complex:
- (void)grabURLInBackground
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}

iPhone development: How to implement HTTPS connection?

I am working on an iPhone project which need to connect to the IIS server over HTTPS or SSL protocol. Can anyone tell me how to implement HTTPS connection in iPhone? Any advice would be appreciate.
Thank you in advance.
Ben Copsey's ASIHTTPRequest framework makes it trivially easy to issue web requests over SSL.
Modifying the site's example slightly:
NSURL *url = [NSURL URLWithString:#"https://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog (#"%#", response);
}
Just use NSURLConnection with an NSURLRequest pointing to an NUSUL with https protocol, just like that:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://mySecureServer.net"]];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
//do something with response & data
}
But I suggest you have a look at the documentation of the URL loading system in Cocoa.
Jason don't forget to implement the connection:canAuthenticateAgainsProtectionSpace and connection:didReceiveAuthenticationChallenge to make sure you're handling the challenges for the Server authentication and Client Authentication:
check out this post that will help you with this https://devforums.apple.com/message/143091#143091
You probably don't have a public certificate. Can you access the https resource with firefox/safari/chrome without an error? if not - that's the problem, not your code.