本文整理汇总了C#中NSUrlSession类的典型用法代码示例。如果您正苦于以下问题:C# NSUrlSession类的具体用法?C# NSUrlSession怎么用?C# NSUrlSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NSUrlSession类属于命名空间,在下文中一共展示了NSUrlSession类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: DidFinishDownloading
public override void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
{
if (!downloadTasks.ContainsKey(downloadTask.TaskIdentifier))
return;
var cachedTaskInfo = downloadTasks[downloadTask.TaskIdentifier];
try
{
OnFileDownloadProgress(cachedTaskInfo.Index, 100f);
var tmpLocation = location.Path;
var dirName = Path.GetDirectoryName(cachedTaskInfo.DestinationDiskPath);
if (!Directory.Exists(dirName))
Directory.CreateDirectory(dirName);
if (File.Exists(cachedTaskInfo.DestinationDiskPath))
File.Delete(cachedTaskInfo.DestinationDiskPath);
File.Move(tmpLocation, cachedTaskInfo.DestinationDiskPath);
OnFileDownloadedSuccessfully(cachedTaskInfo.Index, cachedTaskInfo.DestinationDiskPath);
CleanUpCachedTask(cachedTaskInfo);
}
catch (Exception exception)
{
OnError(cachedTaskInfo, new NSError(new NSString(exception.Message), 1));
}
}
开发者ID:raghurana,项目名称:NsUrlDownloadDemo,代码行数:30,代码来源:HttpFilesDownloadSession.cs
示例2: DidFinishEventsForBackgroundSession
public override void DidFinishEventsForBackgroundSession (NSUrlSession session)
{
var handler = AppDelegate.BackgroundSessionCompletionHandler;
AppDelegate.BackgroundSessionCompletionHandler = null;
if (handler != null) {
handler.Invoke ();
}
}
开发者ID:xamarindevelopervietnam,项目名称:XamarinFormsBackgrounding,代码行数:8,代码来源:CustomSessionDownloadDelegate.cs
示例3: DidFinishEventsForBackgroundSession
public override void DidFinishEventsForBackgroundSession(NSUrlSession session)
{
if (HttpService.BackgroundSessionCompletionHandler != null)
{
Action handler = HttpService.BackgroundSessionCompletionHandler;
HttpService.BackgroundSessionCompletionHandler = null;
handler.Invoke();
}
}
开发者ID:StorozhenkoDmitry,项目名称:SimpleHttp,代码行数:9,代码来源:DownloadDelegate.cs
示例4: NativeMessageHandler
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification)
{
session = NSUrlSession.FromConfiguration(
NSUrlSessionConfiguration.DefaultSessionConfiguration,
new DataTaskDelegate(this), null);
this.throwOnCaptiveNetwork = throwOnCaptiveNetwork;
this.customSSLVerification = customSSLVerification;
}
开发者ID:rickykaare,项目名称:ModernHttpClient,代码行数:9,代码来源:NSUrlSessionHandler.cs
示例5: DidCompleteWithError
public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
if (error != null)
{
TaskDescription description = JsonParser.ParseTaskDescription(task.TaskDescription).Result;
OnDownloadCompleted("", task.TaskDescription ?? "", error.LocalizedDescription);
}
}
开发者ID:StorozhenkoDmitry,项目名称:SimpleHttp,代码行数:9,代码来源:DownloadDelegate.cs
示例6: Init
public void Init(string sessionId, string url, TransferTaskMode mode)
{
using (var configuration = NSUrlSessionConfiguration.BackgroundSessionConfiguration(sessionId))
{
_mode = mode;
_sessionId = sessionId;
_url = url;
session = NSUrlSession.FromConfiguration(configuration);
}
}
开发者ID:garymedina,项目名称:OfflineSyncApp,代码行数:10,代码来源:BackgroundTransferTaskIOS.cs
示例7: DidCompleteWithError
public override void DidCompleteWithError (NSUrlSession session, NSUrlSessionTask task, NSError error)
{
if (error == null) {
return;
}
Console.WriteLine ("DidCompleteWithError - Task: {0}, Error: {1}", task.TaskIdentifier, error);
task.Cancel ();
}
开发者ID:xamarindevelopervietnam,项目名称:XamarinFormsBackgrounding,代码行数:10,代码来源:CustomSessionDownloadDelegate.cs
示例8: DidWriteData
public override void DidWriteData(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, long bytesWritten, long totalBytesWritten, long totalBytesExpectedToWrite)
{
if (downloadTasks.ContainsKey(downloadTask.TaskIdentifier))
{
var index = downloadTasks[downloadTask.TaskIdentifier].Index;
var progress = totalBytesWritten/(float) totalBytesExpectedToWrite;
OnFileDownloadProgress(index, progress);
}
}
开发者ID:raghurana,项目名称:NsUrlDownloadDemo,代码行数:10,代码来源:HttpFilesDownloadSession.cs
示例9: HttpFilesDownloadSession
public HttpFilesDownloadSession(string uniqueSessionId)
{
var config = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(uniqueSessionId);
downloadSession = NSUrlSession.FromConfiguration(config, this, new NSOperationQueue());
downloadTasks = new Dictionary<nuint, DownloadTaskInfo>();
DownloadQueue = new ObservableCollection<DownloadFileInfo>();
DownloadQueue.CollectionChanged += DownloadQueueOnCollectionChanged;
}
开发者ID:raghurana,项目名称:NsUrlDownloadDemo,代码行数:10,代码来源:HttpFilesDownloadSession.cs
示例10: DidWriteData
public override void DidWriteData (NSUrlSession session, NSUrlSessionDownloadTask downloadTask, long bytesWritten, long totalBytesWritten, long totalBytesExpectedToWrite)
{
Console.WriteLine ("Set Progress");
if (downloadTask == controller.downloadTask) {
float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
Console.WriteLine (string.Format ("DownloadTask: {0} progress: {1}", downloadTask, progress));
InvokeOnMainThread( () => {
controller.ProgressView.Progress = progress;
});
}
}
开发者ID:g7steve,项目名称:monotouch-samples,代码行数:11,代码来源:SimpleBackgroundTransferViewController.cs
示例11: NativeMessageHandler
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null)
{
session = NSUrlSession.FromConfiguration(
NSUrlSessionConfiguration.DefaultSessionConfiguration,
new DataTaskDelegate(this), null);
this.throwOnCaptiveNetwork = throwOnCaptiveNetwork;
this.customSSLVerification = customSSLVerification;
this.DisableCaching = false;
}
开发者ID:AMcKane,项目名称:ModernHttpClient,代码行数:11,代码来源:NSUrlSessionHandler.cs
示例12: DidFinishDownloading
public override void DidFinishDownloading (NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
{
CopyDownloadedImage (location);
var message = new DownloadFinishedMessage () {
FilePath = targetFileName,
Url = downloadTask.OriginalRequest.Url.AbsoluteString
};
MessagingCenter.Send<DownloadFinishedMessage> (message, "DownloadFinishedMessage");
}
开发者ID:xamarindevelopervietnam,项目名称:XamarinFormsBackgrounding,代码行数:12,代码来源:CustomSessionDownloadDelegate.cs
示例13: DidWriteData
public override void DidWriteData (NSUrlSession session, NSUrlSessionDownloadTask downloadTask, long bytesWritten, long totalBytesWritten, long totalBytesExpectedToWrite)
{
float percentage = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
var message = new DownloadProgressMessage () {
BytesWritten = bytesWritten,
TotalBytesExpectedToWrite = totalBytesExpectedToWrite,
TotalBytesWritten = totalBytesWritten,
Percentage = percentage
};
MessagingCenter.Send<DownloadProgressMessage> (message, "DownloadProgressMessage");
}
开发者ID:xamarindevelopervietnam,项目名称:XamarinFormsBackgrounding,代码行数:13,代码来源:CustomSessionDownloadDelegate.cs
示例14: InitializeSession
void InitializeSession ()
{
using (var sessionConfig = UIDevice.CurrentDevice.CheckSystemVersion (8, 0)
? NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration (sessionId)
: NSUrlSessionConfiguration.BackgroundSessionConfiguration (sessionId)) {
sessionConfig.AllowsCellularAccess = true;
sessionConfig.NetworkServiceType = NSUrlRequestNetworkServiceType.Default;
sessionConfig.HttpMaximumConnectionsPerHost = 2;
var sessionDelegate = new CustomSessionDownloadDelegate (targetFilename);
this.session = NSUrlSession.FromConfiguration (sessionConfig, sessionDelegate, null);
}
}
开发者ID:xamarindevelopervietnam,项目名称:XamarinFormsBackgrounding,代码行数:15,代码来源:Downloader.cs
示例15: NetworkUrlSession
public NetworkUrlSession(string identifier)
{
_transfers = new List<NetworkUrlSessionTransfer> (10);
_urlSessionDelegate = new NetworkUrlSessionDelegate (this);
NSUrlSessionConfiguration c = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration (identifier);
NSOperationQueue q = new NSOperationQueue ();
// only allow 1 file transfer at a time; SAMCTODO: not sure if this is best..
q.MaxConcurrentOperationCount = 1;
_urlSession = NSUrlSession.FromConfiguration (c, _urlSessionDelegate, q);
_urlSession.GetTasks( HandleNSUrlSessionPendingTasks );
IBackgroundUrlEventDispatcher appDelegate = UIApplication.SharedApplication.Delegate as IBackgroundUrlEventDispatcher;
appDelegate.HandleEventsForBackgroundUrlEvent += HandleEventsForBackgroundUrl;
}
开发者ID:ruiamorimwDev,项目名称:skobbler-mono-bindings,代码行数:15,代码来源:NetworkUrlSession.cs
示例16: DidCompleteWithError
/// <summary>
/// Very misleading method name. Gets called if a download is done. Does not necessarily indicate an error
/// unless the NSError parameter is not null.
/// </summary>
public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
if (error == null)
{
return;
}
Console.WriteLine ("DidCompleteWithError - Task: {0}, Error: {1}", task.TaskIdentifier, error);
var downloadInfo = AppDelegate.GetDownloadInfoByTaskId (task.TaskIdentifier);
if (downloadInfo != null)
{
downloadInfo.Reset ();
}
task.Cancel ();
this.InvokeOnMainThread (() => this.controller.TableView.ReloadData());
}
开发者ID:flolovebit,项目名称:xamarin-evolve-2014,代码行数:21,代码来源:SessionDelegate.cs
示例17: DidCompleteWithError
public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
var fileUpload = this.Manager.GetUploadByTask (task);
Debug.Assert(fileUpload != null, "Could not find FileUpload object for task!");
if(fileUpload == null)
{
return;
}
Debug.Assert(fileUpload.State == FileUpload.STATE.Started || fileUpload.State == FileUpload.STATE.Stopping, "Upload is in invalid state!");
var urlErrorCode = NSUrlError.Unknown;
if(error != null)
{
Enum.TryParse<NSUrlError>(error.Code.ToString(), out urlErrorCode);
}
if(error == null)
{
fileUpload.Progress = 0f;
fileUpload.Error = null;
fileUpload.Response = fileUpload.UploadTask.Response as NSHttpUrlResponse;
fileUpload.UploadTask = null;
fileUpload.State = FileUpload.STATE.Uploaded;
Console.WriteLine($"Completed upload {fileUpload}.");
}
else if(urlErrorCode == NSUrlError.Cancelled)
{
fileUpload.Error = null;
fileUpload.UploadTask = null;
fileUpload.State = FileUpload.STATE.Stopped;
}
else
{
// Upload was stopped by the network.
fileUpload.Error = error;
fileUpload.UploadTask = null;
fileUpload.State = FileUpload.STATE.Failed;
Console.WriteLine($"Upload failed: {fileUpload}");
}
fileUpload.IsStateValid();
this.Manager.ActiveUploads.OnCollectionChanged();
}
开发者ID:Krumelur,项目名称:BackgroundUploadDemo,代码行数:46,代码来源:FileUploadDelegate.cs
示例18: StartAsync
public async Task StartAsync()
{
Debug.Assert(this.session == null, "Session already initialized!");
// Create our view of the world based on the on-disk data structures.
this.RestoreAllUploadsInWorkDirectory();
NSUrlSessionConfiguration config;
if (!string.IsNullOrWhiteSpace(this.SessionIdentifier))
{
Console.WriteLine($"Creating background session with identifier '{this.SessionIdentifier}'");
config = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(this.SessionIdentifier);
}
else
{
Console.WriteLine("Creating ephemeral session configuration.");
config = NSUrlSessionConfiguration.EphemeralSessionConfiguration;
}
// In our case we don't want any (NSURLCache-level) caching to get in the way
// of our tests, so we always disable the cache.
config.RequestCachePolicy = NSUrlRequestCachePolicy.ReloadIgnoringCacheData;
config.Discretionary = true;
this.session = NSUrlSession.FromConfiguration(config, new FileUploadDelegate(this), NSOperationQueue.MainQueue);
config.Dispose();
// This is where things get wacky. From the point that we create the session (in the previous
// line) to the point where the block passed to -getTasksWithCompletionHandler: runs, we can
// be getting delegate callbacks for tasks whose corresponding upload objects are in the wrong
// state (specifically, the task property isn't set and, in some cases, the state might be wrong).
// A lot of the logic in -syncUploadTasks: and, especially -uploadForTask:, is designed to
// compensate for that oddity.
var activeTasks = await this.session.GetTasks2Async();
var activeUploadTasks = activeTasks.UploadTasks;
NSOperationQueue.MainQueue.AddOperation(() => {
this.SyncUploadTasks(activeUploadTasks);
});
Console.WriteLine("FileUploadManager did start.");
}
开发者ID:Krumelur,项目名称:BackgroundUploadDemo,代码行数:43,代码来源:FileUploadManager.cs
示例19: NativeMessageHandler
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null, SslProtocol? minimumSSLProtocol = null)
{
var configuration = NSUrlSessionConfiguration.DefaultSessionConfiguration;
// System.Net.ServicePointManager.SecurityProtocol provides a mechanism for specifying supported protocol types
// for System.Net. Since iOS only provides an API for a minimum and maximum protocol we are not able to port
// this configuration directly and instead use the specified minimum value when one is specified.
if (minimumSSLProtocol.HasValue) {
configuration.TLSMinimumSupportedProtocol = minimumSSLProtocol.Value;
}
session = NSUrlSession.FromConfiguration(
NSUrlSessionConfiguration.DefaultSessionConfiguration,
new DataTaskDelegate(this), null);
this.throwOnCaptiveNetwork = throwOnCaptiveNetwork;
this.customSSLVerification = customSSLVerification;
this.DisableCaching = false;
}
开发者ID:scoodah,项目名称:ModernHttpClient,代码行数:20,代码来源:NSUrlSessionHandler.cs
示例20: DidFinishDownloading
/// <summary>
/// Gets called if the download has been completed.
/// </summary>
public override void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
{
// The download location will be a file location.
var sourceFile = location.Path;
// Construct a destination file name.
var destFile = downloadTask.OriginalRequest.Url.AbsoluteString.Substring(downloadTask.OriginalRequest.Url.AbsoluteString.LastIndexOf("/") + 1);
Console.WriteLine ("DidFinishDownloading - Task: {0}, Source file: {1}", downloadTask.TaskIdentifier, sourceFile);
// Copy over to documents folder. Note that we must use NSFileManager here! File.Copy() will not be able to access the source location.
NSFileManager fileManager = NSFileManager.DefaultManager;
// Create the filename
var documentsFolderPath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
NSUrl destinationURL = NSUrl.FromFilename(Path.Combine(documentsFolderPath, destFile));
// Update download info object.
var downloadInfo = AppDelegate.GetDownloadInfoByTaskId (downloadTask.TaskIdentifier);
// Remove any existing file in our destination
NSError error;
fileManager.Remove(destinationURL, out error);
bool success = fileManager.Copy(sourceFile, destinationURL.Path, out error);
if (success)
{
location = destinationURL;
this.UpdateDownloadInfo (downloadInfo, DownloadInfo.STATUS.Completed, destinationURL);
} else
{
this.UpdateDownloadInfo (downloadInfo, DownloadInfo.STATUS.Cancelled, null);
Console.WriteLine ("Error during the copy: {0}", error.LocalizedDescription);
}
this.InvokeOnMainThread (() => this.controller.TableView.ReloadData());
}
开发者ID:flolovebit,项目名称:xamarin-evolve-2014,代码行数:39,代码来源:SessionDelegate.cs
注:本文中的NSUrlSession类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论