-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
653 lines (582 loc) · 39.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<!DOCTYPE html>
<html lang="en">
<head>
<title>PCloudSDKSwift Reference</title>
<link rel="stylesheet" type="text/css" href="css/jazzy.css" />
<link rel="stylesheet" type="text/css" href="css/highlight.css" />
<meta charset='utf-8'>
<script src="js/jquery.min.js" defer></script>
<script src="js/jazzy.js" defer></script>
</head>
<body>
<a title="PCloudSDKSwift Reference"></a>
<header>
<div class="content-wrapper">
<p><a href="index.html">PCloudSDKSwift 3.0.0 Docs</a> (83% documented)</p>
<p class="header-right"><a href="https://github.com/pcloud/pcloud-sdk-swift"><img src="img/gh.png"/>View on GitHub</a></p>
</div>
</header>
<div class="content-wrapper">
<p id="breadcrumbs">
<a href="index.html">PCloudSDKSwift Reference</a>
<img id="carat" src="img/carat.png" />
PCloudSDKSwift Reference
</p>
</div>
<div class="content-wrapper">
<nav class="sidebar">
<ul class="nav-groups">
<li class="nav-group-name">
<a href="Classes.html">Classes</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Classes/AnyCancellationToken.html">AnyCancellationToken</a>
</li>
<li class="nav-group-task">
<a href="Classes/CallTask.html">CallTask</a>
</li>
<li class="nav-group-task">
<a href="Classes/DownloadTask.html">DownloadTask</a>
</li>
<li class="nav-group-task">
<a href="Classes/PCloudClient.html">PCloudClient</a>
</li>
<li class="nav-group-task">
<a href="Classes/URLSessionBasedCallOperation.html">URLSessionBasedCallOperation</a>
</li>
<li class="nav-group-task">
<a href="Classes/URLSessionBasedDownloadOperation.html">URLSessionBasedDownloadOperation</a>
</li>
<li class="nav-group-task">
<a href="Classes/URLSessionBasedNetworkOperation.html">URLSessionBasedNetworkOperation</a>
</li>
<li class="nav-group-task">
<a href="Classes/URLSessionBasedUploadOperation.html">URLSessionBasedUploadOperation</a>
</li>
<li class="nav-group-task">
<a href="Classes/URLSessionEventHub.html">URLSessionEventHub</a>
</li>
<li class="nav-group-task">
<a href="Classes/UploadTask.html">UploadTask</a>
</li>
<li class="nav-group-task">
<a href="Classes/WebViewControllerMobile.html">WebViewControllerMobile</a>
</li>
<li class="nav-group-task">
<a href="Classes/WebViewControllerPresenterMobile.html">WebViewControllerPresenterMobile</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Enums.html">Enumerations</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Enums/APIServerRegion.html">APIServerRegion</a>
</li>
<li class="nav-group-task">
<a href="Enums/CallError.html">CallError</a>
</li>
<li class="nav-group-task">
<a href="Enums/Content.html">Content</a>
</li>
<li class="nav-group-task">
<a href="Enums/Content/Metadata.html">– Metadata</a>
</li>
<li class="nav-group-task">
<a href="Enums/NetworkOperationError.html">NetworkOperationError</a>
</li>
<li class="nav-group-task">
<a href="Enums/NetworkOperationState.html">NetworkOperationState</a>
</li>
<li class="nav-group-task">
<a href="Enums/PCloud.html">PCloud</a>
</li>
<li class="nav-group-task">
<a href="Enums/URLScheme.html">URLScheme</a>
</li>
<li class="nav-group-task">
<a href="Enums/URLSessionTaskBuilder.html">URLSessionTaskBuilder</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Extensions.html">Extensions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Extensions/Dictionary.html">Dictionary</a>
</li>
<li class="nav-group-task">
<a href="Extensions/String.html">String</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Functions.html">Functions</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Functions.html#/s:14PCloudSDKSwift2eeoiySbAA4CallV7CommandV_AFtF">==(_:_:)</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Protocols.html">Protocols</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Protocols/Authenticator.html">Authenticator</a>
</li>
<li class="nav-group-task">
<a href="Protocols/CallOperation.html">CallOperation</a>
</li>
<li class="nav-group-task">
<a href="Protocols/Cancellable.html">Cancellable</a>
</li>
<li class="nav-group-task">
<a href="Protocols/DownloadOperation.html">DownloadOperation</a>
</li>
<li class="nav-group-task">
<a href="Protocols/HostProvider.html">HostProvider</a>
</li>
<li class="nav-group-task">
<a href="Protocols/NetworkOperation.html">NetworkOperation</a>
</li>
<li class="nav-group-task">
<a href="Protocols/OAuthAuthorizationFlowView.html">OAuthAuthorizationFlowView</a>
</li>
<li class="nav-group-task">
<a href="Protocols/PCloudAPIMethod.html">PCloudAPIMethod</a>
</li>
<li class="nav-group-task">
<a href="Protocols/Parser.html">Parser</a>
</li>
<li class="nav-group-task">
<a href="Protocols/URLSessionObserver.html">URLSessionObserver</a>
</li>
<li class="nav-group-task">
<a href="Protocols/UploadOperation.html">UploadOperation</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a href="Structs.html">Structures</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a href="Structs/AudioMetadataParser.html">AudioMetadataParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/Call.html">Call</a>
</li>
<li class="nav-group-task">
<a href="Structs/Call/Command.html">– Command</a>
</li>
<li class="nav-group-task">
<a href="Structs/Call/Request.html">– Request</a>
</li>
<li class="nav-group-task">
<a href="Structs/ContentListParser.html">ContentListParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/Download.html">Download</a>
</li>
<li class="nav-group-task">
<a href="Structs/Download/Request.html">– Request</a>
</li>
<li class="nav-group-task">
<a href="Structs/File.html">File</a>
</li>
<li class="nav-group-task">
<a href="Structs/File/Media.html">– Media</a>
</li>
<li class="nav-group-task">
<a href="Structs/File/Metadata.html">– Metadata</a>
</li>
<li class="nav-group-task">
<a href="Structs/FileIconParser.html">FileIconParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/FileLink.html">FileLink</a>
</li>
<li class="nav-group-task">
<a href="Structs/FileLink/Metadata.html">– Metadata</a>
</li>
<li class="nav-group-task">
<a href="Structs/FileLinkMetadataParser.html">FileLinkMetadataParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/FileMetadataParser.html">FileMetadataParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/Folder.html">Folder</a>
</li>
<li class="nav-group-task">
<a href="Structs/Folder/Permissions.html">– Permissions</a>
</li>
<li class="nav-group-task">
<a href="Structs/Folder/Ownership.html">– Ownership</a>
</li>
<li class="nav-group-task">
<a href="Structs/Folder/Metadata.html">– Metadata</a>
</li>
<li class="nav-group-task">
<a href="Structs/FolderMetadataParser.html">FolderMetadataParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/ImageMetadataParser.html">ImageMetadataParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/NullError.html">NullError</a>
</li>
<li class="nav-group-task">
<a href="Structs/OAuth.html">OAuth</a>
</li>
<li class="nav-group-task">
<a href="Structs/OAuth/Result.html">– Result</a>
</li>
<li class="nav-group-task">
<a href="Structs/OAuth/User.html">– User</a>
</li>
<li class="nav-group-task">
<a href="Structs/OAuth/Error.html">– Error</a>
</li>
<li class="nav-group-task">
<a href="Structs/OAuthAccessTokenBasedAuthenticator.html">OAuthAccessTokenBasedAuthenticator</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI.html">PCloudAPI</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/AuthError.html">– AuthError</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/PermissionError.html">– PermissionError</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/Error.html">– Error</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/UserInfo.html">– UserInfo</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/ListFolder.html">– ListFolder</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/CreateFolder.html">– CreateFolder</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/RenameFolder.html">– RenameFolder</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/MoveFolder.html">– MoveFolder</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/CopyFolder.html">– CopyFolder</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/DeleteFolderRecursive.html">– DeleteFolderRecursive</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/UploadFile.html">– UploadFile</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/CreateUpload.html">– CreateUpload</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/GetUploadInfo.html">– GetUploadInfo</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/WriteToUpload.html">– WriteToUpload</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/SaveUpload.html">– SaveUpload</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/CopyFile.html">– CopyFile</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/RenameFile.html">– RenameFile</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/MoveFile.html">– MoveFile</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/DeleteFile.html">– DeleteFile</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/GetFileLink.html">– GetFileLink</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/GetThumbnailLink.html">– GetThumbnailLink</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPI/GetThumbnailsLinks.html">– GetThumbnailsLinks</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPICallTaskBuilder.html">PCloudAPICallTaskBuilder</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPIDownloadTaskBuilder.html">PCloudAPIDownloadTaskBuilder</a>
</li>
<li class="nav-group-task">
<a href="Structs/PCloudAPIUploadTaskBuilder.html">PCloudAPIUploadTaskBuilder</a>
</li>
<li class="nav-group-task">
<a href="Structs/URLSessionBasedNetworkOperationUtilities.html">URLSessionBasedNetworkOperationUtilities</a>
</li>
<li class="nav-group-task">
<a href="Structs/Upload.html">Upload</a>
</li>
<li class="nav-group-task">
<a href="Structs/Upload/Request.html">– Request</a>
</li>
<li class="nav-group-task">
<a href="Structs/UploadInfo.html">UploadInfo</a>
</li>
<li class="nav-group-task">
<a href="Structs/UploadInfoParser.html">UploadInfoParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/User.html">User</a>
</li>
<li class="nav-group-task">
<a href="Structs/User/Metadata.html">– Metadata</a>
</li>
<li class="nav-group-task">
<a href="Structs/UserMetadataParser.html">UserMetadataParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/VideoMetadataParser.html">VideoMetadataParser</a>
</li>
<li class="nav-group-task">
<a href="Structs/VoidCancellationToken.html">VoidCancellationToken</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="main-content">
<section>
<section class="section">
<h1 id='pcloud-swift-sdk' class='heading'>pCloud Swift SDK</h1>
<p>The official pCloud Swift SDK for iOS and macOS for integration with the <a href="https://docs.pcloud.com/">pCloud API</a>.
You can find the full documentation <a href="https://pcloud.github.io/pcloud-sdk-swift">here</a>.</p>
<hr>
<h2 id='table-of-contents' class='heading'>Table of Contents</h2>
<ul>
<li><a href="#migration-to-v3">Migration to v3</a></li>
<li><a href="#system-requirements">System requirements</a></li>
<li><a href="#get-started">Get started</a>
<ul>
<li><a href="#register-your-application">Register your application</a></li>
<li><a href="#set-up-your-application">Set up your application</a></li>
</ul></li>
<li><a href="#install-the-sdk">Install the SDK</a>
<ul>
<li><a href="#cocoapods">CocoaPods</a></li>
<li><a href="#carthage">Carthage</a></li>
</ul></li>
<li><a href="#initializing-the-sdk">Initializing the SDK</a>
<ul>
<li><a href="#using-the-authorization-flow">Using the authorization flow</a></li>
<li><a href="#manually-creating-a-client">Manually creating a client</a></li>
</ul></li>
<li><a href="#making-api-requests">Making API requests</a>
<ul>
<li><a href="#working-with-the-network-tasks">Working with the network tasks</a></li>
<li><a href="#handling-api-errors">Handling API errors</a></li>
</ul></li>
<li><a href="#examples">Examples</a></li>
<li><a href="#documentation">Documentation</a></li>
</ul>
<hr>
<h2 id='migration-to-v3' class='heading'>Migration to v3</h2>
<p>For instructions on how to migrate to v3 of the SDK, please refer to the release notes of v3.0.0 in the source repository.</p>
<hr>
<h2 id='system-requirements' class='heading'>System requirements</h2>
<ul>
<li>iOS 9.0+</li>
<li>macOS 10.11+</li>
<li>Xcode 10.2+</li>
</ul>
<hr>
<h2 id='get-started' class='heading'>Get started</h2>
<h3 id='register-your-application' class='heading'>Register your application</h3>
<p>In order to use this SDK, you have to register your application in the <a href="https://docs.pcloud.com/my_apps/">pCloud App Console</a>. Take note of the app key in the main page of your application once you create it.</p>
<h3 id='set-up-your-application' class='heading'>Set up your application</h3>
<p>The SDK uses an OAuth 2.0 access token to authorize requests to the pCloud API. You can obtain a token using the SDK’s authorization flow. To allow the SDK to do that, find the ‘Redirect URIs’ section in your application configuration page and add a URI with the following format: <code>pclsdk-w-YOUR_APP_KEY://oauth2redirect</code> where <code>YOUR_APP_KEY</code> is the app key from your app console.</p>
<hr>
<h2 id='install-the-sdk' class='heading'>Install the SDK</h2>
<p>You can integrate the SDK into your project using any of the following methods:</p>
<h3 id='cocoapods' class='heading'>CocoaPods</h3>
<p><a href="http://cocoapods.org">CocoaPods</a> is a dependency manager for Swift and Objective-C Cocoa projects. If you do not already use CocoaPods, you can check out how to get started with it <a href="http://guides.cocoapods.org/using/getting-started.html">here</a>.</p>
<p>First you should install CocoaPods:</p>
<pre class="highlight shell"><code><span class="nv">$ </span>gem <span class="nb">install </span>cocoapods
</code></pre>
<p>Then navigate to your project root and run <code>pod init</code>. This will create a file called <code>Podfile</code>. Open it and add <code>pod 'PCloudSDKSwift'</code> to your target. Your Podfile should look something like this.</p>
<pre class="highlight ruby"><code><span class="n">use_frameworks!</span>
<span class="n">target</span> <span class="s1">'YOUR_TARGET_NAME'</span> <span class="k">do</span>
<span class="n">pod</span> <span class="s1">'PCloudSDKSwift'</span>
<span class="k">end</span>
</code></pre>
<p>Then run the following command to install the SDK and integrate it into your project:</p>
<pre class="highlight shell"><code>pod <span class="nb">install</span>
</code></pre>
<p>Once the SDK is integrated into your project, you can pull SDK updates using the following command:</p>
<pre class="highlight shell"><code>pod update
</code></pre>
<h3 id='carthage' class='heading'>Carthage</h3>
<p><a href="https://github.com/Carthage/Carthage">Carthage</a> is a simple, decentralized dependency manager for Cocoa. If you don’t already use Carthage, you can check out how you can install it <a href="https://github.com/Carthage/Carthage#installing-carthage">here</a>.</p>
<p>To install the pCloud Swift SDK via Carthage, you need to create a <code>Cartfile</code> in your project (This file lists the frameworks you’d like to use in your project.) with the following contents:</p>
<pre class="highlight plaintext"><code>github "https://github.com/pcloud/pcloud-sdk-swift"
</code></pre>
<p>Then, run the following command (This will fetch dependencies into a <code>Carthage/Checkouts</code> folder and build each one):</p>
<h5 id='ios' class='heading'>iOS</h5>
<pre class="highlight shell"><code>carthage update <span class="nt">--platform</span> iOS
</code></pre>
<p>In the Project Navigator in Xcode, select your project, then select your target, then navigate to <strong>General</strong> > <strong>Linked Frameworks and Libraries</strong>, and drag and drop <code>PCloudSDKSwift.framework</code> from <code>Carthage/Build/iOS</code>.</p>
<p>Then, on your application targets’ <strong>Build Phases</strong> settings tab, click the <strong>+</strong> button and choose <strong>New Run Script Phase</strong>. In the newly-created <strong>Run Script</strong> section, add the following code to the script body area:</p>
<pre class="highlight shell"><code>/usr/local/bin/carthage copy-frameworks
</code></pre>
<p>Then navigate to the <strong>Input Files</strong> section and add the path to the framework:</p>
<pre class="highlight shell"><code><span class="si">$(</span>SRCROOT<span class="si">)</span>/Carthage/Build/iOS/PCloudSDKSwift.framework
</code></pre>
<h5 id='macos' class='heading'>macOS</h5>
<pre class="highlight shell"><code>carthage update <span class="nt">--platform</span> Mac
</code></pre>
<p>In the Project Navigator in Xcode, select your project, and then navigate to <strong>General</strong> > <strong>Linked Frameworks and Libraries</strong>, then drag and drop <code>PCloudSDKSwift.framework</code> from <code>Carthage/Build/Mac</code>.</p>
<p>Then, on your application target’s <strong>Build Phases</strong> settings tab, click the <strong>+</strong> icon and choose <strong>New Copy Files Phase</strong>. In the newly-created <strong>Copy Files</strong> section, click the <strong>Destination</strong> drop-down menu and select <strong>Products Director</strong>, then drag and drop <code>PCloudSDKSwift.framework.dSYM</code> from <code>Carthage/Build/Mac</code>.</p>
<hr>
<h2 id='initializing-the-sdk' class='heading'>Initializing the SDK</h2>
<p>Once integrated into your project, the SDK needs to authenticate a user in order to make API calls.</p>
<h3 id='using-the-authorization-flow' class='heading'>Using the authorization flow</h3>
<p>The SDK has a pre-defined flow for obtaining a user. It attempts to authenticate the user via a <code>ASWebAuthenticationSession</code> if the current OS version allows it. Otherwise it opens a web view inside your app and loads the pCloud authorization page where the user can log in and authorize your app. To use the authorization flow:</p>
<h4 id='initialize-the-code-pcloud-code-instance' class='heading'>Initialize the <code><a href="Enums/PCloud.html">PCloud</a></code> instance</h4>
<p>In the app delegate:</p>
<h5 id='ios' class='heading'>iOS</h5>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PCloudSDKSwift</span>
<span class="kd">func</span> <span class="nf">application</span><span class="p">(</span><span class="n">_</span> <span class="nv">application</span><span class="p">:</span> <span class="kt">UIApplication</span><span class="p">,</span> <span class="n">didFinishLaunchingWithOptions</span> <span class="nv">launchOptions</span><span class="p">:</span> <span class="p">[</span><span class="kt">UIApplicationLaunchOptionsKey</span><span class="p">:</span> <span class="kt">Any</span><span class="p">]?)</span> <span class="o">-></span> <span class="kt">Bool</span> <span class="p">{</span>
<span class="kt">PCloud</span><span class="o">.</span><span class="nf">setUp</span><span class="p">(</span><span class="nv">withAppKey</span><span class="p">:</span> <span class="s">"YOUR_APP_KEY"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
<h5 id='macos' class='heading'>macOS</h5>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PCloudSDKSwift</span>
<span class="kd">func</span> <span class="nf">applicationDidFinishLaunching</span><span class="p">(</span><span class="n">_</span> <span class="nv">notification</span><span class="p">:</span> <span class="kt">Notification</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">PCloud</span><span class="o">.</span><span class="nf">setUp</span><span class="p">(</span><span class="nv">withAppKey</span><span class="p">:</span> <span class="s">"YOUR_APP_KEY"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
<hr>
<h4 id='perform-the-authorization-flow' class='heading'>Perform the authorization flow</h4>
<p>To start the authorization flow, call <code><a href="Enums/PCloud.html#/s:14PCloudSDKSwift0A0O9authorize4with_ySo16UIViewControllerC_yAA5OAuthV6ResultOctFZ">PCloud.authorize(with:_:)</a></code> and provide a view controller and a block to be invoked once authorization completes or is cancelled by the user. The view controller is automatically dismissed before the completion block is called.</p>
<p>From your view controller:</p>
<h5 id='ios' class='heading'>iOS</h5>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PCloudSDKSwift</span>
<span class="c1">// Inside a UIViewController subclass.</span>
<span class="kd">func</span> <span class="nf">logInButtonTapped</span><span class="p">(</span><span class="n">_</span> <span class="nv">sender</span><span class="p">:</span> <span class="kt">UIButton</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">PCloud</span><span class="o">.</span><span class="nf">authorize</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="k">if</span> <span class="k">case</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="o">=</span> <span class="n">result</span> <span class="p">{</span>
<span class="c1">// You can make calls via the SDK. </span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
<p>This will either attempt to authenticate using <code>ASWebAuthenticationSession</code> or will present a view controller with a web view from the view controller passed to the method.</p>
<h5 id='macos' class='heading'>macOS</h5>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PCloudSDKSwift</span>
<span class="c1">// Inside an NSViewController subclass.</span>
<span class="kd">func</span> <span class="nf">logInButtonTapped</span><span class="p">(</span><span class="n">_</span> <span class="nv">sender</span><span class="p">:</span> <span class="kt">NSButton</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">PCloud</span><span class="o">.</span><span class="nf">authorize</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="k">if</span> <span class="k">case</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="o">=</span> <span class="n">result</span> <span class="p">{</span>
<span class="c1">// You can make calls via the SDK. </span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
<p>This will either attempt to authenticate using <code>ASWebAuthenticationSession</code> or will present a view controller with a web view as a sheet from the view controller passed to the method.</p>
<hr>
<p>Once <code><a href="Enums/PCloud.html#/s:14PCloudSDKSwift0A0O9authorize4with_ySo16UIViewControllerC_yAA5OAuthV6ResultOctFZ">PCloud.authorize(with:_:)</a></code> finishes successfully, you can start making API calls via a global <code><a href="Classes/PCloudClient.html">PCloudClient</a></code> instance accessible via <code><a href="Enums/PCloud.html#/s:14PCloudSDKSwift0A0O12sharedClientAA0aD0CSgvpZ">PCloud.sharedClient</a></code>. Furthermore, your access token is stored in the device’s keychain, so the next time your app is launched, the shared client instance will be initialized inside the <code><a href="Enums/PCloud.html#/s:14PCloudSDKSwift0A0O5setUp10withAppKeyySS_tFZ">PCloud.setUp(withAppKey:)</a></code> call.</p>
<h3 id='manually-creating-a-client' class='heading'>Manually creating a client</h3>
<p>This is a more flexible approach to using the SDK. However, it requires you to do a bit more work. <strong>Using this approach also delegates management of the access token to you</strong>.
You can manually create a <code><a href="Classes/PCloudClient.html">PCloudClient</a></code> instance with an access token. Manually managing the lifetime of this instance might be a lot more convenient for you in certain cases. To request an access token without automatically initializing the shared client instance:</p>
<pre class="highlight swift"><code><span class="kt">OAuth</span><span class="o">.</span><span class="nf">performAuthorizationFlow</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="n">anchor</span><span class="p">,</span> <span class="nv">appKey</span><span class="p">:</span> <span class="s">"YOUR_APP_KEY"</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="k">if</span> <span class="k">case</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="k">let</span> <span class="nv">user</span><span class="p">)</span> <span class="o">=</span> <span class="n">result</span> <span class="p">{</span>
<span class="k">let</span> <span class="nv">client</span> <span class="o">=</span> <span class="kt">PCloud</span><span class="o">.</span><span class="nf">createClient</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="n">user</span><span class="p">)</span>
<span class="c1">// Use the client.</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
<p>where <code>anchor</code> would be an instance of <code>UIWindow</code> on iOS or <code>NSWindow</code> on macOS. This method will attempt to authenticate via a <code>ASWebAuthenticationSession</code>, which is the recommended way of authenticating. It requires, however, iOS 13 / macOS 10.15. Another option is to use:</p>
<pre class="highlight swift"><code><span class="kt">OAuth</span><span class="o">.</span><span class="nf">performAuthorizationFlow</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="n">view</span><span class="p">,</span> <span class="nv">appKey</span><span class="p">:</span> <span class="s">"YOUR_APP_KEY"</span><span class="p">)</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="k">if</span> <span class="k">case</span> <span class="o">.</span><span class="nf">success</span><span class="p">(</span><span class="k">let</span> <span class="nv">user</span><span class="p">)</span> <span class="o">=</span> <span class="n">result</span> <span class="p">{</span>
<span class="k">let</span> <span class="nv">client</span> <span class="o">=</span> <span class="kt">PCloud</span><span class="o">.</span><span class="nf">createClient</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="n">user</span><span class="p">)</span>
<span class="c1">// Use the client.</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
<p>where <code>view</code> would be an instance of <code><a href="Classes/WebViewControllerPresenterMobile.html">WebViewControllerPresenterMobile</a></code> on iOS or <code>WebViewControllerPresenterDesktop</code> on macOS. </p>
<hr>
<h2 id='making-api-requests' class='heading'>Making API requests</h2>
<p>Once you have an authorized client, you can try some API requests using the SDK. To begin, create a reference to your <code><a href="Classes/PCloudClient.html">PCloudClient</a></code> instance:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">client</span> <span class="o">=</span> <span class="kt">PCloud</span><span class="o">.</span><span class="n">sharedClient</span> <span class="c1">// When using the authorization flow</span>
</code></pre>
<h3 id='working-with-the-network-tasks' class='heading'>Working with the network tasks</h3>
<p>The SDK comes with the most common API requests predefined and has exposed them through the <code><a href="Classes/PCloudClient.html">PCloudClient</a></code> instance as methods. Each method returns a non-running task object representing the API request. Once you have obtained a task, you can assign callback blocks to it and start it. Once a task completes it produces a <code>Result</code> value.</p>
<p>There are three types of tasks:</p>
<h5 id='calltask' class='heading'>CallTask</h5>
<p>Performs an RPC request. On success produces the pre-parsed response of the request. On failure, produces a <code><a href="Enums/CallError.html">CallError</a></code> value.</p>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PCloudSDKSwift</span>
<span class="n">client</span><span class="o">.</span><span class="nf">createFolder</span><span class="p">(</span><span class="nv">named</span><span class="p">:</span> <span class="s">"Movies"</span><span class="p">,</span> <span class="nv">inFolder</span><span class="p">:</span> <span class="kt">Folder</span><span class="o">.</span><span class="n">root</span><span class="p">)</span>
<span class="o">.</span><span class="n">addCompletionBlock</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="c1">// Handle result</span>
<span class="p">}</span>
<span class="o">.</span><span class="nf">start</span><span class="p">()</span>
</code></pre>
<h5 id='uploadtask' class='heading'>UploadTask</h5>
<p>Performs an upload. On success produces the metadata of the uploaded file. On failure, produces a <code><a href="Enums/CallError.html">CallError</a></code> value.</p>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PCloudSDKSwift</span>
<span class="n">client</span><span class="o">.</span><span class="nf">upload</span><span class="p">(</span><span class="nv">fromFileAt</span><span class="p">:</span> <span class="s">"file:///path/to/file"</span><span class="p">,</span> <span class="nv">toFolder</span><span class="p">:</span> <span class="kt">Folder</span><span class="o">.</span><span class="n">root</span><span class="p">,</span> <span class="nv">asFileNamed</span><span class="p">:</span> <span class="s">"song.mp3"</span><span class="p">)</span>
<span class="o">.</span><span class="n">addProgressBlock</span> <span class="p">{</span> <span class="n">uploaded</span><span class="p">,</span> <span class="n">total</span> <span class="k">in</span>
<span class="c1">// Handle progress</span>
<span class="p">}</span>
<span class="o">.</span><span class="n">addCompletionBlock</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="c1">// Handle result</span>
<span class="p">}</span>
<span class="o">.</span><span class="nf">start</span><span class="p">()</span>
</code></pre>
<h5 id='downloadtask' class='heading'>DownloadTask</h5>
<p>Downloads a file. On success, produces the URL of the downloaded file. On failure, produces a <code><a href="Enums/NetworkOperationError.html">NetworkOperationError</a></code> value.</p>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PCloudSDKSwift</span>
<span class="k">let</span> <span class="nv">link</span><span class="p">:</span> <span class="kt">FileLink</span><span class="o">.</span><span class="kt">Metadata</span>
<span class="n">client</span><span class="o">.</span><span class="nf">downloadFile</span><span class="p">(</span><span class="nv">from</span><span class="p">:</span> <span class="n">link</span><span class="o">.</span><span class="n">address</span><span class="p">,</span> <span class="nv">downloadTag</span><span class="p">:</span> <span class="n">link</span><span class="o">.</span><span class="n">downloadTag</span><span class="p">,</span> <span class="nv">to</span><span class="p">:</span> <span class="p">{</span> <span class="n">path</span> <span class="k">in</span>
<span class="c1">// Move the file</span>
<span class="p">})</span>
<span class="o">.</span><span class="n">addCompletionBlock</span> <span class="p">{</span> <span class="n">result</span> <span class="k">in</span>
<span class="c1">// Handle completion</span>
<span class="p">}</span>
<span class="o">.</span><span class="n">addProgressBlock</span> <span class="p">{</span> <span class="n">written</span><span class="p">,</span> <span class="n">total</span> <span class="k">in</span>
<span class="c1">// Handle progress</span>
<span class="p">}</span>
<span class="o">.</span><span class="nf">start</span><span class="p">()</span>
</code></pre>
<p>Once started, a task can stop if it succeeds, fails or if it is cancelled. Since tasks are not reusable, once a task stops running in any way, it can no longer be started again.
The completion block of a task will only be called if a task fails or succeeds, <strong>not</strong> when it is cancelled. Also, all of a task’s callback blocks are called on the main queue.
A task will be retained in memory <strong>while it is running</strong>, so there is no need to manually keep a reference to it, given that you start the task at the time of creation.</p>
<h3 id='handling-errors' class='heading'>Handling errors</h3>
<p>Upload and RPC call tasks fail with a <code><a href="Enums/CallError.html">CallError</a></code>. This enum combines the possible errors from the networking layer and the PCloud API layer. One of the possible errors is <code>CallError<T>.methodError(T)</code> and the suberror there will depend on the API method being executed by the task. All API methods are defined in <code>PCloudAPI.swift</code> and each one has an <code>Error</code> enum defined in its namespace. So, for example, if you are executing a <code>ListFolder</code> API method, the task error would be defined as <code>CallError<ListFolder.Error></code>. Some API methods (e.g. <code>UserInfo</code>) cannot fail with anything else than generic API errors so they will define their error as <code><a href="Structs/NullError.html">NullError</a></code>. Such tasks can never fail with <code>CallError<T>.methodError(T)</code>.</p>
<hr>
<h2 id='examples' class='heading'>Examples</h2>
<p>An example app can be found in the <a href="https://github.com/pcloud/pcloud-sdk-swift/tree/master/Example_iOS">Example_iOS</a> folder. The example app demonstrates how to authenticate a user and how to list a user’s files and folders.</p>
<hr>
<h2 id='documentation' class='heading'>Documentation</h2>
<ul>
<li><a href="https://pcloud.github.io/pcloud-sdk-swift">pCloud Swift SDK</a></li>
<li><a href="https://docs.pcloud.com/">pCloud API</a></li>
</ul>
</section>
</section>
<section id="footer">
<p>© 2020 <a class="link" href="http://pcloud.com" target="_blank" rel="external">pCloud</a>. All rights reserved. (Last updated: 2020-05-27)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.1</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
</div>
</body>
</div>
</html>