-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJmAd.swift
419 lines (350 loc) · 16.8 KB
/
JmAd.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// JmAd.swift
//
// Copyright ROI-App
// AlamoFire 5
//
import Foundation
import Alamofire
import SDWebImage
import SafariServices
enum jmadEventType: String {
case print = "print"
case hit = "hit"
}
enum jmadEventOrientation: String {
case portrait = "portrait"
case landscape = "landscape"
}
class JmAd
{
typealias CompletionHandler = (Bool) -> Void
static let instance = JmAd()
private var appID = ""
private var adView: UIView!
private var sponsorlabel:UILabel!
private var mytimer: Foundation.Timer!
private var adDuration: Int!
private var adObject: jm_Ad!
private var orientation: jmadEventOrientation!
private var adcomplete: CompletionHandler!
private var containerVC: UIViewController!
private var adTapped: Bool!
struct jm_Ad: Codable {
let id: Int
let ad_id: String
let type: String
let format: String
let duration: Int
let app_id: Int
let filename: String
let store_url: String
}
public func initSDK(_ appid:String)
{
self.appID = appid
self.orientation = .portrait
self.adTapped = false
}
public func callInventory(completionHandler:@escaping (Bool) -> Void)
{
if self.appID == "" {
completionHandler(false)
return
}
let URL: String = "https://roi-app.com/api/ad/inventory/" + self.appID
print("API jma inventory: \(URL)")
AF.request(URL, method: .get).responseData { response in
switch response.result {
case .success(let value):
do {
//let data = try Data(contentsOf: testUrl)
self.adObject = try JSONDecoder().decode(jm_Ad.self, from: value)
completionHandler(true)
}
catch {
print(error)
completionHandler(false)
}
case .failure(let error):
print(error)
completionHandler(false)
}
}
}
public func loadAd(completionHandler:@escaping (Bool) -> Void)
{
if self.adTapped == true {
completionHandler(false)
return
}
completionHandler(true)
/*
let url: String = "https://roi-app.com/api/v1/fr/1.0/jma/show/" + self.adObject.ad_id
print("API jma loadAd: \(url)")
let documentsPath = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let downloadFilePath = documentsPath.appendingPathComponent(self.adObject.filename)
print("downloadFilePath= " + documentsPath.absoluteString)
let destination: DownloadRequest.Destination = { _, _ in
return (downloadFilePath, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(
url,
method: .get,
encoding: JSONEncoding.default,
headers: nil,
to: destination
).downloadProgress(closure: { (progress) in
print("progress : \(progress.fractionCompleted)")
}).response(completionHandler: { (DefaultDownloadResponse) in
completionHandler(true)
})*/
}
public func showAd(_ vcont:UIViewController, _ completionHandler: @escaping CompletionHandler)
{
containerVC = vcont
adcomplete = completionHandler
/*if self.adObject.format == jmadEventOrientation.landscape.rawValue {
self.orientation = jmadEventOrientation.landscape
}*/
self.orientation = self.adObject.format == jmadEventOrientation.portrait.rawValue ? jmadEventOrientation.portrait : jmadEventOrientation.landscape
if let appOrientation = vcont.view.window?.windowScene?.interfaceOrientation {
//self.orientation = (appOrientation.isPortrait == true) ? .portrait : .landscape
}
if adView != nil {
adcomplete(false)
adcomplete = nil
return
//hideAd()
}
adView = UIView(frame: vcont.view.frame)
adView.backgroundColor = .white // vcont.view.backgroundColor != .clear ? vcont.view.backgroundColor : .white
vcont.view.addSubview(adView)
/*
let documentsPath = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let downloadFilePath = documentsPath.appendingPathComponent(self.adObject.filename)
print("API jma showAd= " + downloadFilePath.path)
guard let img = UIImage(contentsOfFile: downloadFilePath.path) else { return }
if adView != nil {
hideAd()
}
adView = UIView(frame: vcont.view.frame)
adView.backgroundColor = .white // vcont.view.backgroundColor != .clear ? vcont.view.backgroundColor : .white
vcont.view.addSubview(adView)
*/
if self.orientation == .landscape {
//adView.transform = adView.transform.rotated(by:(-90.0 * CGFloat(Double.pi / 180)))
}
let bgAdView = UIView(frame: .zero)
bgAdView.backgroundColor = .white // .clear
bgAdView.translatesAutoresizingMaskIntoConstraints = false
adView.addSubview(bgAdView)
NSLayoutConstraint.activate([
bgAdView.topAnchor.constraint(equalTo: vcont.view.safeAreaLayoutGuide.topAnchor),
bgAdView.leftAnchor.constraint(equalTo: vcont.view.safeAreaLayoutGuide.leftAnchor),
bgAdView.rightAnchor.constraint(equalTo: vcont.view.safeAreaLayoutGuide.rightAnchor),
bgAdView.bottomAnchor.constraint(equalTo: vcont.view.safeAreaLayoutGuide.bottomAnchor),
])
/*
let imageView = UIImageView(image: img)
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
bgAdView.addSubview(imageView)
*/
let url = URL(string: self.adObject.filename)
SDWebImageManager.shared.loadImage(with: url, options: .highPriority, progress: nil, completed: { [weak self] (image, data, error, cacheType, finished, url) in
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
bgAdView.addSubview(imageView)
if self?.orientation == .landscape {
imageView.transform = imageView.transform.rotated(by:(-90.0 * CGFloat(Double.pi / 180)))
}
//center image
if self?.orientation == .landscape {
let centerXConst = NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: bgAdView, attribute: .centerY, multiplier: 1.0, constant: 0.0)
let centerYConst = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: bgAdView, attribute: .centerX, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([centerXConst, centerYConst])
}
else {
let centerXConst = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: bgAdView, attribute: .centerX, multiplier: 1.0, constant: 0.0)
let centerYConst = NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: bgAdView, attribute: .centerY, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([centerXConst, centerYConst])
}
if self?.orientation == .landscape {
let heightConstraint = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.height)
let widthConstraint = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.width)
imageView.addConstraints([heightConstraint, widthConstraint])
}
else {
let heightConstraint = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.height)
let widthConstraint = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.width)
imageView.addConstraints([heightConstraint, widthConstraint])
}
})
/*
if self.orientation == .landscape {
bgAdView.transform = bgAdView.transform.rotated(by:(-90.0 * CGFloat(Double.pi / 180)))
}
//center image
if self.orientation == .landscape {
let centerXConst = NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: bgAdView, attribute: .centerY, multiplier: 1.0, constant: 0.0)
let centerYConst = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: bgAdView, attribute: .centerX, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([centerXConst, centerYConst])
}
else {
let centerXConst = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: bgAdView, attribute: .centerX, multiplier: 1.0, constant: 0.0)
let centerYConst = NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: bgAdView, attribute: .centerY, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([centerXConst, centerYConst])
}
if self.orientation == .landscape {
let heightConstraint = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.height)
let widthConstraint = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.width)
imageView.addConstraints([heightConstraint, widthConstraint])
}
else {
let heightConstraint = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.height)
let widthConstraint = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: vcont.view.safeAreaLayoutGuide.layoutFrame.width)
imageView.addConstraints([heightConstraint, widthConstraint])
}
*/
adDuration = self.adObject.duration
if self.orientation == .landscape {
sponsorlabel = UILabel(frame: CGRect(x: 10+190, y: -12-170, width: vcont.view.safeAreaLayoutGuide.layoutFrame.width, height: 14))
}
else {
sponsorlabel = UILabel(frame: CGRect(x: 10, y: -12, width: vcont.view.safeAreaLayoutGuide.layoutFrame.width, height: 14))
}
sponsorlabel.textColor = .black
//sponsorlabel.backgroundColor = .green
sponsorlabel.font = UIFont.boldSystemFont(ofSize: 14)
sponsorlabel.adjustsFontForContentSizeCategory = true
sponsorlabel.layer.shadowOffset = CGSize(width: 1, height: 1)
sponsorlabel.layer.shadowOpacity = 1//0.8
sponsorlabel.layer.shadowRadius = 1
sponsorlabel.layer.shadowColor = CGColor.init(srgbRed: 1, green: 1, blue: 1, alpha: 1)
bgAdView.addSubview(sponsorlabel)
updateSponsorLabel()
if (mytimer != nil) && mytimer.isValid {
mytimer.invalidate()
}
mytimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
// Initialize Tap Gesture Recognizer
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapImageView(_:)))
adView.addGestureRecognizer(tapGestureRecognizer)
sendStats(.print, completionHandler: { response in
})
}
@objc func fireTimer()
{
adDuration = adDuration - 1
if adDuration == 0 {
hideAd()
adcomplete(true)
adcomplete = nil
containerVC = nil
return
}
updateSponsorLabel()
}
private func updateSponsorLabel()
{
var stext = "Sponsor.. "
if adDuration > 0 {
stext += "\(adDuration!)"
}
sponsorlabel.text = stext
}
public func hideAd()
{
mytimer?.invalidate()
if adView != nil {
adView.removeFromSuperview()
adView = nil
}
self.adTapped = false
}
private func sendStats(_ event: jmadEventType, completionHandler:@escaping (Bool) -> Void)
{
if self.adObject == nil {
completionHandler(false)
return
}
let URL: String = "https://roi-app.com/api/ad/stats"
print("API jma stats: \(URL)")
/*let callheaders: HTTPHeaders = [
.accept("application/json"),
.contentType("application/json")
]*/
let params = ["ad_id": self.adObject.ad_id,
"app_id_src": self.appID,
"app_id_dst": self.adObject.app_id,
"event": event.rawValue] as [String : Any]
print(params)
AF.request(URL, method: .post,
parameters: params/*,
headers: callheaders*/
).responseString { response in
//print("response: \(response)")
switch response.result {
case .success(let value):
print("value**: \(value)")
completionHandler(true)
case .failure(let error):
print(error)
completionHandler(false)
}
}
}
@IBAction func didTapImageView(_ sender: UITapGestureRecognizer)
{
self.adTapped = true
print("did tap image view", sender)
sendStats(.hit, completionHandler: { response in
})
if let url = URL(string: self.adObject.store_url) {
let safariViewController = SFSafariViewController(url: url)
safariViewController.modalPresentationStyle = .overFullScreen // To ensure it doesn't cause the app to enter background
self.containerVC.present(safariViewController, animated: true, completion: nil)
}
}
func imageRotatedByDegrees(oldImage: UIImage,degrees: CGFloat) -> UIImage
{
let size = oldImage.size
UIGraphicsBeginImageContext(size)
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
//Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap.translateBy(x: size.width / 2, y: size.height / 2)
//Rotate the image context
bitmap.rotate(by: (degrees * CGFloat(Double.pi / 180)))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
let origin = CGPoint(x: -size.width / 2, y: -size.width / 2)
bitmap.draw(oldImage.cgImage!, in: CGRect(origin: origin, size: size))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
/*
extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func downloadedFrom(link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}
}
*/