Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Weird NSURL and NSString Problems

Weird NSURL and NSString Problems
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 9, 2004, 11:30 AM
 
Hey guys,

Im trying to launch Mail.app using NSWorkspace. So I use the following code:

Code:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"mailto:you@there.com?Subject=Hello%20There&Body=Hi%20There"]];
This opens fine in Mail.app and fills in the appropriate fields with spaces. But when I try this:

Code:
NSString *subject = [NSString stringWithFormat:@"Tag%20Registration"]; NSString *body = [NSString stringWithFormat:@"Body"]; [NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?Subject=%@&Body=%@", [emailAddressCell stringValue], subject, body]]];
If I use the above code, when it launches Mail.app, the subject is shown as TagRegistration. With no space. How would I go about adding spaces properly?

Thanks,
Oliver
     
Dedicated MacNNer
Join Date: Apr 2004
Status: Offline
Reply With Quote
May 9, 2004, 11:54 AM
 
[NSString stringWithFormat:@"somethin%20some"];
isn't what you want...
just set the string equal to whatever you want, like so:

NSString * subject = @"Tag Registration";


should work fine.

the thing is that cocoa looks at the % as a format thing... %d and then another argument prints the argument as a decimal, and such... I don't think %2 or %20 is one of the options, so that's probably why you've got an error.
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 9, 2004, 11:59 AM
 
Hmm,

This code didn't seem to work, Mail wouldn't open:

Code:
- (void)openMail:(id)sender { NSString *subject = @"Tag Registration"; NSString *body = @"Body Is Here"; [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?Subject=%@&Body=%@", [emailAddressCell stringValue], subject, body]]]; }
But, if there is no spaces, it works fine.

Any ideas appreciated,
Oliver
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 9, 2004, 04:34 PM
 
You can't use "%20" in a printf-style format string because the percent character is what identifies the places where variables are to be inserted. To include a literal percent character, use "%%".

Since your URL is constructed partially from user-entered text, though, it'd be better to do your percent-escaping after you construct the string. So, let "Tag%20Registration" be "Tag Registration", construct your URL string, and then use -stringByAddingPercentEscapesUsingEncoding: (see NSURL.h) to turn it into a URL-safe string before making an NSURL from it.
Rick Roe
icons.cx | weblog
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 10, 2004, 12:03 PM
 
Hey thanks,

That did the trick.

Thanks again,
Oliver
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 06:30 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2