访问 iOS 中的联系人后无法拨打网络电话?
unable to make network call after accessing contacts in iOS?
我遇到了一个非常奇怪的情况 here.In 我的应用程序我正在尝试访问用户 phone 的联系人,当用户授予权限时,我正在尝试对 server.My 代码在访问联系人时工作正常,然后在进行网络调用后永远不会调用委托方法。
代码
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];
时,函数被调用,但委托方法从未被调用,如果我从代码中的其他任何地方调用此函数,我不会得到 that.But 的响应,然后它工作正常。
尝试这样使用
dispatch_async(dispatch_get_main_queue(), ^{
[self sample];
});
我遇到了一个非常奇怪的情况 here.In 我的应用程序我正在尝试访问用户 phone 的联系人,当用户授予权限时,我正在尝试对 server.My 代码在访问联系人时工作正常,然后在进行网络调用后永远不会调用委托方法。
代码
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];
时,函数被调用,但委托方法从未被调用,如果我从代码中的其他任何地方调用此函数,我不会得到 that.But 的响应,然后它工作正常。
尝试这样使用
dispatch_async(dispatch_get_main_queue(), ^{
[self sample];
});