Ios 无法从完成处理程序进行网络调用?
我正在尝试访问用户表单电话的联系人。出于某种目的,我正在使用完成处理程序。因此,当应用程序尝试访问用户的联系人时,它会显示警报并请求允许联系人。当用户按下“确定”按钮时 然后执行一个块&此时我进行网络调用。虽然调用了函数,但从未调用委托&从未收到响应Ios 无法从完成处理程序进行网络调用?,ios,objective-c,iphone,xcode,ipad,Ios,Objective C,Iphone,Xcode,Ipad,我正在尝试访问用户表单电话的联系人。出于某种目的,我正在使用完成处理程序。因此,当应用程序尝试访问用户的联系人时,它会显示警报并请求允许联系人。当用户按下“确定”按钮时 然后执行一个块&此时我进行网络调用。虽然调用了函数,但从未调用委托&从未收到响应 ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL); if (ABAddressBookGetAuthorizationStatus()
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];
与
您是否检查了failure delegate方法?是的,我已检查过。如果我使用[viewdidLoad]方法进行网络cal,则会收到响应。但在另一种情况下,我没有收到任何响应。@AshishP.Hey您之前的回答在我的演示代码中起作用。@AshishI认为您是在地址簿参考的完成块中进行ws调用,http请求还从完成块获取委托引用,即实际上是地址簿的委托引用。因此,finish方法不会在您的代理上被调用。
dispatch_async(dispatch_get_main_queue(), ^{
[self sample];
});