【发布时间】:2015-10-14 09:18:23
【问题描述】:
项目概述: 我目前正在使用 Unity iOS 并在我正在使用 customURL 的 2 个应用程序之间发送信息。 App A 成功向 App B 发送数据,App B 将接收到的数据存储在静态变量中。但是,当应用程序完成启动并尝试检索数据(NSString*)时,会发生分段错误 11(应用程序崩溃)。
Xcode 新手,我对解决分段错误一无所知。请指导我解决问题。谢谢。
CustomUrlContoller.mm
#import "CustomURLController.h"
#import <stdio.h>
#import <stdlib.h>
@implementation CustomURLUnity
static int AccountID = -1;
static NSString *AccountIgn = @"";
+(void)SetAccount:(int)NewID AccIgn:(NSString*)NewIgn
{
AccountID = NewID;
AccountIgn = NewIgn;
}
+(int)GetAccountID { return AccountID; }
+(NSString*)GetAccountIgn { return AccountIgn; }
@end
extern "C" {
int GetAccID() { return [CustomURLUnity GetAccountID]; } //WORKS
char* GetAccIGN() {
NSLog(@"GetAccIGN: %@", [CustomURLUnity GetAccountIgn]); //CRASH
NSString* IgnString = [CustomURLUnity GetAccountIgn];
if(IgnString isEqual:[NSNull null]])
{
IgnString = @"";
}
const char* stringAsChar = [IgnString UTF8String];
char* cpy = (char*)calloc(1, [IgnString length]+1);
strncpy(cpy, stringAsChar, [IgnString length]);
return cpy;
}
}
UnityAppController.mm
#import "CustomURLController.h"
-(BOOL) application:(UIApplication*)application openURL:(NSURL*)url
sourceApplication:(NSString*)sourceApplication
annotation(id)annotation
{
/*...default...*/
if([sourceApplication isEqualToString:@"com.xxx.xxx"])
{
NSString* QueryStrings = [url query];
NSArray* AccountStrings = [QueryStrings componentsSeparatedByString: @"&"];
NSString* AccountID = [AccountStrings objectAtIndex:0];
NSString* IGN = [AccountStrings objectAtIndex:1];
int AccountIDNo = [AccountID intvalue];
[CustomURLUnity SetAccount:AccountIDNo AccIgn:IGN];
NSLog(@"openURL GetIgn: %@", [CustomURLUnity GetAccountIgn]);
}
return YES;
}
openURL 中的 NSLog 根据第一个 App 的输入正确记录 Ign。但是,当 App B 完成启动并尝试通过调用 extern 方法“GetAccIGN”获取 Ign 时,App B 崩溃并出现分段错误 11。
外部方法“GetAccID”根据输入成功返回AccountID。
【问题讨论】:
标签: ios xcode unity3d nsstring segmentation-fault