使用openURL发送NSDictionary

时间:2012-01-02 15:27:04

标签: iphone objective-c ios

我想使用

打开其他应用程序
[[UIApplication sharedApplication]openURL:[[NSURL alloc]initWithString:myString]];

作为myString的日志就像

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

它工作正常,但如果我尝试使用NSDictionary之类的

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

失败而没有错误

希望你能帮助我。

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

在格式字符串中使用NSDictionary时,会得到如下所示的URL:

testHandleOpenUrl://?{
    bar = foo;
}

在userInfo上调用-description的结果只是替换了%@。据推测,您希望传递URL中字典中包含的参数。可能是这样的:

NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", nil];
NSString *myString=[NSString stringWithFormat:@"testHandleOpenUrl://?foo=%@", [userInfo objectForKey:@"foo"]];
NSLog(@"myString: %@", myString); // prints "myString: testHandleOpenUrl://?foo=bar"

答案 2 :(得分:0)

尝试枚举字典中的所有元素并将其附加到您的网址。

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