2012-08-15 32 views
4

工作,下面的代碼:的UIApplication的OpenURL:不與相對URL

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

它是蘋果的錯誤?

+0

'absoluteString'不回什麼是 「中」 的'NSURL'。您可能試圖打開網址的相關部分。你的錯誤是什麼? – Dustin 2012-08-15 12:30:18

+0

沒有錯誤。方法調用後沒有任何反應。 – HiveHicks 2012-08-15 19:00:26

+3

你有沒有想過*爲什麼會發生這種情況?我遇到完全相同的行爲。這沒有意義。 – Nik 2013-05-01 00:11:12

回答

2
在我的Xcode輸出 NSLog(@"NEW %@", newsUrl)這裏我聲明 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:]是給你以不同的格式的URL。這是你的結果無效的原因。

+0

這就是我想說的,實際測試它的道具。 – Dustin 2012-08-15 19:08:28

-1

不是真正清楚你要完成什麼,但如果你想與潛在不同的主機或路徑,以編程方式建立自己的網址,爲什麼不這樣的事情:

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

[[UIApplication sharedApplication] openURL:newsUrl]; 
+2

我有一個靜態主機和許多動態相對路徑。我認爲Apple推出了[NSURL URLWithString:relativeToURL:],這樣人們就不必手動連接URL的部分爲NSString。顯然,我錯了。我沒有看到任何其他使用相對NSURLs。任何人? – HiveHicks 2012-08-16 11:20:16

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]; 
相關問題