以异步方式调用对象的正确方法?

时间:2014-09-13 15:58:49

标签: ios objective-c

我把我的第一件物品弄得乱七八糟。该对象的目标是发送文本消息,即可。但是我从ViewDidLoad中的第二个UIViewController调用它,它仍然挂在Segue过渡中。所以我知道我需要异步地获取它,但是读取其他一些线程它们暗示了解决它的正确方法是使它成为“AppDelegate对象”,所以我假设我需要从AppDelegate调用该对象,但是我不确定如何解决这个问题,因为在我正在做的一些教程中我没有真正使用它,而且最重要的是,使用我的对象的正确方法是什么?

从我的视图控制器初始化对象

Twilio *twilio = [[Twilio alloc] init];
    [twilio sendMessage: self.phoneNumber: [self getRandomNumberBetween:1000 to:9999]];

标头文件

#import <Foundation/Foundation.h>

@interface Twilio : NSObject

@property (strong, nonatomic) NSString *TwilioSID;
@property (strong, nonatomic) NSString *TwilioSecret;
@property (strong, nonatomic) NSString *FromNumber;
@property (strong, nonatomic) NSString *ToNumber;
@property (strong, nonatomic) NSString *Message;

-(id)init;
-(id)sendMessage:(NSString *)phoneNumber :(NSString *)message;

@end

实施档案

#import "Twilio.h"

@implementation Twilio

-(id)init {
    self = [super init];

    if(self) {
        // Twilio Common constants
        self.TwilioSID = @"A....3";
        self.TwilioSecret = @"e...8";
        self.FromNumber = @"5...2";
        self.ToNumber = nil;
        self.Message = nil;
    }

    return self;
}

-(id)sendMessage:(NSString *)phoneNumber :(NSString *)message
{
    NSLog(@"Sending request.");

    self.ToNumber = phoneNumber;
    self.Message = message;

    // Build request
    NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", self.TwilioSID, self.TwilioSecret, self.TwilioSID];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];

    // Set up the body
    NSString *bodyString = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", self.FromNumber, self.ToNumber, self.Message];
    NSData *data = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    NSError *error;
    NSURLResponse *response;
    NSData *receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    // Handle the received data
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSString *receivedString = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
        NSLog(@"Request sent. %@", receivedString);
    }
    return self.Message;
}
@end

更新

根据下面的推荐,我改变了我的对象实现:

    //NSError *error;
    //NSURLResponse *response;
    //NSData *receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    [NSURLConnection sendAsynchronousRequest:request queue:self.queue completionHandler:^(NSURLResponse *response, NSData *data, NSError
*error) {

            // Handle the received data
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                NSString *receivedString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"Request sent. %@", receivedString);
            }
            NSLog(@"%@",response);
        }];

1 个答案:

答案 0 :(得分:1)

使用方法sendAsynchronousRequest:queue:completionHandler:

请参阅:https://stackoverflow.com/a/9270711/2828120