从plist文件加载Webview Url

时间:2016-01-02 11:53:05

标签: ios swift

我是iOS新手。我从plist文件中设置了webview中的url。下面的代码是客观的C.

Swift会有什么相同的东西?`

NSString *path = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];       
[_webView loadRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[dict objectForKey:@"webViewURL"]]]];`

2 个答案:

答案 0 :(得分:1)

试试这个,

let plistPath = NSBundle.mainBundle().pathForResource("YourPlistName", ofType: "plist")
let dict = NSDictionary(contentsOfFile: plistPath!)
webview.loadRequest(NSURLRequest(URL: NSURL(string: dict?.objectForKey("webViewURL") as! String)!))

答案 1 :(得分:1)

以下是等效代码:

var webview: UIWebView?

func loadwebView() {
    if let plist = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist"),
        let plistDict = NSDictionary(contentsOfFile: plist),
        let urlString = plistDict.objectForKey("webViewURL") as? String,
        let url = NSURL(string: urlString) {

            webview?.loadRequest(NSURLRequest(URL: url))
    }
}