Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. // This object will generate HTML requests for our WebView, that we will use to show the coupon
  5. // details and fire the redemption
  6. id theDelegate = [[UIApplication sharedApplication] delegate];
  7. self.requestFactory = [[theDelegate coupiesManager] requestFactoryForType:kCOUPIESRequestFactoryTypeHTML];
  8.  
  9. // Load the details of the coupon and count the click
  10. [webView loadRequest:[self.requestFactory requestForCouponWithId:(NSUInteger)self.coupon.couponId]];
  11. }
  12.  
  13. #pragma mark - Private methods
  14.  
  15. /**
  16. * Some coupons can be redeemed by taking a photo of the receipt after buying a product.
  17. * In this case, we open the photo-view in the COUPIES framework.
  18. */
  19. - (void)redeemCouponCashback:(id)sender {
  20. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  21. id theDelegate = [[UIApplication sharedApplication] delegate];
  22.  
  23. // Initialize COUPIESCashbackRedemptionViewController from Bundle and present it as a modal
  24. UIStoryboard *cashbackStoryboard = [UIStoryboard storyboardWithName:@"COUPIESCashbackRedemptionMain" bundle:[NSBundle CoupiesResourcesBundle]];
  25. COUPIESCashbackRedemptionViewController *viewController = [cashbackStoryboard instantiateViewControllerWithIdentifier:@"COUPIESCashbackRedemptionViewController"];
  26. viewController.redemptionDelegate = self;
  27. viewController.restService = [[theDelegate coupiesManager] newRestService];
  28. viewController.coupon = self.coupon;
  29. [self presentViewController:viewController animated:YES completion:nil];
  30. } else {
  31. //Error handling in case the device has no camera. In this case, those coupons cannot be redeemed.
  32. }
  33. }
  34.  
  35. /**
  36. * This is a general action that triggers the redemption depending on the type of coupon
  37. * It could be called from an IBAction, but in this case we intercept the click on "redeem now" in the
  38. * WebView and trigger it from here.
  39. */
  40. - (IBAction)redeemCoupon {
  41. // Only coupons can be redeemed. Offers and DailyDeals link to a website instead.
  42. if ([self.coupon isKindOfClass:[COUPIESCoupon class]]) {
  43. COUPIESCoupon *theCoupon = self.coupon;
  44. if (theCoupon.action == kActionCashback) {
  45. [self redeemCouponCashback:nil];
  46. } else {
  47. // In this case we can directly load the green redemption view and need no ViewController in between.
  48. [self didChooseRedeemDespiteNoSticker];
  49. }
  50. }
  51. }
  52.  
  53. #pragma mark COUPIESCashbackRedemptionViewControllerDelegate
  54.  
  55. - (void)didFinishPickingReceipt:(NSArray *)images withQuantity:(int)quantity {
  56. [activityIndicatorView startAnimating];
  57.  
  58. [webView loadRequest:[self.requestFactory requestForRedeemingCoupon:self.coupon withReceipt:images quantity:quantity]];
  59. }
  60.  
  61. #pragma mark UIWebViewDelegate
  62.  
  63. /**
  64. * In this function, we intercept the click in "redeem now", so we can use the native camera feature
  65. * to take a photo of a receipt where required. In case of a Cashback-Coupon, the HTML-representation redirects to a "preview"-page,
  66. * where the picture is taken. Since this page is replaced by the native part of the framework, we intercept this as well.
  67. */
  68. - (BOOL)webView:(UIWebView *)inWebView shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inNavigationType {
  69. if (inRequest.URL.path && [inRequest.URL.path rangeOfString:@"/redemptions/cashback/preview"].location != NSNotFound) {
  70. [self redeemCoupon];
  71. return NO;
  72. } else {
  73. return YES;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement