Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Commit 74ddea6

Browse files
committed
Fix
1 parent 97c35ff commit 74ddea6

File tree

2 files changed

+42
-167
lines changed

2 files changed

+42
-167
lines changed

Lib/StudyplusSDK/Studyplus.swift

Lines changed: 41 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -26,88 +26,67 @@
2626

2727
import UIKit
2828

29-
/**
30-
The class for using Studyplus.
31-
For example, you can authenticate in Studyplus account, de-authentication, and post study record.
32-
33-
Studyplusの各機能を使うためのクラスです。
34-
Studyplusアカウントとの連携、連携解除、勉強記録の投稿ができます。
35-
*/
3629
final public class Studyplus {
3730

38-
/**
39-
Returns the shared defaults object.
40-
41-
Studyplusのオブジェクトを返します
42-
*/
43-
public static let shared: Studyplus = Studyplus()
31+
private init() {
32+
guard let data = Bundle.main.infoDictionary?["StudyplusSDK"] as? [String: String],
33+
let consumerKey = data["consumerKey"],
34+
let consumerSecret = data["consumerSecret"] else {
35+
assert(false, "StudyplusSDK: *** Please set consumerKey and consumerSecret in your Info.plist. ***")
4436

45-
/**
46-
Consumer Key for Studyplus API.
37+
self.consumerKey = ""
38+
self.consumerSecret = ""
39+
return
40+
}
4741

48-
StudyplusAPI用のConsumer Keyです。
49-
*/
50-
public private(set) var consumerKey: String
42+
if consumerKey == "set_your_consumerKey" || consumerSecret == "set_your_consumerSecret" {
43+
assert(false, "StudyplusSDK: *** Please set consumerKey and consumerSecret in Info.plist. ***")
5144

52-
/**
53-
Consumer Secret for Studyplus API.
45+
self.consumerKey = ""
46+
self.consumerSecret = ""
47+
return
48+
}
5449

55-
StudyplusAPI用のConsumer Secretです。
56-
*/
57-
public private(set) var consumerSecret: String
50+
self.consumerKey = consumerKey
51+
self.consumerSecret = consumerSecret
52+
}
5853

59-
/**
60-
see StudyplusLoginDelegate protocol
61-
*/
54+
public static let shared: Studyplus = Studyplus()
55+
56+
public private(set) var consumerKey: String
57+
public private(set) var consumerSecret: String
6258
public weak var delegate: StudyplusLoginDelegate?
6359

6460
private var serviceName: String {
6561
return "Studyplus_iOS_SDK_\(consumerKey)"
6662
}
6763

68-
/// Opens the login screen by invoking the Studyplus application.
69-
/// If Studyplus app is not installed, open the Studyplus page in AppStore.
70-
/// After the process has returned from Studyplus application, delegate method will be called back.
71-
///
72-
/// Studyplusアプリを起動してStudyplusログイン画面を開きます。
73-
/// Studyplusアプリがインストールされていない場合、AppStore を起動して Studyplus を開きます。
74-
/// Studyplusアプリから操作が戻ってきた後、delegateオブジェクトのコールバックメソッドを呼び出します。
64+
/// Studyplusアプリを経由して、Studyplusアカウントにログインします。
65+
/// Studyplusアプリがインストールされていない場合、AppStoreへ遷移します。
66+
/// ログイン処理の完了後にStudyplusLoginDelegateを呼び出します。
7567
public func login() {
76-
openStudyplus(command: "auth")
68+
let customScheme = URL(string: "studyplus://")!
69+
guard UIApplication.shared.canOpenURL(customScheme) else {
70+
if let store = URL(string: "https://apps.apple.com/jp/app/id505410049?mt=8") {
71+
UIApplication.shared.open(store)
72+
}
73+
74+
return
75+
}
76+
77+
if let url = URL(string: "\(customScheme)external_app/auth/\(consumerKey)/\(consumerSecret)") {
78+
UIApplication.shared.open(url)
79+
}
7780
}
7881

79-
/// Cancels the cooperation with Studyplus application.
80-
///
8182
/// Studyplusアプリとの連携を解除します。
8283
public func logout() {
8384
StudyplusKeychain.deleteAll(serviceName: serviceName)
8485
}
8586

86-
/// Returns to whether or not it is connected with Studyplus application.
87-
///
8887
/// Studyplusアプリと連携されているか否かを返します。
89-
///
90-
/// - Returns: true is connected, false is not connected
9188
public func isConnected() -> Bool {
92-
return self.accessToken() != nil
93-
}
94-
95-
/// Access token of Studyplus API. It is set when the auth or login is successful.
96-
///
97-
/// StudyplusAPIのアクセストークンです。login または authが成功したとき設定されます。
98-
///
99-
/// - Returns: accessToken
100-
public func accessToken() -> String? {
101-
return StudyplusKeychain.accessToken(serviceName: serviceName)
102-
}
103-
104-
/// Username of Studyplus account. It is set when the auth or login is successful.
105-
///
106-
/// Studyplusアカウントのユーザ名です。login または authが成功したとき設定されます。
107-
///
108-
/// - Returns: username
109-
public func username() -> String? {
110-
return StudyplusKeychain.username(serviceName: serviceName)
89+
return StudyplusKeychain.accessToken(serviceName: serviceName) != nil
11190
}
11291

11392
/// Studyplusに学習記録を投稿
@@ -116,7 +95,7 @@ final public class Studyplus {
11695
/// - record: 学習記録
11796
/// - completion: 投稿完了後のコールバック
11897
public func post(_ record: StudyplusRecord, completion: @escaping (Result<Void, StudyplusPostError>) -> Void) {
119-
guard let accessToken = self.accessToken() else {
98+
guard let accessToken = StudyplusKeychain.accessToken(serviceName: serviceName) else {
12099
completion(.failure(.loginRequired))
121100
return
122101
}
@@ -143,21 +122,13 @@ final public class Studyplus {
143122
})
144123
}
145124

146-
/// It is responsible for processing custom URL scheme
147-
/// when it came back from the authorization / login screen of Stuudyplus app.
148-
/// After handling openURL method in AppDelegate, pass the url parameter to this method.
149-
/// If the URL is passed by Studyplus application, calls the callback method of the delegate object.
150-
///
151125
/// Studyplusアプリの認可・ログイン画面から戻ってきた時のカスタムURLスキーム処理を担当します。
152126
/// AppDelegateでopenURLをハンドリングしてから、このメソッドにurlを渡して委譲してください。
153127
/// Studyplusアプリ関連のURLであれば、delegateオブジェクトのコールバックメソッドを呼び出します。
154128
///
155-
/// - Parameter appDelegateUrl:
156-
/// The parameter of AppDelegate#openURL method.
129+
/// - Parameter url:
157130
/// AppDelegate#openURLメソッドで受け取ったurlパラメータをそのまま渡して下さい。
158131
/// - Returns:
159-
/// If the url is supported by StudyplusSDK, returns true.
160-
/// The valid URL has a __[studyplus-{consumerKey}]__ scheme, and right pathComponents and host.
161132
/// 渡されたurlがStudyplusSDKで対応すべきURLであれば true、それ以外は false を返します。
162133
/// __[studyplus-{consumerKey}]__と正しいpathComponentsを持つことを確認してください。
163134
public func handle(_ url: URL) -> Bool {
@@ -172,14 +143,8 @@ final public class Studyplus {
172143
.pathComponents[2]
173144
.trimmingCharacters(in: .whitespacesAndNewlines)
174145
.data(using: .utf8, allowLossyConversion: false)!
175-
let username: Data = url
176-
.pathComponents[3]
177-
.trimmingCharacters(in: .whitespacesAndNewlines)
178-
.data(using: .utf8, allowLossyConversion: false)!
179146

180-
StudyplusKeychain.set(serviceName: serviceName,
181-
accessToken: accessToken,
182-
username: username) { result in
147+
StudyplusKeychain.set(serviceName: serviceName, accessToken: accessToken) { result in
183148
switch result {
184149
case .failure(let error):
185150
self.delegate?.studyplusLoginFail(error: error)
@@ -198,13 +163,6 @@ final public class Studyplus {
198163
return true
199164
}
200165

201-
/// Change the consumer key and secret.
202-
/// Calling this method allows you to switch to another consumer key and secret.
203-
/// For example, you log in to Studyplus, log out, and post a study record.
204-
/// If multiple applications are connected with Studyplus, you need to call this method.
205-
/// If there is only one connected application, you do not need to call this method.
206-
/// If multiple applications are connected with Studyplus, do not forget to set up custom URL schemes.
207-
///
208166
/// StudyplusAPI用のConsumer Key と Secret を変更します。
209167
/// このメソッドを呼ぶとStudyplusにログイン、ログアウト、勉強記録を投稿するときなどに、別の Consumer Key と Secret に切り替えることができます。
210168
/// 複数のアプリケーションがStudyplusと連携している場合は、このメソッドを呼ぶ必要があります。
@@ -219,61 +177,6 @@ final public class Studyplus {
219177
self.consumerSecret = consumerSecret
220178
}
221179

222-
// MARK: - private method
223-
224-
private init() {
225-
guard let data = Bundle.main.infoDictionary?["StudyplusSDK"] as? [String: String],
226-
let consumerKey = data["consumerKey"],
227-
let consumerSecret = data["consumerSecret"] else {
228-
229-
assert(false, "StudyplusSDK: *** Please set consumerKey and consumerSecret in your Info.plist. ***")
230-
print("StudyplusSDK: *** Please set consumerKey and consumerSecret in your Info.plist. ***")
231-
232-
self.consumerKey = ""
233-
self.consumerSecret = ""
234-
return
235-
}
236-
237-
if consumerKey == "set_your_consumerKey" || consumerSecret == "set_your_consumerSecret" {
238-
239-
assert(false, "StudyplusSDK: *** Please set consumerKey and consumerSecret in Info.plist. ***")
240-
print("StudyplusSDK: *** Please set consumerKey and consumerSecret in your Info.plist. ***")
241-
242-
self.consumerKey = ""
243-
self.consumerSecret = ""
244-
return
245-
}
246-
247-
self.consumerKey = consumerKey
248-
self.consumerSecret = consumerSecret
249-
}
250-
251-
private func openStudyplus(command: String) {
252-
253-
guard UIApplication.shared.canOpenURL(URL(string: "studyplus://")!) else {
254-
let appStoreURLString: String = "https://apps.apple.com/jp/app/id505410049?mt=8"
255-
guard let appStoreURL = URL(string: appStoreURLString) else { return }
256-
applicationOpen(appStoreURL)
257-
return
258-
}
259-
260-
let urlString: String = "studyplus://external_app/" + command + "/" + consumerKey + "/" + consumerSecret
261-
262-
if let url = URL(string: urlString) {
263-
264-
applicationOpen(url)
265-
}
266-
}
267-
268-
private func applicationOpen(_ url: URL) {
269-
270-
if #available(iOS 10.0, *) {
271-
UIApplication.shared.open(url)
272-
} else {
273-
_ = UIApplication.shared.openURL(url)
274-
}
275-
}
276-
277180
private func isAcceptableURL(url: URL) -> Bool {
278181
guard let host = url.host else { return false }
279182
guard host == "auth-result" || host == "login-result" else { return false }

Lib/StudyplusSDK/internal/StudyplusKeychain.swift

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import Foundation
2828

2929
internal struct StudyplusKeychain {
3030
private static let accessTokenStoreKey: String = "accessToken"
31-
private static let usernameStoreKey: String = "username"
3231

3332
static internal func accessToken(serviceName: String) -> String? {
3433
let query = [
@@ -49,28 +48,8 @@ internal struct StudyplusKeychain {
4948
return String(data: data, encoding: .utf8)
5049
}
5150

52-
static internal func username(serviceName: String) -> String? {
53-
let query = [
54-
kSecClass: kSecClassGenericPassword,
55-
kSecAttrService: serviceName,
56-
kSecAttrSynchronizable: kSecAttrSynchronizableAny,
57-
kSecMatchLimit: kSecMatchLimitOne,
58-
kSecReturnData: true,
59-
kSecAttrAccount: usernameStoreKey
60-
] as CFDictionary
61-
62-
var item: CFTypeRef?
63-
let status = SecItemCopyMatching(query, &item)
64-
guard status == errSecSuccess, let data = item as? Data else {
65-
return nil
66-
}
67-
68-
return String(data: data, encoding: .utf8)
69-
}
70-
7151
static internal func set(serviceName: String,
7252
accessToken: Data,
73-
username: Data,
7453
completion: (Result<Void, StudyplusLoginError>) -> Void) {
7554
// delete previous keys
7655
deleteAll(serviceName: serviceName)
@@ -83,15 +62,8 @@ internal struct StudyplusKeychain {
8362
kSecAttrAccount: accessTokenStoreKey,
8463
kSecValueData: accessToken
8564
] as CFDictionary, nil)
86-
let statusUsername = SecItemAdd([
87-
kSecClass: kSecClassGenericPassword,
88-
kSecAttrService: serviceName,
89-
kSecAttrSynchronizable: kSecAttrSynchronizableAny,
90-
kSecAttrAccount: usernameStoreKey,
91-
kSecValueData: username
92-
] as CFDictionary, nil)
9365

94-
if statusAccessToken != noErr || statusUsername != noErr {
66+
if statusAccessToken != noErr {
9567
completion(.failure(.keychainError))
9668
return
9769
}

0 commit comments

Comments
 (0)