【发布时间】:2016-01-20 21:15:59
【问题描述】:
我是 iOS 编程的初学者。我对 NSURLConnection 有一些问题:我已经安装了 SWRevealViewController https://github.com/John-Lluch/SWRevealViewController,当我的应用程序从服务器加载数据时,我无法使用与屏幕的交互。加载数据时,我无法打开我的 SWR 菜单。
这是我在 viewDidLoad 中的 SWR:
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController ) {
[self.openMenyItmet setTarget: self.revealViewController];
[self.openMenyItmet setAction: @selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
之后,我在 viewDidLoad 中调用了 Get 方法:
[self GetQUIZ];
方法详解:
- (void)GetQUIZ {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *url = [NSString stringWithFormat:@"http://stringlearning.com/api/v1/user-quiz?token=%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"token"]];
[request setURL:[NSURL URLWithString: url]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[UIDevice currentDevice].name forHTTPHeaderField:@"device"];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Left menu, User details: %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
NSLog(@"%@", [request allHTTPHeaderFields]);
if(conn) {
NSLog(@"Connection Successful");
} else
NSLog(@"Connection could not be made");
然后我在connectionDidFinishLoading中使用数据:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *deserr = nil;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseData options: 0 error: &deserr];
我读到我应该使用异步方法,但我以前从未使用过它。你会写一些详细的解决方案吗? 也许,有不同的路径吗? 非常感谢您的帮助!
【问题讨论】:
标签: ios objective-c nsurlconnection