访问iOS中的联系人后无法拨打网络电话?

时间:2015-07-23 12:41:59

标签: ios objective-c iphone xcode ipad

我在这里有一个非常奇怪的情况。在我的应用程序中,我试图访问用户手机的联系人,当用户授予权限,然后我正在尝试对服务器进行网络调用。我的代码在访问联系人时工作正常在进行网络调用后,委托方法永远不会被调用。

代码

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];//Here is the function i call for making network request.
        });
    }
    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");
    }

发出网络请求的功能

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

    // 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];


}

委派方法

 //delegate methods of NSURLCONNECTION
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
    {
        NSLog(@"response recieved");

    }
    - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
    {

        if([connection isEqual:sampleConn])
        {
            NSLog(@"this is sample");

        }
    }
    - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
    {
        NSLog(@"fail to load");

    }
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {



    }

当我调用[self sample];时,调用函数但是从不调用委托方法&我没有得到回应。但是,如果我从代码中的任何其他地方调用此函数,那么它工作正常。

1 个答案:

答案 0 :(得分:0)

尝试像这样使用

dispatch_async(dispatch_get_main_queue(), ^{

    [self sample];
});