4

这是代码:

NSURL *newsUrl = [NSURL URLWithString:@"/Document/News/1593" relativeToURL:[NSURL URLWithString:@"http://exist.ru"]];

// Outputs "http://exist.ru/Document/News/1593"
NSLog(@"%@", [newsUrl absoluteString]);

// works
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[newsUrl absoluteString]]];

// doesn't work
//[[UIApplication sharedApplication] openURL:newsUrl];

是苹果的bug吗?

4

3 回答 3

2

NSLog(@"NEW %@", newsUrl)在我声明newUrl为的 Xcode 输出中

NSURL *newsUrl = [NSURL URLWithString:@"/Document/News/1593" relativeToURL:[NSURL URLWithString:@"http://exist.ru"]]:

NSLog输出是

/Document/News/1593 -- http://exist.ru

但对于 [newsUrl absoluteString]

NSLog输出是

http://exist.ru/Document/News/1593

所以我猜这[URLWithString: relativeToURL:]是给你不同格式的 URL。这就是您的结果无效的原因。

于 2012-08-15T12:25:12.800 回答
0

在 iOS 9.x 中似乎不再是问题。

对于 < iOS 9.x 支持,在调用openURL之前添加这个非常无用的步骤:

//Construct your relative URL 
NSURL *url = [NSURL URLWithString:@"path/to/whatever" relativeToURL:
    [NSURL URLWithString:@"http://www.example.com"]];

//FIX
url = [NSURL URLWithString:url.absoluteString];
于 2016-03-18T17:30:47.943 回答
-1

Not real clear on what you're trying to accomplish, but if you want to programmatically build your URL's with potentially different hosts or paths, why not something like this:

NSURL *newsUrl = [NSURL URLWithString:
 [NSString stringWithFormat:@"%@%@",@"http://exist.ru",@"/Document/News/1593"]];

[[UIApplication sharedApplication] openURL:newsUrl]; 
于 2012-08-15T19:12:52.350 回答