WLNativeAdTableViewCell.m 2.88 KB
//
//  WLNativeAdTableViewCell.m
//  DemoApp
//
//  Created by Nick Xirotyris on 14/10/15.
//  Copyright © 2015 YC. All rights reserved.
//

#import "WLNativeAdTableViewCell.h"
#import "Warply.h"
#import <WebKit/WebKit.h>
#import "UIImageView+AFNetworking.h"

//#import "WLGlobals.h"

@implementation WLNativeAdTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.webView.UIDelegate = self;
    self.adImageView.clipsToBounds = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)setupCellWithItem:(WLInboxItem *)item {
    self.item = item;
    self.adLabel.text = item.title;
    self.webView.alpha = 0;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[item getImageURL]]];
    
    [self.activityIndicator startAnimating];
    
    [self.adImageView setImageWithURLRequest:request
                               placeholderImage:nil
                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                            
                                            [self.activityIndicator stopAnimating];
                                            self.adImageView.image = image;
                                        }
                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                            [self.activityIndicator stopAnimating];
                                        }];
}


-(void)loadContentwithURL:(NSString *)url {
    
    self.adLabel.alpha = 0;
    self.adImageView.alpha = 0;
    
    NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    [request setValue:[Warply sharedService].webId forHTTPHeaderField:@"loyalty-web-id"];
    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    [request setValue:@"gzip" forHTTPHeaderField:@"User-Agent"];
    [self.webView loadRequest:request];
    [self.activityIndicator startAnimating];
}


#pragma mark - WebView Delegate


-(BOOL)webView:(WKWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    if (![request.URL.host isEqualToString:[Warply sharedService].host] && ![request.URL.host isEqualToString:@"warplydata.blob.core.windows.net"]) {
        [[UIApplication sharedApplication] openURL:request.URL];
    }
    
    return YES;
}


-(void)webViewDidFinishLoad:(WKWebView *)webView {
    [self.activityIndicator stopAnimating];
}

-(void)webView:(WKWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"%@", error);
    [self.activityIndicator stopAnimating];
}





@end