在 iOS 的一个区块中无法获得网络调用的响应?
Unable to get response of network call in a block in iOS?
我正在从 objective 中的 block
发送网络请求 - c.My 函数被调用但我没有得到网络调用的响应意味着委托方法是从来没有 called.I 认为这是与 it.Actually 相关的线程的问题我正在尝试访问 user.For 的联系人,目的是块总是 called.When 块被称为它询问用户对于允许访问 contacts.when 用户的权限,请按 `'OK' 然后我调用我的自定义 function.Function,但未收到响应,也未调用委托。
访问联系人:
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];
}
使用这个
dispatch_async(dispatch_get_main_queue(), ^{
[self sample];
});
我正在从 objective 中的 block
发送网络请求 - c.My 函数被调用但我没有得到网络调用的响应意味着委托方法是从来没有 called.I 认为这是与 it.Actually 相关的线程的问题我正在尝试访问 user.For 的联系人,目的是块总是 called.When 块被称为它询问用户对于允许访问 contacts.when 用户的权限,请按 `'OK' 然后我调用我的自定义 function.Function,但未收到响应,也未调用委托。
访问联系人:
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];
}
使用这个
dispatch_async(dispatch_get_main_queue(), ^{
[self sample];
});