我设置了我的应用程序,以便在p2p连接期间以数组的形式向另一个人发送自定义级别。接收设备将阵列保存到文件中以备后用。我在应用程序中设置了gamekit,它将成功搜索并连接到另一台设备,而没有任何问题。虽然将数据发送到设备时会出现问题,但是接收设备将接收数据(并保存自定义级别,如应有的那样),但后言将立即崩溃。
这是我用来发送和接收数据的方法。
-(void) sendDataToPeers:(NSData *) data
{
if (currentSession)
{
//send the data
[self.currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
//Alerting the user that the custom level has been sent.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Your custom level has been sent." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void) btnSend
{
//Data that will be sent
NSMutableData *theData = [NSMutableData data];
//Archiver
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData];
//Desired level to send
int theLevel =[[CTManager sharedInstance]getLevel];
//Path to the custom levels
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *customLevelsSen = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"customLevels"]];
//Custom levels array
NSArray *theLevels = [[NSArray alloc] initWithContentsOfFile: customLevelsSen];
//Gets the desired level array from array of custom levels
NSArray *myArray = [[NSArray alloc]initWithArray:[theLevels objectAtIndex:theLevel-51]];
//prepare data
[archiver encodeObject:myArray forKey:@"level"];
[archiver finishEncoding];
//send the data
[self sendDataToPeers:theData];
//cleanup
[archiver release];
[theLevels release];
[myArray release];
}
-(void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context
{
//Archiver
NSKeyedUnarchiver *archiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//Gets the custom level in form of an array from data.
NSArray *level = [archiver decodeObjectForKey:@"level"];
[archiver finishDecoding];
[archiver release];
//Path to the array of custom levels
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *customLevelsRec = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"customLevels"]];
//Gets the array of custom levels
NSMutableArray *customLevelArray = [[NSMutableArray alloc] initWithContentsOfFile:customLevelsRec];
//Adds a new array to the array of custom levels
[customLevelArray addObject:level];
//Saves the array.
[customLevelArray writeToFile:customLevelsRec atomically:YES];
//cleanup
[customLevelArray release];
//Message saying a custom level has been recieved
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Received!" message:@"A custom level has been saved." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
由于目前我自己没有两台设备,因此对此进行测试非常痛苦,因此我将Beta版本发送给我的 friend ,后者又对其进行了测试(他拥有ipod和iphone)。任何帮助,对此表示赞赏...
如果找不到问题,我很可能会将整个xcode项目发送给他,并通过屏幕共享与他计算机上的项目一起工作,以有效地构建和测试应用程序。而且我将能够使用调试模式。
最佳答案
我不知道您是否找到了这个问题的答案,希望您找到了。但是,如果您没有这样做,我强烈建议您尝试使用新SDK的新功能。通过让您执行以下操作(使用发送方法),他们可以简化整个过程,而不是通过整个编码/解码过程:data = [NSKeyedArchiver archivedDataWithRootObject:anObject];
其中anObject几乎可以是任何对象,数组,字典等等。
在您的接收方法中:
NSObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
其中对象也几乎可以是任何对象。
就您所经历的崩溃而言,您是否已验证崩溃发生在哪条线上?您确定在您发布的代码中会发生这种情况吗?还是发生在其他地方?
关于ios - 游戏套件和iPhone,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2266656/