无法从完成处理程序进行网络调用?
Unable to make network call from completion handler?
我正在尝试访问用户表单 phone.So 的联系人,或者我正在使用完成 handler.So 的目的,当应用程序尝试访问用户的联系人时,它会显示警告并请求权限允许 contacts.When 用户按下按钮 'OK'。
然后执行一个块,那时我创建网络 call.Although 函数被调用,但从未调用委托,也从未收到响应。
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];
}
试试这个
替换您的代码
[self sample];
有
dispatch_async(dispatch_get_main_queue(), ^{
[self sample];
});
我正在尝试访问用户表单 phone.So 的联系人,或者我正在使用完成 handler.So 的目的,当应用程序尝试访问用户的联系人时,它会显示警告并请求权限允许 contacts.When 用户按下按钮 'OK'。
然后执行一个块,那时我创建网络 call.Although 函数被调用,但从未调用委托,也从未收到响应。
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];
}
试试这个
替换您的代码
[self sample];
有
dispatch_async(dispatch_get_main_queue(), ^{
[self sample];
});