GKSession sendDataToAllPeers越来越无效的参数

问题描述:

这是我的第一篇文章,我希望通过感谢许多*的贡献者,他们永远不会知道他们在过去的几天中帮助我完成了我的第一个iOS应用。我感谢他们和这个网站给他们一个发布的地方。为了'还清'一些债务,我希望这将帮助其他人,因为我已经帮助...GKSession sendDataToAllPeers越来越无效的参数

因此,我的应用程序使用GKPeerPickerController来连接到第二个设备。一旦连接,设备可以发送短信给对方。接收方在UIAlertView中显示消息。一切工作正常。

然后,我决定尝试位置并添加代码以获取当前位置。我将它转换为纬度&度,分,秒的经度,并将它们放入一个NSString中。我在故事板中添加了一个名为“发送位置”的按钮,当点击该按钮时,会将位置发送给已连接的对等设备。这是我遇到问题的地方。

btnSendLocation使用NSString调用sendPacket。 sendPacket将字符串转换为NSData并调用失败的sendDataToAllPeers。当我学会如何捕获错误时,它是“无效参数 - sendDataToAllPeers:withDataMode:error:”。为什么发生错误?我有两个其他方法也调用sendPacket并正常工作。

-(IBAction)btnSendLocation: (id)sender 
    { 
// Put latitude, longitude into string, then convert to NSData for sending 
NSMutableString *tmp = [[NSMutableString alloc] initWithString:@"Latitude: "]; 
// Build the location string to send 
[tmp appendString:slatitude]; 
    // …code for longitude 
strToSend = [tmp copy]; 
bool sentPkt = false; 
sentPkt = [self sendPacket:strToSend failedWithError:nil]; 
} 

    - (bool)sendPacket: (NSString *)strToSend failedWithError: (NSError *)error 
{ 
bool sendPktOK = false; 
packet = [strToSend dataUsingEncoding:NSASCIIStringEncoding]; 
// Send the packet. 
sendPktOK = [self.currentSession sendDataToAllPeers:packet 
         withDataMode:GKSendDataReliable error:&error]; 
// If there was an error, log it. 
if (!sendPktOK) { 
    NSLog(@"Error Domain: %@", [error domain]); 
    NSLog(@"Error Descr: %@", [error localizedDescription]); 
    NSLog(@"Error Code: %@", [error code]); 
    return NO; 
}  
return YES; 
} 

    - (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
    { 
// Location default is decimal degrees notation; convert to deg-min-secs 
    int degrees = newLocation.coordinate.latitude; 
    double decimalPart = fabs(newLocation.coordinate.latitude - degrees); 
    int minutes = decimalPart * 60; 
    double seconds = decimalPart * 3600 - minutes * 60; 
    NSString slatitude = [NSString stringWithFormat:@"%d° %d' %1.1f\"", 
        degrees, minutes, seconds]; 
// rest of code... 
    } 
+0

欢迎来到Stack Overflow。你解决了你的问题真是太好了。您应该将回答*作为回答这个问题的答案,然后点击绿色的选中标记将其标记为已接受的答案。 – 2012-04-08 05:28:46

我能够做一些更多的测试,并找到了答案。这个问题是不是在sendDataToAllPeers,它是在的NSString(strToSend),以NSData的转换:

packet = [strToSend dataUsingEncoding:NSASCIIStringEncoding]; 

具体地说,是程度的标志符(小圆圈,ASCII 176)。 NSASCIIStringEncoding只包含高达127的ASCII字符,这在NSString.h(Foundation.framework)中的注释中得到确认。我确定有一种更快的方法来找到问题,但我还不熟悉Objective-C或Xcode的调试工具。