p2p 接続中にカスタム レベルを配列形式で別の人に送信するようにアプリを設定しました。受信デバイスは、後で使用するために配列をファイルに保存します。アプリケーションでゲームキットをセットアップすると、問題なく別のデバイスを検索して接続できます。デバイスにデータを送信すると問題が発生しますが、受信側のデバイスはデータを受信します (そして、カスタム レベルを正常に保存します) が、後ですぐにクラッシュします。
データの送受信に使用するメソッドを次に示します。
-(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];
}
現在、私は自分のデバイスを 2 つ持っていないので、これをテストするのは大変なことでした。そのため、ベータ ビルドを友人に送り、テストしてもらいました (彼は iPod と iPhone を持っています)。これについての助けをいただければ幸いです...
問題が見つからない場合は、おそらく xcode プロジェクト全体を彼に送信し、画面共有を介して彼のコンピューターでプロジェクトを操作して、アプリケーションを効率的にビルドおよびテストします。そして、デバッグモードを使用できるようになります。