I'm trying for hours to translate this short code. Objective C :
NSString *urlStr = [request.URL absoluteString];
NSArray *urlParts = [urlStr componentsSeparatedByString:[NSString stringWithFormat:@"%@/", kREDIRECTURI]];
if (urlParts.count > 1)
{
urlStr = urlParts[1];
NSRange token = [urlStr rangeOfString:@"#access_token="];
if (token.location != NSNotFound)
{
vc.access_token = [urlStr substringFromIndex:NSMaxRange(token)];
}
}
What i have tried :
Swift :
var urlParts : NSArray = urlStr!.componentsSeparatedByString("\(kREDIRECTURI)")
if urlParts.count > 1
{
urlStr = urlParts[1] as? String
var token = urlStr!.rangeOfString("#access_token=", options: NSStringCompareOptions.allZeros) as NSRange!
if token != nil
{
var vc = ViewController()
urlStr!.substringFromIndex(NSMaxRange(token))!
}
}
Any idea?
I will assume that the access_token query parameter is at the end and the code follows the string like this 'http:\...#access_token=', the url ends with the code at the end. Here is a simple method that would extract the code out of the url,
For earlier version of Swift, you could use if let patter to unwrap the optional,