FIRIAMMessageContentDataWithImageURL.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseCore/FIRAppInternal.h>
  17. #import <FirebaseCore/FIRLogger.h>
  18. #import "FIRCore+InAppMessaging.h"
  19. #import "FIRIAMMessageContentData.h"
  20. #import "FIRIAMMessageContentDataWithImageURL.h"
  21. #import "FIRIAMSDKRuntimeErrorCodes.h"
  22. static NSInteger const SuccessHTTPStatusCode = 200;
  23. @interface FIRIAMMessageContentDataWithImageURL ()
  24. @property(nonatomic, readwrite, nonnull, copy) NSString *titleText;
  25. @property(nonatomic, readwrite, nonnull, copy) NSString *bodyText;
  26. @property(nonatomic, copy, nullable) NSString *actionButtonText;
  27. @property(nonatomic, copy, nullable) NSURL *actionURL;
  28. @property(nonatomic, nullable, copy) NSURL *imageURL;
  29. @property(readonly) NSURLSession *URLSession;
  30. @end
  31. @implementation FIRIAMMessageContentDataWithImageURL
  32. - (instancetype)initWithMessageTitle:(NSString *)title
  33. messageBody:(NSString *)body
  34. actionButtonText:(nullable NSString *)actionButtonText
  35. actionURL:(nullable NSURL *)actionURL
  36. imageURL:(nullable NSURL *)imageURL
  37. usingURLSession:(nullable NSURLSession *)URLSession {
  38. if (self = [super init]) {
  39. _titleText = title;
  40. _bodyText = body;
  41. _imageURL = imageURL;
  42. _actionButtonText = actionButtonText;
  43. _actionURL = actionURL;
  44. if (imageURL) {
  45. _URLSession = URLSession ? URLSession : [NSURLSession sharedSession];
  46. }
  47. }
  48. return self;
  49. }
  50. #pragma protocol FIRIAMMessageContentData
  51. - (NSString *)description {
  52. return [NSString stringWithFormat:@"Message content: title '%@',"
  53. "body '%@', imageURL '%@', action URL '%@'",
  54. self.titleText, self.bodyText, self.imageURL, self.actionURL];
  55. }
  56. - (NSString *)getTitleText {
  57. return _titleText;
  58. }
  59. - (NSString *)getBodyText {
  60. return _bodyText;
  61. }
  62. - (nullable NSString *)getActionButtonText {
  63. return _actionButtonText;
  64. }
  65. - (void)loadImageDataWithBlock:(void (^)(NSData *_Nullable imageData,
  66. NSError *_Nullable error))block {
  67. if (!block) {
  68. // no need for any further action if block is nil
  69. return;
  70. }
  71. if (!_imageURL) {
  72. // no image data since image url is nil
  73. block(nil, nil);
  74. } else {
  75. NSURLRequest *imageDataRequest = [NSURLRequest requestWithURL:_imageURL];
  76. NSURLSessionDataTask *task = [_URLSession
  77. dataTaskWithRequest:imageDataRequest
  78. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  79. if (error) {
  80. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000003",
  81. @"Error in fetching image: %@", error);
  82. block(nil, error);
  83. } else {
  84. if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  85. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  86. if (httpResponse.statusCode == SuccessHTTPStatusCode) {
  87. if (httpResponse.MIMEType == nil || ![httpResponse.MIMEType hasPrefix:@"image"]) {
  88. NSString *errorDesc =
  89. [NSString stringWithFormat:@"None image MIME type %@"
  90. " detected for url %@",
  91. httpResponse.MIMEType, self.imageURL];
  92. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000004", @"%@", errorDesc);
  93. NSError *error =
  94. [NSError errorWithDomain:kFirebaseInAppMessagingErrorDomain
  95. code:FIRIAMSDKRuntimeErrorNonImageMimetypeFromImageURL
  96. userInfo:@{NSLocalizedDescriptionKey : errorDesc}];
  97. block(nil, error);
  98. } else {
  99. block(data, nil);
  100. }
  101. } else {
  102. NSString *errorDesc =
  103. [NSString stringWithFormat:@"Failed HTTP request to crawl image %@: "
  104. "HTTP status code as %ld",
  105. self->_imageURL, (long)httpResponse.statusCode];
  106. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000001", @"%@", errorDesc);
  107. NSError *error =
  108. [NSError errorWithDomain:NSURLErrorDomain
  109. code:httpResponse.statusCode
  110. userInfo:@{NSLocalizedDescriptionKey : errorDesc}];
  111. block(nil, error);
  112. }
  113. } else {
  114. FIRLogWarning(kFIRLoggerInAppMessaging, @"I-IAM000002",
  115. @"Internal error: got a non http response from fetching image for "
  116. @"image url as %@",
  117. self->_imageURL);
  118. }
  119. }
  120. }];
  121. [task resume];
  122. }
  123. }
  124. @end