26
26
27
27
import UIKit
28
28
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
- */
36
29
final public class Studyplus {
37
30
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. *** " )
44
36
45
- /**
46
- Consumer Key for Studyplus API.
37
+ self . consumerKey = " "
38
+ self . consumerSecret = " "
39
+ return
40
+ }
47
41
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. *** " )
51
44
52
- /**
53
- Consumer Secret for Studyplus API.
45
+ self . consumerKey = " "
46
+ self . consumerSecret = " "
47
+ return
48
+ }
54
49
55
- StudyplusAPI用のConsumer Secretです。
56
- */
57
- public private ( set ) var consumerSecret : String
50
+ self . consumerKey = consumerKey
51
+ self . consumerSecret = consumerSecret
52
+ }
58
53
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
62
58
public weak var delegate : StudyplusLoginDelegate ?
63
59
64
60
private var serviceName : String {
65
61
return " Studyplus_iOS_SDK_ \( consumerKey) "
66
62
}
67
63
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を呼び出します。
75
67
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
+ }
77
80
}
78
81
79
- /// Cancels the cooperation with Studyplus application.
80
- ///
81
82
/// Studyplusアプリとの連携を解除します。
82
83
public func logout( ) {
83
84
StudyplusKeychain . deleteAll ( serviceName: serviceName)
84
85
}
85
86
86
- /// Returns to whether or not it is connected with Studyplus application.
87
- ///
88
87
/// Studyplusアプリと連携されているか否かを返します。
89
- ///
90
- /// - Returns: true is connected, false is not connected
91
88
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
111
90
}
112
91
113
92
/// Studyplusに学習記録を投稿
@@ -116,7 +95,7 @@ final public class Studyplus {
116
95
/// - record: 学習記録
117
96
/// - completion: 投稿完了後のコールバック
118
97
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 {
120
99
completion ( . failure( . loginRequired) )
121
100
return
122
101
}
@@ -143,21 +122,13 @@ final public class Studyplus {
143
122
} )
144
123
}
145
124
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
- ///
151
125
/// Studyplusアプリの認可・ログイン画面から戻ってきた時のカスタムURLスキーム処理を担当します。
152
126
/// AppDelegateでopenURLをハンドリングしてから、このメソッドにurlを渡して委譲してください。
153
127
/// Studyplusアプリ関連のURLであれば、delegateオブジェクトのコールバックメソッドを呼び出します。
154
128
///
155
- /// - Parameter appDelegateUrl:
156
- /// The parameter of AppDelegate#openURL method.
129
+ /// - Parameter url:
157
130
/// AppDelegate#openURLメソッドで受け取ったurlパラメータをそのまま渡して下さい。
158
131
/// - 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.
161
132
/// 渡されたurlがStudyplusSDKで対応すべきURLであれば true、それ以外は false を返します。
162
133
/// __[studyplus-{consumerKey}]__と正しいpathComponentsを持つことを確認してください。
163
134
public func handle( _ url: URL ) -> Bool {
@@ -172,14 +143,8 @@ final public class Studyplus {
172
143
. pathComponents [ 2 ]
173
144
. trimmingCharacters ( in: . whitespacesAndNewlines)
174
145
. data ( using: . utf8, allowLossyConversion: false ) !
175
- let username : Data = url
176
- . pathComponents [ 3 ]
177
- . trimmingCharacters ( in: . whitespacesAndNewlines)
178
- . data ( using: . utf8, allowLossyConversion: false ) !
179
146
180
- StudyplusKeychain . set ( serviceName: serviceName,
181
- accessToken: accessToken,
182
- username: username) { result in
147
+ StudyplusKeychain . set ( serviceName: serviceName, accessToken: accessToken) { result in
183
148
switch result {
184
149
case . failure( let error) :
185
150
self . delegate? . studyplusLoginFail ( error: error)
@@ -198,13 +163,6 @@ final public class Studyplus {
198
163
return true
199
164
}
200
165
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
- ///
208
166
/// StudyplusAPI用のConsumer Key と Secret を変更します。
209
167
/// このメソッドを呼ぶとStudyplusにログイン、ログアウト、勉強記録を投稿するときなどに、別の Consumer Key と Secret に切り替えることができます。
210
168
/// 複数のアプリケーションがStudyplusと連携している場合は、このメソッドを呼ぶ必要があります。
@@ -219,61 +177,6 @@ final public class Studyplus {
219
177
self . consumerSecret = consumerSecret
220
178
}
221
179
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
-
277
180
private func isAcceptableURL( url: URL ) -> Bool {
278
181
guard let host = url. host else { return false }
279
182
guard host == " auth-result " || host == " login-result " else { return false }
0 commit comments