This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathConfig.swift
75 lines (66 loc) · 2.11 KB
/
Config.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Config.swift
// reddift
//
// Created by sonson on 2015/04/13.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import Foundation
/**
Class to manage parameters of reddift.
This class is used as singleton model
*/
struct Config {
/// Application verison, be updated by Info.plist later.
let version: String
/// Bundle identifier, be updated by Info.plist later.
let bundleIdentifier: String
/// Developer's reddit user name
let developerName: String
/// OAuth redirect URL you register
let redirectURI: String
/// Application ID
let clientID: String
/**
Singleton model.
*/
static let sharedInstance = Config()
/**
Returns User-Agent for API
*/
var userAgent: String {
return "ios:" + bundleIdentifier + ":v" + version + "(by /u/" + developerName + ")"
}
/**
Returns scheme of redirect URI.
*/
var redirectURIScheme: String {
if let scheme = URL(string: redirectURI)?.scheme {
return scheme
} else {
return ""
}
}
init() {
version = Bundle.infoValueInMainBundle(for: "CFBundleShortVersionString") as? String ?? "1.0"
bundleIdentifier = Bundle.infoValueInMainBundle(for: "CFBundleIdentifier") as? String ?? ""
var _developerName: String? = nil
var _redirectURI: String? = nil
var _clientID: String? = nil
if let path = Bundle.main.path(forResource: "reddift_config", ofType: "json") {
if let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
do {
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? JSONDictionary {
_developerName = json["DeveloperName"] as? String
_redirectURI = json["redirect_uri"] as? String
_clientID = json["client_id"] as? String
}
} catch {
}
}
}
developerName = _developerName ?? ""
redirectURI = _redirectURI ?? ""
clientID = _clientID ?? ""
}
}