Xamarin Forms Share Extension同步NSUserDefaults值失败及相关问题求助
我正在实现Xamarin Forms的Share Extension,让我的应用能接收其他应用发来的Zip文件。已经在Apple开发者账号中配置好App Group,也能获取到文件的共享URL,但通过NSUserDefaults将文件信息同步回容器应用时遇到了问题。
(注:Xamarin Forms的扩展应用无法调试,所以我给代码加了日志语句)
扩展应用代码
internal class CodeBasedViewController : SLComposeServiceViewController { public override async void DidSelectPost() { int count = 0; try { NSExtensionItem item = ExtensionContext.InputItems[0]; foreach(NSItemProvider prov in item.Attachments) { Log(count++.ToString()); if (prov.HasItemConformingTo(UTType.URL) == true) { Log("url type ok"); //Load item with Load Completion action prov.LoadItem(UTType.URL, null, async (dataRaw, error) => { Log("load url item ok"); //dataRaw是类似这样的路径:"file:///Users/appname/Library/Developer/CoreSimulator/Devices/..GUID1../data/Containers/Shared/AppGroup/...GUID2.../" string uri = dataRaw.ToString(); Log2(uri); NSUserDefaults shared = new NSUserDefaults( "group.com.companyname.appname.ShareExtension", NSUserDefaultsType.SuiteName); shared.SetString(uri, "zipfile"); if (shared.Synchronize()) { //同步成功,代码走到这里 Log("sync success"); } else Log("sync failed"); //base.DidSelectPost(); }); } } } catch (Exception e) { Log("ex: " + e.Message); } } }
遇到的问题
LoadItem的完成操作处理6KB文件耗时约7秒,时长过长;- 取消注释
base.DidSelectPost()后,点击“Post”时扩展会立即关闭,怀疑完成操作代码被跳过; - 容器应用激活时读取
NSUserDefaults,发现uri值未同步过来(我认为获取到该uri就能访问zip文件)。
容器应用读取代码
public override void OnActivated(UIApplication uiApplication) { base.OnActivated(uiApplication); NSUserDefaults shared = new NSUserDefaults( "group.com.companyname.appname.ShareExtension", NSUserDefaultsType.SuiteName); //ok = true bool ok = shared.Synchronize(); //这里返回null!! string uri = shared.StringForKey("zipfile"); }
我已经在主机虚拟机中创建了和容器、扩展应用Bundle名称一致的XCode应用,同样配置了App Groups,但问题依然存在。以下是两个应用的Info.plist文件:
容器应用Info.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>UIDeviceFamily</key> <array> <integer>1</integer> <integer>2</integer> </array> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>MinimumOSVersion</key> <string>8.0</string> <key>CFBundleDisplayName</key> <string>MyApp</string> <key>CFBundleIdentifier</key> <string>com.companyname.MyApp</string> <key>CFBundleVersion</key> <string>1.0</string> <key>CFBundleName</key> <string>MyApp</string> <key>XSAppIconAssets</key> <string>Assets.xcassets/AppIcon.appiconset</string> <key>UILaunchStoryboardName</key> <string>LaunchScreen</string> <key>UIViewControllerBasedStatusBarAppearance</key> <false/> <key>NSExtensionPointIdentifier</key> <string>com.apple.share-services</string> </dict> </plist>
扩展应用Info.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleDevelopmentRegion</key> <string>en</string> <key>CFBundleDisplayName</key> <string>MyApp ShareExtension</string> <key>CFBundleExecutable</key> <string>MyApp.iOS.ShareExtension</string> <key>CFBundleIdentifier</key> <string>com.companyname.MyApp.ShareExtension</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>com.your-company.MyApp.iOS.ShareExtension</string> <key>CFBundlePackageType</key> <string>XPC!</string> <key>CFBundleShortVersionString</key> <string>1.0</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>1</string> <key>MinimumOSVersion</key> <string>8.0</string> <key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>NSExtensionActivationRule</key> <dict> <key>NSExtensionActivationSupportsFileWithMaxCount</key> <integer>1</integer> <key>NSExtensionActivationSupportsImageWithMaxCount</key> <integer>1</integer> <key>NSExtensionActivationSupportsMovieWithMaxCount</key> <integer>0</integer> <key>NSExtensionActivationSupportsText</key> <true/> <key>NSExtensionActivationSupportsWebURLWithMaxCount</key> <integer>1</integer> </dict> </dict> <key>NSExtensionPrincipalClass</key> <string>CodeBasedViewController</string> <key>NSExtensionPointIdentifier</key> <string>com.apple.share-services</string> </dict> </dict> </plist>
内容的提问来源于stack exchange,提问作者jho
相关产品推荐
相关产品推荐

