Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-20-2011, 09:21 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Smile Change attribute in URL

Hi,

I have a url:
www.test.com/webservice/id=1111

How can i change the id value 1111 in the url. I tried something like this:

Code:
NSURL = [[NSURL alloc] initWithContentsOfURL:@"www.test.com/webservice/id=%@", string];
Is this the wrong way of doing. Should i use ASIHTTPRequest? Can someone point me in the right direction, that would be nice!

DrJones is offline   Reply With Quote
Old 12-20-2011, 09:25 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DrJones View Post
Hi,

I have a url:
www.test.com/webservice/id=1111

How can i change the id value 1111 in the url. I tried something like this:

Code:
NSURL = [[NSURL alloc] initWithContentsOfURL:@"www.test.com/webservice/id=%@", string];
Is this the wrong way of doing. Should i use ASIHTTPRequest? Can someone point me in the right direction, that would be nice!

You're close. However, NSURL does not have a method initWithContentsOfURL. You should replace initWithContentsOfURL with initWithString in the above code.

You also need to make sure that the string you pass in does not contain any characters that need to be escaped, like "/", "?", quotes, spaces, "%", ":", etc. If it might, you will need to percent escape it before inserting it into your URL string.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-20-2011, 09:41 AM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You're close. However, NSURL does not have a method initWithContentsOfURL. You should replace initWithContentsOfURL with initWithString in the above code.

You also need to make sure that the string you pass in does not contain any characters that need to be escaped, like "/", "?", quotes, spaces, "%", ":", etc. If it might, you will need to percent escape it before inserting it into your URL string.
Thanks for the quick answer. I'm still having some trouble after following your advice. Currently i have this piece of code which takes a value from a textfield and convert it into a string. When i try to put that string in a url, the compiler says that have to many arguments in the method call.

Code:
int number = ([textfield.text integerValue]);
    NSString *numberString = [[NSString alloc] initWithFormat:@"%d", number];
    
    NSLog(@"number = %@", numberString);
    
    NSURL *url = [[NSURL alloc] initWithString:@"http://www.test.com/webservice/?id=%@", numberString];
DrJones is offline   Reply With Quote
Old 12-20-2011, 09:52 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by DrJones View Post
Thanks for the quick answer. I'm still having some trouble after following your advice. Currently i have this piece of code which takes a value from a textfield and convert it into a string. When i try to put that string in a url, the compiler says that have to many arguments in the method call.

Code:
int number = ([textfield.text integerValue]);
    NSString *numberString = [[NSString alloc] initWithFormat:@"%d", number];
    
    NSLog(@"number = %@", numberString);
    
    NSURL *url = [[NSURL alloc] initWithString:@"http://www.test.com/webservice/?id=%@", numberString];

I'm guessing that the last line is the one that's throwing an error?

The method initWithString only takes one parameter, and you are passing 2 parameters.

You need to first create an URL string, then pass that string to the initWith string method.

Add a new local variable theURLString. Use stringWithFormat to build theURLString from your URL string, and the number string. Then pass theURLString to the NSURL method initWithString.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-20-2011, 09:57 AM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 36
DrJones is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
I'm guessing that the last line is the one that's throwing an error?

The method initWithString only takes one parameter, and you are passing 2 parameters.

You need to first create an URL string, then pass that string to the initWith string method.

Add a new local variable theURLString. Use stringWithFormat to build theURLString from your URL string, and the number string. Then pass theURLString to the NSURL method initWithString.
Ahh, thanks you are great!
DrJones is offline   Reply With Quote
Reply

Bookmarks

Tags
attribute, ios, url, webservice

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 394
13 members and 381 guests
7twenty7, AppsBlogger, Creativ, Dalia, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0