我将我的应用程序设置为在 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 版本,他反过来测试了它们(他有 ipod 和 iphone)。对此的任何帮助表示赞赏......
如果我找不到问题,我很可能会将整个 xcode 项目发送给他,并通过屏幕共享与他计算机上的项目一起工作,以有效地构建和测试应用程序。我将能够使用调试模式。