1

我将我的应用程序设置为在 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 项目发送给他,并通过屏幕共享与他计算机上的项目一起工作,以有效地构建和测试应用程序。我将能够使用调试模式。

4

3 回答 3

2

我不知道你是否找到了这个问题的答案,我希望你找到了。但是,如果您不这样做,我强烈建议您尝试新 SDK 的新功能。他们没有通过整个编码/解码过程,而是通过让您执行以下操作(在您的发送方法中)使其变得简单:

data = [NSKeyedArchiver archivedDataWithRootObject:anObject];

其中 anObject 几乎可以是任何对象、数组、字典等等……

在您的接收方法中:

NSObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];

其中 object 也可以是几乎任何对象。

至于您遇到的崩溃,您是否验证了崩溃发生在哪条线路上?您确定它发生在您发布的代码中吗?还是发生在其他地方?

于 2011-04-12T16:04:07.570 回答
0
NSData* data = [@"TEXT" dataUsingEncoding:NSUTF8StringEncoding];

NSError *error = nil;
    [self.session sendData:data toPeers:peerID withDataMode:GKSendDataReliable error:&error];



(void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {

   NSString* message = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];//@"TEXT"

   NSString* nameOfTheTransmitter = [session displayNameForPeer:peer];// name who sent

}
于 2012-09-14T06:44:42.730 回答
0

我在您的 receiveData 方法中没有发现任何问题。

您是否检查过您尝试保存数据的文件夹 (customLevels) 是否存在?

我已成功通过使用 GameKit 的应用程序连接设备和 iPhone 模拟器。调试起来真的很方便。
我没有检查它是通过蓝牙还是wifi。

于 2010-02-15T16:10:23.857 回答