无法从完成处理程序进行网络调用?

时间:2015-07-27 05:56:46

标签: ios objective-c iphone xcode ipad

我正在尝试访问用户表单手机的联系人。所以或那个目的我正在使用完成处理程序。所以当应用程序尝试访问用户的联系人时,它会显示警报&请求允许联系人的许可。当用户按下“确定”按钮时。

enter image description here

然后执行一个块&那个时候我打网络电话。虽然这个功能被调用但代表从未被调用过&回复从未收到过。

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {

        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            NSLog(@"access contact");
           [self sample];

        });
          NSLog(@"after sccess contact");

    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        // The user has previously given access, add the contact
        NSLog(@"previous access");
    }
    else
    {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
        NSLog(@"send alert");
    }
    CFErrorRef error = NULL;

网络电话

-(void)sample
{
    NSLog(@"sample func called");
    // Create the request.
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];

    // Specify that it will be a POST request
    request.HTTPMethod = @"POST";

    // This is how we set header fields
    [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    // Convert your data and set your request's HTTPBody property
    NSString *stringData = @"some data";
    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = requestBodyData;

    // Create url connection and fire request
    sampleConn= [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

1 个答案:

答案 0 :(得分:2)

试试这个

替换您的代码

[self sample];

使用

dispatch_async(dispatch_get_main_queue(), ^{

    [self sample];
});