|
| 1 | +#import "PopupViewController.h" |
| 2 | + |
| 3 | +@implementation PopupViewController |
| 4 | + |
| 5 | +- (void)viewDidLoad { |
| 6 | + [super viewDidLoad]; |
| 7 | + [self setupUI]; |
| 8 | +} |
| 9 | + |
| 10 | +- (void)setupUI { |
| 11 | + NSDictionary *popup = self.popupData; |
| 12 | + if (!popup) return; |
| 13 | + |
| 14 | + NSString *title = popup[@"title"]; |
| 15 | + NSString *description = popup[@"description"]; |
| 16 | + NSString *image = popup[@"image"]; |
| 17 | + NSString *video = popup[@"video"]; |
| 18 | + NSString *size = popup[@"size"]; |
| 19 | + NSString *trackingPixelUrl = popup[@"tracking_pixel_url"]; |
| 20 | + |
| 21 | + // Send tracking pixel |
| 22 | + if (trackingPixelUrl) { |
| 23 | + NSURL *url = [NSURL URLWithString:trackingPixelUrl]; |
| 24 | + if (url) { |
| 25 | + [[[NSURLSession sharedSession] dataTaskWithURL:url] resume]; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7]; |
| 30 | + |
| 31 | + // Container view |
| 32 | + self.containerView = [[UIView alloc] init]; |
| 33 | + self.containerView.backgroundColor = [UIColor clearColor]; |
| 34 | + self.containerView.layer.cornerRadius = 15; |
| 35 | + self.containerView.clipsToBounds = YES; |
| 36 | + self.containerView.translatesAutoresizingMaskIntoConstraints = NO; |
| 37 | + [self.view addSubview:self.containerView]; |
| 38 | + |
| 39 | + if ([size isEqualToString:@"large"]) { |
| 40 | + [NSLayoutConstraint activateConstraints:@[ |
| 41 | + [self.containerView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:0.8], |
| 42 | + [self.containerView.heightAnchor constraintEqualToAnchor:self.view.heightAnchor multiplier:0.6], |
| 43 | + [self.containerView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], |
| 44 | + [self.containerView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor constant:-10] |
| 45 | + ]]; |
| 46 | + } else { |
| 47 | + [NSLayoutConstraint activateConstraints:@[ |
| 48 | + [self.containerView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| 49 | + [self.containerView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| 50 | + [self.containerView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
| 51 | + [self.containerView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor] |
| 52 | + ]]; |
| 53 | + } |
| 54 | + |
| 55 | + // Description label |
| 56 | + UILabel *descriptionLabel = [[UILabel alloc] init]; |
| 57 | + descriptionLabel.text = description; |
| 58 | + descriptionLabel.textColor = [UIColor whiteColor]; |
| 59 | + descriptionLabel.font = [UIFont systemFontOfSize:16]; |
| 60 | + descriptionLabel.numberOfLines = 0; |
| 61 | + descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 62 | + [self.view addSubview:descriptionLabel]; |
| 63 | + |
| 64 | + if ([size isEqualToString:@"large"]) { |
| 65 | + [NSLayoutConstraint activateConstraints:@[ |
| 66 | + [descriptionLabel.topAnchor constraintEqualToAnchor:self.containerView.bottomAnchor constant:10], |
| 67 | + [descriptionLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:20], |
| 68 | + [descriptionLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-20] |
| 69 | + ]]; |
| 70 | + } else { |
| 71 | + [NSLayoutConstraint activateConstraints:@[ |
| 72 | + [descriptionLabel.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-70], |
| 73 | + [descriptionLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:20], |
| 74 | + [descriptionLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-20] |
| 75 | + ]]; |
| 76 | + } |
| 77 | + |
| 78 | + // Title label |
| 79 | + UILabel *titleLabel = [[UILabel alloc] init]; |
| 80 | + titleLabel.text = title; |
| 81 | + titleLabel.textColor = [UIColor whiteColor]; |
| 82 | + titleLabel.font = [UIFont boldSystemFontOfSize:20]; |
| 83 | + titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 84 | + [self.view addSubview:titleLabel]; |
| 85 | + |
| 86 | + if ([size isEqualToString:@"large"]) { |
| 87 | + [NSLayoutConstraint activateConstraints:@[ |
| 88 | + [titleLabel.bottomAnchor constraintEqualToAnchor:self.containerView.topAnchor constant:-10], |
| 89 | + [titleLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:20], |
| 90 | + [titleLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-20] |
| 91 | + ]]; |
| 92 | + } else { |
| 93 | + [NSLayoutConstraint activateConstraints:@[ |
| 94 | + [titleLabel.bottomAnchor constraintEqualToAnchor:descriptionLabel.topAnchor constant:-10], |
| 95 | + [titleLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:20], |
| 96 | + [titleLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-20] |
| 97 | + ]]; |
| 98 | + } |
| 99 | + |
| 100 | + // Image or Video |
| 101 | + if (image != nil && ![image isKindOfClass:[NSNull class]] && ![image isEqualToString:@""]) { |
| 102 | + NSURL *imageUrl = [NSURL URLWithString:image]; |
| 103 | + if (imageUrl && imageUrl.scheme && imageUrl.host) { |
| 104 | + UIImageView *imageView = [[UIImageView alloc] init]; |
| 105 | + imageView.contentMode = UIViewContentModeScaleAspectFit; |
| 106 | + imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 107 | + |
| 108 | + |
| 109 | + [self.containerView addSubview:imageView]; |
| 110 | + [NSLayoutConstraint activateConstraints:@[ |
| 111 | + [imageView.widthAnchor constraintEqualToAnchor:self.containerView.widthAnchor], |
| 112 | + [imageView.heightAnchor constraintEqualToAnchor:self.containerView.heightAnchor], |
| 113 | + [imageView.centerXAnchor constraintEqualToAnchor:self.containerView.centerXAnchor], |
| 114 | + [imageView.centerYAnchor constraintEqualToAnchor:self.containerView.centerYAnchor] |
| 115 | + ]]; |
| 116 | + |
| 117 | + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 118 | + NSData *data = [NSData dataWithContentsOfURL:imageUrl]; |
| 119 | + UIImage *image = [UIImage imageWithData:data]; |
| 120 | + |
| 121 | + NSLog(@"Image data length: %lu", (unsigned long)data.length); |
| 122 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 123 | + imageView.image = image; |
| 124 | + CGSize size = imageView.frame.size; |
| 125 | + NSLog(@"Image size: %f x %f", size.width, size.height); |
| 126 | + }); |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (video != nil && ![video isKindOfClass:[NSNull class]] && ![video isEqualToString:@""]) { |
| 132 | + NSURL *videoUrl = [NSURL URLWithString:video]; |
| 133 | + if (videoUrl) { |
| 134 | + self.videoPlayer = [AVPlayer playerWithURL:videoUrl]; |
| 135 | + self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer]; |
| 136 | + self.playerLayer.frame = CGRectZero; |
| 137 | + [self.containerView.layer addSublayer:self.playerLayer]; |
| 138 | + [self.videoPlayer play]; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Close button |
| 143 | + UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeSystem]; |
| 144 | + closeButton.backgroundColor = [UIColor whiteColor]; |
| 145 | + closeButton.layer.cornerRadius = 20; |
| 146 | + UIImage *icon = nil; |
| 147 | + |
| 148 | + if (@available(iOS 13.0, *)) { |
| 149 | + icon = [UIImage systemImageNamed:@"xmark"]; |
| 150 | + } else { |
| 151 | + icon = [UIImage imageNamed:@"ic_close"]; |
| 152 | + } |
| 153 | + |
| 154 | + [closeButton setImage:icon forState:UIControlStateNormal]; |
| 155 | + closeButton.tintColor = [UIColor blackColor]; |
| 156 | + closeButton.translatesAutoresizingMaskIntoConstraints = NO; |
| 157 | + [closeButton addTarget:self action:@selector(closePopup) forControlEvents:UIControlEventTouchUpInside]; |
| 158 | + [self.view addSubview:closeButton]; |
| 159 | + |
| 160 | + [NSLayoutConstraint activateConstraints:@[ |
| 161 | + [closeButton.heightAnchor constraintEqualToConstant:40], |
| 162 | + [closeButton.widthAnchor constraintEqualToConstant:40], |
| 163 | + [closeButton.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20], |
| 164 | + [closeButton.trailingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor constant:-20] |
| 165 | + ]]; |
| 166 | +} |
| 167 | + |
| 168 | +- (void)viewDidLayoutSubviews { |
| 169 | + [super viewDidLayoutSubviews]; |
| 170 | + self.playerLayer.frame = self.containerView.bounds; |
| 171 | +} |
| 172 | + |
| 173 | +- (void)closePopup { |
| 174 | + [UIView animateWithDuration:0.3 animations:^{ |
| 175 | + self.view.alpha = 0; |
| 176 | + } completion:^(BOOL finished) { |
| 177 | + // if (self.result) { |
| 178 | + // self.result(nil); |
| 179 | + // } |
| 180 | + [self dismissViewControllerAnimated:NO completion:nil]; |
| 181 | + }]; |
| 182 | +} |
| 183 | + |
| 184 | +@end |
0 commit comments