項目概述: 我目前正在Unity iOS上工作,並在使用customURL的2個應用程序之間發送信息。應用程序A成功發送數據到應用程序B,應用程序B將數據接收存儲在一個靜態變量中。但是,當應用程序完成啓動並嘗試檢索數據(NSString *)時,會發生段錯誤11(應用程序崩潰)。Xcode分割錯誤11(自定義url)
新的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;
}
的的NSLog中的OpenURL記錄該IGN正確地根據從所述第一應用程序的輸入。然而,當應用B完成啓動並試圖通過調用extern方法「GetAccign」來獲得Ign時,應用B與分段錯誤11一起崩潰。
extern方法「GetAccID」根據輸入成功返回AccountID。