iOS平台下Objective-C与Unity应用的文本数据共享方案咨询
Hey there! I’ve worked through similar cross-app data sharing scenarios on iOS before, so here are several proven, practical solutions tailored to your use case:
This is Apple’s official recommended method for data sharing between apps under your developer account—it enables seamless, background-friendly sharing without user intervention.
Setup & Implementation:
- First, configure an identical App Group ID (e.g.,
group.com.yourteam.3dmodelshare) for both apps in the Apple Developer Portal, then enable the group in each app’s Xcode project settings. - In your Objective-C export app, write the model text to the shared container:
NSURL *sharedContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.yourteam.3dmodelshare"]; NSURL *modelFileURL = [sharedContainerURL URLByAppendingPathComponent:@"exported_3d_model.txt"]; [yourModelTextString writeToURL:modelFileURL atomically:YES encoding:NSUTF8StringEncoding error:nil]; - In your Unity C# viewer, read from the shared container (note the path adjustment for Unity’s sandbox):
string sharedContainerPath = Path.Combine(Application.temporaryCachePath, "../Group Containers/group.com.yourteam.3dmodelshare"); string modelFilePath = Path.Combine(sharedContainerPath, "exported_3d_model.txt"); if (File.Exists(modelFilePath)) { string modelData = File.ReadAllText(modelFilePath); // Proceed to render your 3D model }
Practical Notes:
- Ensure both apps’ Bundle IDs are linked to the same App Group in Xcode.
- No extra user permissions are needed for same-group app access.
Great if you want to trigger the viewer app directly from the exporter and pass lightweight model text (note: URL payloads have a size limit ~2KB, so only use for small models).
Setup & Implementation:
- Add a custom URL scheme (e.g.,
3dmodelviewer://) to your Unity viewer’sInfo.plist(underCFBundleURLTypes). - In your Objective-C export app, encode and send the model text via the URL:
NSString *encodedModelText = [yourModelTextString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSString *launchURLString = [NSString stringWithFormat:@"3dmodelviewer://load?data=%@", encodedModelText]; NSURL *launchURL = [NSURL URLWithString:launchURLString]; if ([[UIApplication sharedApplication] canOpenURL:launchURL]) { [[UIApplication sharedApplication] openURL:launchURL options:@{} completionHandler:nil]; } - In your Unity C# viewer, handle the incoming URL:
void Awake() { // Handle URL if app launches directly from the exporter if (!string.IsNullOrEmpty(Application.absoluteURL)) { ProcessModelURL(Application.absoluteURL); } } void OnApplicationPause(bool paused) { // Handle URL if app is woken from background if (!paused && !string.IsNullOrEmpty(Application.absoluteURL)) { ProcessModelURL(Application.absoluteURL); } } void ProcessModelURL(string url) { if (url.StartsWith("3dmodelviewer://load?")) { string queryParams = url.Substring("3dmodelviewer://load?".Length); var parameters = HttpUtility.ParseQueryString(queryParams); if (parameters["data"] != null) { string decodedModelText = HttpUtility.UrlDecode(parameters["data"]); // Render the model } } }
Practical Notes:
- Add the scheme to the exporter’s
LSApplicationQueriesSchemesinInfo.plistto check if the viewer is installed. - Avoid using this for large model texts—you’ll hit URL length limits.
Perfect if you want users to manually share models between apps, and it supports large text files without size restrictions.
Setup & Implementation:
- In your Objective-C export app, trigger AirDrop with the model text:
NSData *modelData = [yourModelTextString dataUsingEncoding:NSUTF8StringEncoding]; UIActivityViewController *shareVC = [[UIActivityViewController alloc] initWithActivityItems:@[modelData] applicationActivities:nil]; [self presentViewController:shareVC animated:YES completion:nil]; - In your Unity viewer, configure it to accept text files by adding a document type to
Info.plist(declare support forpublic.textUTI), then handle incoming files:void OnOpenURL(string url) { if (url.StartsWith("file://")) { string localFilePath = url.Replace("file://", ""); string modelText = File.ReadAllText(localFilePath); // Render the model } }
Practical Notes:
- Users need to select your viewer app from the AirDrop menu, so this is a user-initiated flow, not automated.
Use this if you need models to persist across devices or be stored long-term—requires users to have an active iCloud account.
Setup & Implementation:
- Enable iCloud capabilities for both apps in Xcode, using the same iCloud container.
- In your Objective-C export app, save the model to iCloud Drive:
NSURL *iCloudContainerURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; if (iCloudContainerURL) { NSURL *modelFileURL = [iCloudContainerURL URLByAppendingPathComponent:@"Documents/3d_model_export.txt"]; [yourModelTextString writeToURL:modelFileURL atomically:YES encoding:NSUTF8StringEncoding error:nil]; } - In your Unity viewer, read the iCloud file using a native iOS plugin or Unity’s iCloud APIs to fetch the file path, then load the text content as you would a local file.
Practical Notes:
- Depends on users having enough iCloud storage and being signed in.
Recommendation for Your Use Case
If both apps are under your developer account, App Groups + Shared Container is the best choice—it’s fast, automated, and supports large model texts without user input. For user-initiated sharing, AirDrop works great for larger files, while URL Schemes are ideal for quick, small model transfers.
内容的提问来源于stack exchange,提问作者user9668888

