无法弄清楚这个Obj-C到Swift的转换

时间:2015-04-04 06:14:29

标签: ios objective-c swift uiwebview

我遵循了有关如何实施instagram API的教程,并且我坚持了几个部分。在我走的时候把Obj-C转换成Swift,我以为我明白了我的意思,但显然不是。

这部分目前给我一个问题,我不能将这些常数全部放在NSString(format: .. , args: ...)

NSString *fullURL = [NSString stringWithFormat:@"%@%@&redirect_uri= %@&response_type=token",KAUTHURL,KCLIENTID,kREDIRECTURI];

我无法让UIWebView.loadRequest加载fullURL变量

任何帮助将不胜感激!

答案:

import UIKit

class OAuthViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var myWebView: UIWebView?

    var savedToken = ""
    var userDefaults = NSUserDefaults.standardUserDefaults()

    var KAUTHURL: String = "https://api.instagram.com/oauth/authorize/"
    var kAPIURl: String = "https://api.instagram.com/v1/users/"
    var KCLIENTID: String = "02627256292c4083a42e55db3ef21a82"
    var KCLIENTSERCRET: String = "15d0c073b82545d7b000fdf8440bf0c6"
    var KREDIRECTURI: String = "https://www.chahooo.tumblr.com"
    var KSCOPEADD: String = "&scope=relationships+comments"


    override func viewDidLoad() {
        super.viewDidLoad()

        // Loading the Instagram login page for user's to log in
        var fullURL = "\(KAUTHURL)?client_id=\(KCLIENTID)&redirect_uri=\(KREDIRECTURI)&response_type=token"

        var url = NSURL(string: fullURL)

        var request: NSURLRequest = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 40)
        myWebView!.loadRequest(request)

            }

3 个答案:

答案 0 :(得分:0)

var KAUTHURL = "blah blah blah"
var KCLIENTID = "blah blah blah"
var kREDIRECTURI = "Blah blah blah"

var fullURL = String(format:"%s%s&redirect_uri= %s&response_type=token", KAUTHURL, KCLIENTID, kREDIRECTURI)

答案 1 :(得分:0)

最好的方法是使用swift String:

var fullURL = "\(KAUTHURL)\(KCLIENTID)&redirect_uri=\(kREDIRECTURI)&response_type=token"

然后你可以从这个String中获取一个NSURL对象:

var url = NSURL(string: fullURL)

要加载webView,请创建NSURLRequest:

var request: NSURLRequest = NSURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 40)
yourWebView.loadRequest(request)

答案 2 :(得分:-1)

试试这个:

let fullURL = NSString(format: "%@%@&redirect_uri= %@&response_type=token", KAUTHURL,KCLIENTID,kREDIRECTURI)