Changing url for NSMutableURLRequest after its being initialized - iphone

I am trying to see if there is a way to change the request url once the NSMutableURLRequest is being initialize? I looked into the instance method, but nowhere it indicates that you can change the url. I tried to make a request of "URL" and change it accordingly, but it still points to the old url.
Anyone know how to change this would like to know it.
Thanks.

There is a method on NSMutableURLRequest called - (void)setURL:(NSURL *)theURL
Here's an example:
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
NSURL *anotherURL = [NSURL URLWithString:#"http://www.yahoo.com"];
[req setURL:anotherURL];
Specifically, the method documentation is at:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableURLRequest/setURL:

Related

how to pass arguments and parameters in json web service?

-(void)gettop10button
{
NSURL *url=[NSURL URLWithString:#"http://appabn.com/iphone/files/api/api.php?q=helo_users&u_id=5&page=1"]; // web api link
NSURLRequest *request=[NSURLRequest requestWithURL:url];
connection=[NSURLConnection connectionWithRequest:request delegate:self];
in the last of the link we see that page=1 , i have more than 10 pages .
so how can i pass a variable/parameter/argument , so i can increment page no.
Thanks in advance 'int page;
page++;
can perform like as .
in the last of the link we see that page=1 , i have more than 10 pages .
so how can i pass a variable/parameter/argument , so i can increment page no.
Thanks in advance 'int page;
page++;
can perform like as .
if(connection)
{
webData=[[NSMutableData alloc]init];
}
[JustConfesstable reloadData];
It's so simple. I am still amazed which part of the code you are not getting. You just have to take one variable (say pageNo) of type int and then provide it the default value 1.
You just have to increment the pageNo (using pageNo++) to get the next page value and then you have to create string for URL with pageNo and then simply pass it to URL using the method called URLWithString of class NSURL.
Translation of all the above description in 4 easy steps using Objective-C :
NSString *urlString = [NSString stringWithFormat:#"http://appabn.com/iphone/files/api/api.php?q=helo_users&u_id=5&page=%d",pageNo];
NSURL *url = [NSURL URLWithString:urlString]; // web api link
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];

How to make url and google bar into one UIWebView

in the new safari on the mac they have a bar that can distinguish between google and a url.Thats what i am trying to do. I am wondering what i am doing wrong. It only wants to google not search url. Thanks in advance.
-(IBAction)SearchAll:(id)sender
{
if (googlebar) {
NSString *query = [googlebar.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.co./search?q=%#", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
else{
NSString *query = [googlebar.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.%#", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
}
Firstly, your code is way off. You will not achieve your goal this way.
Secondly, You should start off your IBAction statement by checking if the string that was input into the search bar (or urlbar whatever you want to call it) has a prefix of .com or .org...etc. If it does not then it then google searches the text in the UITextfield.
are you sure of your if statement?
you're using searchbar in the else, where searchbar if supposed not to exist.
you should also check the google.co./search url. I'd use google.com/search as it will save you a redirect
I'm still confused by your question though, as you have two toolbars: searchbar and googlebar, not one as in the latest safari for mac

data from iPhone to php

i have this in Xcode:
NSString *post =[[NSString alloc] initWithString:#"message";
this string i want to send to myadress.php. anybody help me with some good reference or code please.
this is my php side:
<?
$connect = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
$feed = $_GET['feed_message'];
$login = mysql_query("INSERT INTO feedback SET feed_message ='".$feed."'", $connect) or die(mysql_error());
mysql_close($connect);
?>
You can try this and you need implement the NSURLConnection delegate method
NSURL *url = [NSURL URLWithString:#"http://yoururl"];
NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlReq delegate:self];
[connection start];
You can also use ASIHttpRequest, it has more Features.
In fact you just want to send a request to specific URL containing parameter "feed_message":
http://myserver.com/myscrypt.php?feed_message=message
To do it you need:
a. Create an url:
NSString * scriptURLString = #"http://myserver.com/myscrypt.php";
NSString * parameterName = #"feed_message";
NSString * post = #"message";
NSString * urlString = [NSString stringWithFormat:#"%#?%#=%#",scriptURLString, parameterName, post];
NSURL * url = [NSURL URLWithString:urlString];
b. send request to url. There are many ways to do it depending on your needs.
You may send asynchronous request (set delegate property and implement delegate methods to handle results)
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:nil];
or simply use NSData synchronous method:
[NSData dataWithContentsOfURL:url];

NSURLConnection or NSurl?

what is the difference between NSURLConnection and NSURL?
i mean if i am downloading a file, does it make and difference which one i use?
Rgds
for:
NSString *myUrl = #"http://www.test.com/";
NSString *returnData = [NSString stringWithContentsOfURL:[NSURL URLWithString: myUrl]];
or
NSString *myUrl = #"http://www.test.com/";
NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString:myUrl] ];
NSString *returnData = [NSURLConnection sendSynchronousRequest:myRequest returningResponse: nil error: nil ];
whats the difference?
thks
The Connection
An NSURLConnection object provides support to perform the loading of a URL request.
The Request
NSURLRequest objects represent a URL load request in a manner independent of protocol and URL scheme.
E.g. requestWithURL:
Creates and returns a URL request for a specified URL with default cache policy and timeout value.
+ (id)requestWithURL:(NSURL *)theURL
The URL
The NSURL class provides a way to manipulate URLs and the resources they reference. NSURL objects understand URLs as specified in RFCs 1808, 1738, and 2732. ...
To get the contents of a URL, NSString provides stringWithContentsOfURL: and NSData provides dataWithContentsOfURL:.
References:
NSURLConnection_Class/Reference/Reference.html
NSURL_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURL
Best thing about NSURLConnection is its asynchronous behaviour so that you dont have to wait until the url is loaded.

What is the quickest way to get the String contents of a URL using Cocoa/iPhoneSDK?

Say I want to get the HTML of
http://www.google.com
as a String using some built-in classes of the Cocoa Touch framework.
What is the least amount of code I need to write?
I've gotten this far, but can't figure out how to progress. There must be an easier way.
CFHTTPMessageRef req;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
req = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
CFSTR("GET"),
(CFURLRef)url,
kCFHTTPVersion1_1);
The quickest way is to use NSString's +stringWithContentsOfURL: method. However, this is a modal call, and your application will be non-responsive while it runs. You can either move it to a background thread, or use the NSURLConnection class to make a proper, asynchronous request.
One way to do this is as follows, however as Ben Gottlieb points out, this is a synchronouseRequest and will cause your program's execution to wait on the return of this function call, possibly making your application non-responsive.
NSURL *url = [ NSURL URLWithString: #"http://www.google.com"];
NSURLRequest *req = [ NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30.0 ];
NSError *err;
NSURLResponse *res;
NSData *d = [ NSURLConnection sendSynchronousRequest:req
returningResponse:&res
error:&err ];
You can find information on writing the proper delegate methods to handle a Asynchronous Connection here on the Apple dev-docs.