From 1899842e256c5709431c5169abacf1d4da5a0ed1 Mon Sep 17 00:00:00 2001 From: banjun Date: Tue, 31 Mar 2015 20:11:27 +0900 Subject: [PATCH] relative url fix for AsakusaSatellite on a sub directory such like http://example.com/asakusasatellite --- AsakusaSatellite.podspec | 2 +- Classes/Client.swift | 4 +-- Classes/Endpoint.swift | 2 +- Classes/ios/TwitterAuthViewController.swift | 32 ++++++--------------- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/AsakusaSatellite.podspec b/AsakusaSatellite.podspec index 55b5931..7750aae 100644 --- a/AsakusaSatellite.podspec +++ b/AsakusaSatellite.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "AsakusaSatellite" - s.version = "0.2.0" + s.version = "0.2.1" s.summary = "AsakusaSatellite API Client for Swift" s.description = <<-DESC AsakusaSatellite is a realtime chat application for developers. diff --git a/Classes/Client.swift b/Classes/Client.swift index 06ebeeb..fa1d5cf 100644 --- a/Classes/Client.swift +++ b/Classes/Client.swift @@ -13,11 +13,11 @@ import SwiftyJSON public class Client { public let rootURL: String - var apiBaseURL: String { return "\(rootURL)/api/v1" } + var apiBaseURL: String { return rootURL.stringByAppendingFormat("api/v1") } let apiKey: String? public convenience init(apiKey: String?) { - self.init(rootURL: "https://asakusa-satellite.herokuapp.com", apiKey: apiKey) + self.init(rootURL: "https://asakusa-satellite.herokuapp.com/", apiKey: apiKey) } public init(rootURL: String, apiKey: String?) { diff --git a/Classes/Endpoint.swift b/Classes/Endpoint.swift index f12bdb4..1e1e8c8 100644 --- a/Classes/Endpoint.swift +++ b/Classes/Endpoint.swift @@ -78,7 +78,7 @@ public enum Endpoint { static func URLRequest(baseURL: String, method: Alamofire.Method, path: String, parameters: [String: AnyObject]?, body: NSData?) -> NSURLRequest { let urlRequestWithParams: NSURLRequest = { - let getRequest = NSMutableURLRequest(URL: NSURL(string: baseURL + path)!) + let getRequest = NSMutableURLRequest(URL: NSURL(string: baseURL.stringByAppendingPathComponent(path))!) getRequest.HTTPMethod = Method.GET.rawValue let (request, error) = Alamofire.ParameterEncoding.URL.encode(getRequest, parameters: parameters) // Alamofire encode params into body when POST if let e = error { diff --git a/Classes/ios/TwitterAuthViewController.swift b/Classes/ios/TwitterAuthViewController.swift index 17a23b3..1c20810 100644 --- a/Classes/ios/TwitterAuthViewController.swift +++ b/Classes/ios/TwitterAuthViewController.swift @@ -10,13 +10,11 @@ import Foundation import UIKit -private let kAuthTwitterPath = "/auth/twitter" -private let kAccountPath = "/account" - - public class TwitterAuthViewController: UIViewController, UIWebViewDelegate { let webview = UIWebView(frame: CGRectZero) let rootURL: NSURL + var authTwitterURL: NSURL { return NSURL(string: "auth/twitter", relativeToURL: rootURL)! } + var accountURL: NSURL { return NSURL(string: "account", relativeToURL: rootURL)! } let completion: (String? -> Void) // MARK: init @@ -41,31 +39,17 @@ public class TwitterAuthViewController: UIViewController, UIWebViewDelegate { webview.delegate = self view.addSubview(webview) - if let url = NSURL(string: kAuthTwitterPath, relativeToURL: rootURL) { - // load /auth/twitter with referer /account - // oauth callback redirects to referer - let request = NSMutableURLRequest(URL: url) - request.setValue(kAccountPath, forHTTPHeaderField: "Referer") - webview.loadRequest(request) - } else { - let ac = UIAlertController( - title: NSLocalizedString("Cannot Load", comment: ""), - message: NSLocalizedString("Invalid URL: ", comment: "") + "\(rootURL)", - preferredStyle: .Alert) - ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: { _ in - ac.dismissViewControllerAnimated(true, completion: nil) - })) - self.presentViewController(ac, animated: true, completion: nil) - } + // load /auth/twitter with referer /account + // oauth callback redirects to referer + let request = NSMutableURLRequest(URL: authTwitterURL) + request.setValue(accountURL.absoluteString, forHTTPHeaderField: "Referer") + webview.loadRequest(request) } // MARK: UIWebViewDelegate private func isRedirectedBackToAsakusaSatellite(request: NSURLRequest) -> Bool { - let reqURLString = request.URL.absoluteString - let rootURLString = rootURL.absoluteString! - - return reqURLString?.hasPrefix(rootURLString) == true && request.URL.path == kAccountPath + return request.URL.absoluteString == accountURL.absoluteString } public func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {