我们正在Xamarin开发移动应用程序。如何在iOS中完成下一步:
由于文件操作是特定于平台的,因此这里的接口定义为:
public interface IFileHelper
{
void DownloadFileAndSave(Models.DocumentModel document);
}
Android实施:
public class FileHelper : IFileHelper
{
// download file and view status in download manager
public void DownloadFileAndSave(Models.DocumentModel document)
{
DownloadManager dm = (DownloadManager)Android.App.Application.Context.GetSystemService(Context.DownloadService);
string url = WebApiUtils.GetBaseUrl() + string.Format("Api/v1/Dms/{0}", document.UniqueId);
DownloadManager.Request request = new Android.App.DownloadManager.Request(Android.Net.Uri.Parse(url)));
request.AddRequestHeader("Authorization", "Basic " + WebApiUtils.GetEncodedCredentials(Auth.Users.Current));
var downloadFile = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
string path = Path.Combine(downloadFile.AbsolutePath, document.FileName);
request.SetDestinationUri(Android.Net.Uri.FromFile(new Java.IO.File(path)));
request.SetMimeType(document.ContentType);
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
dm.Enqueue(request);
}
在Android文件中只是存储在文件系统上,并且文件资源管理器默认安装在任何Android上(即我的文件 - >设备存储 - >下载),该文件在文件&#39的默认应用程序中打开; s mime类型。 Android上的一切都很好。
Apple iOS实施:
public class FileHelper : IFileHelper
{
public void DownloadFileAndSave(Models.DocumentModel document)
{
WebClient webClient = new WebClient();
webClient.Headers.Add(HttpRequestHeader.Authorization, "Basic " + WebApiUtils.GetEncodedCredentials(Auth.Users.Current));
webClient.DownloadDataAsync(new System.Uri(WebApiUtils.GetBaseUrl() + string.Format(Consts.ApiUrls.GetDocument, document.UniqueId)));
webClient.DownloadDataCompleted += (sender, e) =>
{
byte[] content = e.Result;
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), document.FileName);
// doesn't throw exception therefore saved ok
File.WriteAllBytes(path, content);
Uri uri = new Uri(String.Format("file://{0}", path));
// doesn't work.
Device.OpenUri(uri);
};
}
}
是否有其他方法可以在默认应用程序中打开下载的文件。如果我打开网址,例如http://example.com/files/file1.png它会在Safari中打开文件,但我无法将Authorization: Basic
标题放在Device.OpenUri
中。
我了解了Load Non-Web Documents with WebView,但您必须将每个文件构建为BundleResource
。
答案 0 :(得分:0)
正如Code Warrior评论的那样,链接上发布了一种方法:https://forums.xamarin.com/discussion/36964/why-is-it-that-nothing-is-working-to-open-an-existing-local-pdf-file-in-the-ios-portion-of-my-pcl。
但保存图片操作无效,其他一切似乎都有效。
public void DownloadFileAndSave(Models.DocumentModel document)
{
WebClient webClient = new WebClient();
webClient.Headers.Add(HttpRequestHeader.Authorization, "Basic " + WebApiUtils.GetEncodedCredentials(Auth.Users.Current));
string tempPath = Path.GetTempPath();
string localFilename = Path.GetFileName(document.FileName);
string localPath = Path.Combine(tempPath, localFilename);
webClient.DownloadFileCompleted += (sender, e) =>
{
Device.BeginInvokeOnMainThread(() =>
{
QLPreviewItemFileSystem prevItem = new QLPreviewItemFileSystem(localFilename, localPath); // ql = quick look
QLPreviewController previewController = new QLPreviewController()
{
DataSource = new PreviewControllerDS(prevItem)
};
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(previewController, true, null);
});
};
// download file
Uri uri = new System.Uri(WebApiUtils.GetBaseUrl() + string.Format(Consts.ApiUrls.GetDocument, document.UniqueId));
webClient.DownloadFileAsync(uri, localPath);
}
当触发保存图片时,我会收到下一个错误:
2017-10-03 13:45:56.797 MyApp.iOS [477:61030] 视频 /private/var/mobile/Containers/Data/Application/33D7139A-53E0-4A2E-8C78-D3D13A2259B0/tmp/water-h2o-md.png 无法保存到已保存的相册:错误 Domain = AVFoundationErrorDomain Code = -11828"无法打开" UserInfo = {NSUnderlyingError = 0x1c0445d60 {错误 Domain = NSOSStatusErrorDomain Code = -12847"(null)"}, NSLocalizedFailureReason =不支持此媒体格式。 NSURL =文件:///private/var/mobile/Containers/Data/Application/33D7139A-53E0-4A2E-8C78-D3D13A2259B0/tmp/water-h2o-md.png, NSLocalizedDescription =无法打开}
iOS将图片视为视频吗?这是iOS上的错误还是我缺少的东西。
<强>更新强>
事实证明,Info.plist文件中缺少下一个权限:
<key>NSPhotoLibraryUsageDescription</key>
<string>Application needs to access photos</string>
<!-- for iOS 11 -->
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Application needs to access photos</string>
现在保存图片操作正常。但严重的是Apple可以返回比Video image.jpg cannot be saved ...