UIApplication openURL:不使用相对URL

时间:2012-08-15 12:14:05

标签: objective-c ios cocoa-touch nsurl uiapplication

以下是代码:

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

这是Apple的错误吗?

3 个答案:

答案 0 :(得分:2)

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

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:]会以不同的格式提供您的网址。这是您的结果无效的原因。

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

答案 2 :(得分:-1)

不清楚你要完成什么,但是如果你想以编程方式使用可能不同的主机或路径构建你的URL,为什么不能这样:

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

[[UIApplication sharedApplication] openURL:newsUrl]; 
相关问题