0

I want to open an other application using

[[UIApplication sharedApplication]openURL:[[NSURL alloc]initWithString:myString]];

as log as myString is like

NSString *myString=[NSString stringWithFormat:@"testHandleOpenUrl://?%@",@"123"];

it works fine but if I try to use an NSDictionary like

NSString *myString=[NSString stringWithFormat:@"testHandleOpenUrl://?%@",userInfo];

it fails without an error

Hope you can help me.

Starbax
  • 1,005
  • 2
  • 12
  • 32

3 Answers3

0
NSString * paramString = @"";
int i = 0;
for(NSString * key in [userInfo allKeys]){
    NSString * value = (NSString*)[userInfo objectForKey:key];
    NSString * valueParam = [NSString stringWithFormat:@"%@%@=%@",(i==0)?@"?":@"&",key,value];
    paramString = [paramString stringByAppendingString:valueParam];
    i++;
}
NSString *myString=[NSString stringWithFormat:@"testHandleOpenUrl://%@", paramString];
Philippe Sabourin
  • 8,066
  • 3
  • 31
  • 46
  • no, i would like to send the whole userInfo. because I cannot be sure what kind of information in userinfo i'll get – Starbax Jan 02 '12 at 15:49
  • if you need the whole information, you should parse the NSDictionary to a JSON String ( with SBJson or JSONKit ) and than base64 encode the JSON String ( mayb with http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk ). You can now append the url with the base64 encoded JSON String. – CarlJ Jan 02 '12 at 15:56
  • He's right, you should encode this since it will be in a URL. I also assumed that every key/value are strings in the dictionary, which may not be the case. – Philippe Sabourin Jan 02 '12 at 15:59
  • ok, I have a JSONString. but I don't get the Base64 Stuff from http://www.cocoadev.com/index.pl?BaseSixtyFour working – Starbax Jan 02 '12 at 17:21
0

When you use an NSDictionary in the format string, you get a URL that looks like this:

testHandleOpenUrl://?{
    bar = foo;
}

The result of calling -description on userInfo is simply substituted for the %@. Presumably, you want to pass parameters contained in the dictionary in the URL. Probably something like this:

NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", nil];
NSString *myString=[NSString stringWithFormat:@"testHandleOpenUrl://?foo=%@", [userInfo objectForKey:@"foo"]];
NSLog(@"myString: %@", myString); // prints "myString: testHandleOpenUrl://?foo=bar"
Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
0

Try enumerating all the elements in your dictionary and appending those to your URL.

NSMutableString *params = [[[NSMutableString alloc] init] autorelease];

NSEnumerator *keys = [userInfo keyEnumerator];

NSString *name = [keys nextObject];
while (nil != name) {

    [params appendString: name];
    [params appendString: @"="];
    [params appendString: [userInfo objectForKey:name]];

    name = [keys nextObject];

    if (nil != name) {
        [params appendString: @"&"];
    }
}

NSString *myString=[NSString stringWithFormat:@"testHandleOpenUrl://?%@",params];
Ignacio Inglese
  • 2,605
  • 16
  • 18