diff --git a/README.md b/README.md index 6572681..e38f954 100644 --- a/README.md +++ b/README.md @@ -349,8 +349,64 @@ If you are trying to use the Objective-C Pixlee API with a Swift project please #import ``` + 1. These files are now accessible across all your Swift code and can be use the same way as before. +### Swift example + +To load an album from a sku number you can run the following Swift code, please check the swift_example project directory: +``` +let album: PXLAlbum = PXLAlbum(skuIdentifier: PXLSkuAlbumIdentifier) +let filterOptions:PXLAlbumFilterOptions = PXLAlbumFilterOptions() +album.filterOptions = filterOptions + +// Create and set sort options on the album. +let sortOptions = PXLAlbumSortOptions() +sortOptions.sortType = PXLAlbumSortType.random +sortOptions.ascending = true +album.sortOptions = sortOptions +album.perPage = 1 + +album.loadNextPageOfPhotos(fromSku: { photos, error in +if let error = error { +print("\(error)") +} +print(type(of: photos)) +if photos?.count != nil { +// var indexPaths: [AnyHashable] = [] +// var firstIndex: Int? = nil +if let arr = photos as? Array { +for p in arr{ +print(p.cdnLargeUrl) + +} +print(arr) +} + +} + +}) +``` + +#### Swift type casting + +Unfortunately the type casting is not fully working when using Objective-C libraries. You will have to cast the return object from the PIxlee API manually like so ``` let arr = photos as? Array``` + +Check the snipet of code for a full version: + +``` +album.loadNextPageOfPhotos(fromSku: { photos, error in +if let error = error { +print("\(error)") +} +print(type(of: photos)) # Optional> +if let arr = photos as? Array { + +} +}) + +``` + ### Important If you are using xcode 10, the new build system doesn't work with the example project. A temporary workaround seems to be switching to the legacy build system by going to (in Xcode) File -> Workspace Settings -> Build System -> Legacy Build System. But compiling with the CLI still doesnt work. diff --git a/example_swift/pixlee_example/pixlee_example/FirstViewController.swift b/example_swift/pixlee_example/pixlee_example/FirstViewController.swift index 9267085..beca1e6 100644 --- a/example_swift/pixlee_example/pixlee_example/FirstViewController.swift +++ b/example_swift/pixlee_example/pixlee_example/FirstViewController.swift @@ -13,13 +13,15 @@ class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. + let PXLAlbumIdentifier = "4515393" + let PXLSkuAlbumIdentifier = "300152" let album: PXLAlbum = PXLAlbum(skuIdentifier: PXLSkuAlbumIdentifier) let filterOptions:PXLAlbumFilterOptions = PXLAlbumFilterOptions() - var dict = [AnyHashable : Any](minimumCapacity: 10) + album.filterOptions = filterOptions // Create and set sort options on the album. @@ -35,8 +37,7 @@ class FirstViewController: UIViewController { } print(type(of: photos)) if photos?.count != nil { - var indexPaths: [AnyHashable] = [] - var firstIndex: Int? = nil + if let arr = photos as? Array { for p in arr{ print(p.cdnLargeUrl) diff --git a/pixlee_sdk/PXLAlbum.h b/pixlee_sdk/PXLAlbum.h index 2ad729e..e573d40 100644 --- a/pixlee_sdk/PXLAlbum.h +++ b/pixlee_sdk/PXLAlbum.h @@ -100,7 +100,7 @@ @param completionBlock A block called after the photos have been loaded or an error has occurred. `photos` will contain the photos loaded in this network call. - @return The `NSURLSessionDataTask` used to load the data from the server. + @return The `NSURLSessionDataTask` used to load the data from the server. With two parameters NSarray and NSerror, if using swift cast it to the correct type Check Readme. */ - (NSURLSessionDataTask *)loadNextPageOfPhotos:(void (^)(NSArray *photos, NSError *error))completionBlock; @@ -114,7 +114,7 @@ @param completionBlock A block called after the photos have been loaded or an error has occurred. `photos` will contain the photos loaded in this network call. - @return The `NSURLSessionDataTask` used to load the data from the server. + @return The `NSURLSessionDataTask` used to load the data from the server. With two parameters NSarray and NSerror, if using swift cast it to the correct type Check Readme. */ - (NSURLSessionDataTask *)loadNextPageOfPhotosFromSku:(void (^)(NSArray *photos, NSError *error))completionBlock; @@ -155,17 +155,6 @@ Call this whenever a user clicks 'Load More' button on the widget -///--------------------- -/// @name Analytics Event -///--------------------- - -/** -Call this whenever a user clicks 'Load More' button on the widget - - - @return The `NSURLSessionDataTask` the server will retun 'OK' if accepted. - */ -- (NSURLSessionDataTask *)triggerEventLoadMoreClicked:(void (^)(NSError *error))completionBlock; @end