2010-07-03 50 views
0

大家好我更新到iOS 4,但在我的应用程序使用:功能的IOS弃用4

NSString *connected = [NSString string withContentofURL:[NSURL URLWithString:@"http://myurl.com/myFile]]; 

,但现在我得到如下:

StringWithContentsofURL is deprecated ! 

我用这个来测试连接可用。

我能做什么?

感谢

+0

检查此堆栈问题:http://stackoverflow.com/questions/2039203/what-is-the-stringwithcontentsofurl-replacement-for-objective-c – Alan 2010-07-03 00:56:18

回答

3

如iPhone OS2(因此这不是新的)的

[NSString withContentsOfURL: (NSURL*) url]

方法已被替换

+ (id)stringWithContentsOfURL: (NSURL *)url encoding: (NSStringEncoding)enc error: (NSError **)error

下面是使用新的签名的例子:

NSError* error = nil; 
NSURL* url = [NSURL urlWithString: @"www.google.com"]; 
NSString* stringForUrlPath = [NSString stringWithContentsOfURL: url 
                 encoding: NSUTF8StringEncoding 
                 error: &error]; 

请参阅this为您的NSStringEncoding选项。

+0

是啊,但编码? – sarlin13 2010-07-03 00:58:15

+0

你正在访问什么样的网站?很可能NSUTF8StringEncoding应该很好地为你服务。 – sdolan 2010-07-03 01:33:36

0

使用

NSError* error; 
NSString *string = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 

代替。苹果不应该包含stringWithContentsOfURL:(无编码)开始。它在OS X上已被弃用了很长一段时间,因为这是导致非英语使用者头痛的原因。

也就是说,不要下载某些东西来测试连通性。相反,使用Reachiability

+0

可达性只会告诉你,如果你有3G或WiFi连接,而不是如果你可以到达一个网站。最好的检查一个地址是否可达是实现一个ping类。 Apple在这里有一个例子:http://developer.apple.com/mac/library/samplecode/SimplePing/Introduction/Intro.html – sdolan 2010-07-03 01:14:13