2012-01-05 41 views
30

我有一個網址,我在NSString的urlone是來自XML。但我運行的應用程序它使過剩的嚴重錯誤如何打開iPhone上的按鈕點擊網址

// urlone contains http://www.forasinvest.com/v2/index.php 

    NSString*myurl=urlone; 
    NSURL *url = [NSURL URLWithString:myurl]; 
    [[UIApplication sharedApplication] openURL:url]; 
+0

假設'urlone'是有效的,應該沒有錯。你確定'urlone'好嗎? – 2012-01-05 06:21:42

+0

它說outscope而跟蹤我已經聲明,在頭類 – 2012-01-05 06:24:09

+0

NSString * myurl = urlone; NSLog(urlone); NSURL * url = [NSURL URLWithString:myurl]; [[UIApplication sharedApplication] openURL:url]; – 2012-01-05 06:26:12

回答

68

我認爲它應該工作正常,OK只是複製粘貼測試只是

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.forasinvest.com/v2/index.php"]]; 
+0

其開放像這樣的硬編碼,但不工作像我想 – 2012-01-05 08:05:49

+2

我想你忘了把@在初始化urlone – 2012-01-05 09:03:21

+0

工作完美。非常感謝 – Septronic 2015-12-22 13:12:17

11

的NSString *的pURL = @「xyz.cpm」以下;

+1

我在xcode 8和ios 10上試過了。它正在工作。 – 2016-12-24 09:49:42

0

斯威夫特3版本:

let url = URL(string: "http://www.apple.com")! 
if #available(iOS 10.0, *) { 
    UIApplication.shared.open(url, options: [:], completionHandler: nil) 
} else { 
    UIApplication.shared.openURL(url) 
}