当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - Which version of SendGrid is compatible with AFNetworking 3.0?

问题描述:

In my app, I'm sending emails through SendGrid (0.2.0). Sendgrid was working properly with afnetworking 2.0 but due to some reason, I had to update afnetworking version from 2.0 to 3.0. But now SendGrid is giving errors.So I update it too through a pod.

pod error - Analyzing dependencies

[!] Unable to satisfy the following requirements:

  • AFNetworking (~> 3.0) required by Podfile
  • AFNetworking (~> 2.0) required by SendGrid (0.3.0)

content of Podfile:

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, ‘8.0’

target ‘Projectname’ do

pod 'Firebase/Core'

pod 'Firebase/Messaging'

pod 'AFNetworking', '~> 3.0’

pod 'SendGrid', '~> 0.3.0’

end

Can anybody tell me how to resolve this issue?

thank you!

网友答案:

To Customize SendGrid there are some point to pay attention.

  • You have to add sendGrid manually without Pods.
  • Add AFNetworking 3.0 using Pod.

Now, in SendGrid there is one method :

- (void)sendWithWeb:(SendGridEmail *)email successBlock:(void(^)(id responseObject))successBlock failureBlock:(void(^)(NSError *error))failureBlock

That method you have to edit like as follow

- (void)sendWithWeb:(SendGridEmail *)email successBlock:(void(^)(id responseObject))successBlock failureBlock:(void(^)(NSError *error))failureBlock
{

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"
                                                                                              URLString:self.baseURL
                                                                                             parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                                                                                                 for (int i = 0; i < email.imgs.count; i++)
                                                                                                 {
                                                                                                     UIImage *img = [email.imgs objectAtIndex:i];
                                                                                                     NSString *filename = [NSString stringWithFormat:@"image%d.png", i];
                                                                                                     NSString *name = [NSString stringWithFormat:@"files[image%d.png]", i];
                                                                                                     NSLog(@"name: %@, Filename: %@", name, filename);
                                                                                                     NSData *imageData = UIImagePNGRepresentation(img);
                                                                                                     [formData appendPartWithFileData:imageData name:name fileName:filename mimeType:@"image/png"];
                                                                                                 }


    }
                                                                                                  error:nil];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager
                  uploadTaskWithStreamedRequest:request
                  progress:^(NSProgress * _Nonnull uploadProgress) {
                      // This is not called back on the main queue.
                      // You are responsible for dispatching to the main queue for UI updates
                      dispatch_async(dispatch_get_main_queue(), ^{
                          //Update the progress view
                      });
                  }
                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                      if (error) {
                          NSLog(@"Error: %@", error);
                          failureBlock(error);
                      } else {
                          NSLog(@"%@ %@", response, responseObject);
                          successBlock(responseObject);
                      }
                  }];

    [uploadTask resume];
}

Like this you have to edit methods. Do editional changes as per your requirement.

Note

That's not tested code. its just example.

分享给朋友:
您可能感兴趣的文章:
随机阅读: