【问题标题】:iMessage bubble calling web service freze the viewiMessage 气泡调用 Web 服务冻结视图
【发布时间】:2016-08-05 05:26:45
【问题描述】:

我正在使用 iMessage 气泡来创建正常工作的聊天视图

https://github.com/kerrygrover/iMessageBubble

当我在我的内部方法中调用我的网络服务时

- (IBAction)sendMessage:(id)sender

这是我的网络方法调用函数,它给出了结果输出成功并将我的聊天发送到服务器,但我的视图冻结了。

 NSError *errorReturned = nil;
    NSString *urlString = @"https://url/methodname”;
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod: @"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:employeeId forKey:@"employeeId"];
    [dict setObject:employeename forKey:@"employeename"];
      NSLog(@"dic=%@",dict);
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions     error:&errorReturned];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: jsonData];

    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];


    if (errorReturned)
    {
        //...handle the error

    }
    else
    {
        NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", responseString);

        //...do something with the returned value
    }
}

【问题讨论】:

    标签: objective-c iphone xcode ios8


    【解决方案1】:

    请创建一个新队列,不要在主队列上运行此代码。您正在主队列上运行代码请使用此

     dispatch_queue_t myQueue = dispatch_queue_create("myQueue", NULL);
    
            // execute a task on that queue asynchronously
            dispatch_async(myQueue, ^{
        NSError *errorReturned = nil;
            NSString *urlString = @"https://url/methodname”;
            NSURL *url = [NSURL URLWithString:urlString];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
            [request setHTTPMethod: @"POST"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setObject:employeeId forKey:@"employeeId"];
            [dict setObject:employeename forKey:@"employeename"];
              NSLog(@"dic=%@",dict);
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions     error:&errorReturned];
            [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
            [request setHTTPBody: jsonData];
    
            NSURLResponse *theResponse =[[NSURLResponse alloc]init];
            NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
    
    
            if (errorReturned)
            {
                //...handle the error
    
            }
            else
            {
                NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"%@", responseString);
    
                //...do something with the returned value
            }
    
            });
    

    【讨论】:

      猜你喜欢
      • 2014-07-06
      • 2013-10-31
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2011-12-23
      相关资源
      最近更新 更多