2011-04-14 76 views
0

我一直试图从plist文件获取一个字符串到NSURL中进行webview。 我得到'零'为返回值,或什么都没有(在控制台中没有错误)将NSString从plist文件转换为NSURL

我知道这个代码有问题,但我不能指出它在哪里。

NSString *filePath = @"/path/to/Info.plist"; 
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

    NSString *value; 
    value = [plistDict objectForKey:@"Link"]; 
    NSString *webStringURL = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *URL = [NSURL URLWithString:webStringURL]; 
    [self loadURL:URL]; 
    [self setURLToLoad:nil]; 

我在哪里搞砸了?

回答

1
NSString *filePath = @"/path/to/Info.plist"; 
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

    NSString *urlString = [plistDict objectForKey:@"Link"]; 

    NSURL *URL = [NSURL URLWithString:urlString]; 
    [self loadURL:URL]; 

为了您更好的理解是指这个网站:

http://iphonesdevsdk.blogspot.com/2011/04/plist.html

它可以帮助您在轻松的方式使用的plist。

+0

感谢您的链接和提示。我曾经使用过这个代码(+它的变体),但不知何故它没有工作。现在我知道为什么。您必须在'www'链接前使用http://,否则不会加载任何内容。这是我几天来的问题。认为webview不使用没有http头的链接,嘿。 – devilhunter 2011-04-15 12:33:04