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

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];
}

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);

How to tell when a request is done (ASIHTTPRequest)?

How do you tell when a request is finished (using ASIHTTPRequest)? Here is the code I am using:
[request setDidFinishSelector:#selector(requestFinished:)];
...
-(void)requestFinished:(ASIHTTPRequest *)request {
NSLog(#"The request is done.");
}
However, when request is done, "The request is done" is never printed to the log. Am I doing something wrong? Thanks.
maybe your connection is failing? In this case, you should test for - (void)requestFailed:(ASIHTTPRequest *)request
Did you set the request delegate? Example code:
- (IBAction)grabURLInBackground:(id)sender
{
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];
}
I forgot to set the delegate to self. Oops!
[request setDelegate:self];

Post Data but GET Method json -ios

how can i use the get method to post data on an api link with 6 values and a json format?
im really fuzzled. im doing this for days . is it best to have 6 textfields that will be used to write the value and a button to indicate post data?
check out my code and please help.
-(IBAction)postDataPressed
{
NSString *urlString = #"http://192.168.18.8/apisample2/friendb.php?fm=jsn";
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:user.text forKey:#"un"];
[request setPostValue:pass.text forKey:#"pd"];
[request setPostValue:gender.text forKey:#"gd"];
[request setPostValue:age.text forKey:#"ag"];
[request setPostValue:status.text forKey:#"st"];
[request setPostValue:lookfor.text forKey:#"lf"];
[request setRequestMethod:#"GET"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(#"Response: %#", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request startAsynchronous];
}
i hope someone will help.
NSString *request_url = [NSString stringWithFormat: #"http://192.168.18.8/apisample2/friendb.php?fm=jsn&un=%#&pd=%#&gd=%#&ag=%#&st=%#&lf=%#",
user.text, pass.text, gender.text, age.text,status.text,lookfor.text];
NSURL *url = [NSURL URLWithString:request_url];
ASIHttpRequest *request = [ASIHttpRequest requestWithURL:url];
request.delegate = self;
....
HTTP Get method only accept data in url, called query string, so you have to build query string yourself.

Upload video taken with iphone camera with PUT method

How can i upload a video taken with the camera or contained in the phone memory?
I need to upload it using a PUT method.
Thank you
Code for photo uploading:
- (void)uploadPhoto:(NSData *)data
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"PUT"];
[request addRequestHeader:#"Date" value:timeStamp];
[request addRequestHeader:#"Auth" value:signature];
[request addRequestHeader:#"Client" value:apiClient];
[request addRequestHeader:#"Accept" value:apiAccept];
[request addRequestHeader:#"USER" value:userHeader];
[request appendPostData:data];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(#"%#",responseString);
// Use when fetching binary data
NSData *responseData = [request responseData];
NSLog(#"%#",responseData);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"%#",error);
}
none of the 3 methods at the end are called after i make this request.
Please help me.
Regards,
response string:
|?xml version="1.0" encoding="UTF-8"?|
|error|
|code|InvalidParameter|/code|
|message|Invalid parameter specified (user)|/message|
|/error|
You can do it using ASIHTTPRequest. Here is a sample:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:myVideoData];
[request setRequestMethod:#"PUT"];

Is the ASIHTTPRequest is not asynchronous?

I am using ASIHTTPRequest to fetch some data from a web service.
I am making requests using a loop.
The problem is that it doesn't seem the request is going asynchronously so that my activityindicator is not working .
Is it true that ASIHTTPRequest is not asynchronous .
or should i use the regular nsmutablerequest to perform asynchronous request .
You should put your request in a download queue, i.e.
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:request];
[request release];
Just
[request startAsynchronous];
runs the request on the UI thread, so you should try it with download queue.
For Sync Synchronous
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];
}
Creating an asynchronous request
- (IBAction)grabURLInBackground:(id)sender
{
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];
}
And there is lot of more options like Queue and many more
You can refer http://allseeing-i.com/ASIHTTPRequest/How-to-use