Skip to content

Commit

Permalink
relative url fix for AsakusaSatellite on a sub directory such like ht…
Browse files Browse the repository at this point in the history
  • Loading branch information
banjun committed Mar 31, 2015
1 parent 1509abf commit 1899842
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
2 changes: 1 addition & 1 deletion AsakusaSatellite.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Classes/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
2 changes: 1 addition & 1 deletion Classes/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 8 additions & 24 deletions Classes/ios/TwitterAuthViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 1899842

Please sign in to comment.