How can i set profile pic against firebase user authentication with email? - swift3

i am developing an employee management app thats why i need to save profile picture against employee id or email .In firebase database what is the process and how can i design database through source code.In swift 3,xcode 8.3.2,ios 10.

You need to store the profile Image on the firebase storage,
after that imageURL will be retrieved. After the imageURL is retrieved you need to save it on the firebase database as a child of the userId.
Refer the code below
func handleRegister() {
guard let email = self.emailTextField.text, let password = self.passwordTextField.text, let name = self.nameTextField.text else {
print("Form is not valid")
return
}
Auth.auth().createUser(withEmail: email, password: password) { (user: User?, error) in
if error != nil {
print(error!)
return
}
guard let uid = user?.uid else {
return
}
//success
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("\(imageName).png")
if let uploadData = UIImageJPEGRepresentation(self.profileImageView.image!, 0.1) {
storageRef.putData(uploadData, metadata: nil, completion:
{ (metadata, error) in
if error != nil {
print(error!)
return
}
print(metadata!)
if let progileImageURL = metadata?.downloadURL()?.absoluteString {
let values = ["name": name, "email": email, "profileImageUrl": progileImageURL]
self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
}
})
}
}
}
func registerUserIntoDatabaseWithUID(uid: String, values: [String: AnyObject]) {
let ref = Database.database().reference()
let userRef = ref.child("users").child(uid)
userRef.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!)
return
}
print("Saved user successfully into Firebase db")
self.messagesController1?.checkIfUseLoggedin()
self.dismiss(animated: true, completion: nil)
})
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
print(editedImage)
}
else if let orignalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImageFromPicker = orignalImage
print(orignalImage)
}
if let selectedImage = selectedImageFromPicker {
self.profileImageView.image = selectedImage
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("picker cancled")
self.dismiss(animated: true, completion: nil)
}

Related

While implementing the MSAL Authentication, not able to make the Graph API Call

I am trying to make a Graph API call after getting the access token but not able to make the graph API call function.
Facing the error in calling the function, actually I don't have an idea about the HTTP response but I need a user credentials for login and the logout functions, if suppose I got a user credentials means I have to send the data's from login function to logout function.
struct FieldUIViewRepresentable : UIViewControllerRepresentable{
func makeUIViewController(context: Context) -> some UIViewController {
let log = loginviewcontroller()
return log
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
}
class loginviewcontroller : UIViewController,UIWindowSceneDelegate{
var lableText : String = "default Login"
var account : MSALAccount? = nil
let kAuthority = "https://login.microsoftonline.com/common"
let kGraphEndpoint = "https://graph.microsoft.com/"
var accessToken : String = ""
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 60))
button.setTitle("Login click", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .blue
button.addTarget(self, action: #selector(login), for: .touchUpInside)
self.view.addSubview(button)
let button2 = UIButton(frame: CGRect(x: 100, y: 180, width: 200, height: 60))
button2.setTitle("Logout", for: .normal)
button2.setTitleColor(.white, for: .normal)
button2.backgroundColor = .red
button2.addTarget(self, action: #selector(logout), for: .touchUpInside)
self.view.addSubview(button2)
}
#objc func login(){
let config = MSALPublicClientApplicationConfig(clientId: "0f166c0f-55e1-4de1-b6fe-e0c35c331f4b")
let scopes = [""]
if let application = try? MSALPublicClientApplication(configuration: config) {
let viewController = self
let webviewParameters = MSALWebviewParameters(authPresentationViewController: viewController)
let interactiveParameters = MSALInteractiveTokenParameters(scopes: scopes, webviewParameters: webviewParameters)
application.acquireToken(with: interactiveParameters, completionBlock: { (result, error) in
guard let authResult = result, error == nil else {
print("The auth result is \(error!)")
print("Auth result localized error \(error!.localizedDescription)")
return
}
print("Auth Result is \(authResult.account)")
self.account = authResult.account
let accessToken = authResult.accessToken
self.accessToken = accessToken
self.getContentWithToken()
print("The access token is \(accessToken)")
let accountIdentifier = authResult.account.identifier
print("Account Identifier \(String(describing: accountIdentifier))")
})
}
else {
print("Unable to create application.")
}
}
func getContentWithToken() {
print("get content with token function invokes")
// Specify the Graph API endpoint
let graphURI = getGraphEndpoint()
let url = URL(string: graphURI)
var request = URLRequest(url: url!)
request.setValue("Bearer \(self.accessToken)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
return
}
guard let result = try? JSONSerialization.jsonObject(with: data!, options: []) else {
return
}
}.resume()
}
func getGraphEndpoint() -> String {
return kGraphEndpoint.hasSuffix("/") ? (kGraphEndpoint + "v1.0/me/") : (kGraphEndpoint + "/v1.0/me/");
}
#objc func logout(){
let account = self.account ?? nil //* account retrieved above
let config = MSALPublicClientApplicationConfig(clientId: "0f166c0f-55e1-4de1-b6fe-e0c35c331f4b")
let scopes = [""]
let application = try? MSALPublicClientApplication(configuration: config)
let viewController = self
let webviewParameters = MSALWebviewParameters(authPresentationViewController: viewController)
let signoutParameters = MSALSignoutParameters(webviewParameters: webviewParameters)
signoutParameters.signoutFromBrowser = false
application?.signout(with: account!, signoutParameters: signoutParameters, completionBlock: {(success, error) in
if let error = error {
// Signout failed
return
}
print("Signout is Completed")
// Sign out completed successfully
})
}
}

Updated Reverse Geolocation

There are a number of examples showing how to do reverse geolocation, but nothing recent on implementation in SwiftUI. My current code uses the iPhone GPS to generate coordinates that are used with maps to show the location. I would also like to display the street address since a map without text indicating the location isn't very helpful.
My Questions:
Do I have all the relevant code to implement reverse geolocation?
I have seen examples using storyboards and print statements to display the location, but how do I return the location to a Swiftui view with an #escaping closure?
import Foundation
import CoreLocation
class LocationManager: NSObject, ObservableObject {
private let locationManager = CLLocationManager()
#Published var currentAddress: String = ""
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.distanceFilter = 10 // distance before update (meters)
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
}
func startLocationServices() {
if locationManager.authorizationStatus == .authorizedAlways || locationManager.authorizationStatus == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
} else {
locationManager.requestWhenInUseAuthorization()
}
}
func getLocationCoordinates() -> (Double, Double) {
let coordinate = self.locationManager.location != nil ? self.locationManager.location!.coordinate : CLLocationCoordinate2D()
print("location = \(coordinate.latitude), \(coordinate.longitude)")
return (Double(coordinate.latitude), Double(coordinate.longitude))
}
// Using closure
func getAddress(handler: #escaping (String) -> Void)
{
self.currentAddress = ""
let coordinate = self.locationManager.location != nil ? self.locationManager.location!.coordinate : CLLocationCoordinate2D()
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark?
placeMark = placemarks?[0]
guard let placemark = placemarks?.first else { return }
if let streetNumber = placemark.subThoroughfare,
let street = placemark.subThoroughfare,
let city = placemark.locality,
let state = placemark.administrativeArea {
DispatchQueue.main.async {
self.currentAddress = "\(streetNumber) \(street) \(city) \(state)"
}
} else if let city = placemark.locality, let state = placemark.administrativeArea {
DispatchQueue.main.async {
self.currentAddress = "\(city) \(state)"
}
} else {
DispatchQueue.main.async {
self.currentAddress = "Address Unknown"
}
}
}
)
print( self.currentAddress)
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
if locationManager.authorizationStatus == .authorizedAlways || locationManager.authorizationStatus == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
}
}
// Get Placemark
func getPlace(for location: CLLocation,
completion: #escaping (CLPlacemark?) -> Void) {
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { placemarks, error in
guard error == nil else {
print("*** Error in \(#function): \(error!.localizedDescription)")
completion(nil)
return
}
guard let placemark = placemarks?[0] else {
print("*** Error in \(#function): placemark is nil")
completion(nil)
return
}
completion(placemark)
}
}
}
If I add the follow code say in ContentView:
#State private var entryLat: Double = 0.0
#State private var entryLong: Double = 0.0
let result = lm.getLocationCoordinates()
entryLat = result.0
entryLong = result.1
How would I call getPlace?
To use the following code you need to setup the appropriate entitlements and authorizations.
Here is a working example of using geolocation in swiftui, from code I got from
a number of sources on the net years ago.
This should give you a base to do reverse geolocation in swiftui:
import Foundation
import CoreLocation
import SwiftUI
import Combine
#main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
let locationProvider = LocationProvider()
#State var currentAddress = ""
var body: some View {
Text(currentAddress)
.onAppear {
getAddress()
}
}
func getAddress() {
// for testing Tokyo
let location = CLLocation(latitude: 35.684602, longitude: 139.751992)
locationProvider.getPlace(for: location) { plsmark in
guard let placemark = plsmark else { return }
if let streetNumber = placemark.subThoroughfare,
let street = placemark.subThoroughfare,
let city = placemark.locality,
let state = placemark.administrativeArea {
self.currentAddress = "\(streetNumber) \(street) \(city) \(state)"
} else if let city = placemark.locality, let state = placemark.administrativeArea {
self.currentAddress = "\(city) \(state)"
} else {
self.currentAddress = "Address Unknown"
}
}
}
}
/**
A Combine-based CoreLocation provider.
On every update of the device location from a wrapped `CLLocationManager`,
it provides the latest location as a published `CLLocation` object and
via a `PassthroughSubject<CLLocation, Never>` called `locationWillChange`.
*/
public class LocationProvider: NSObject, ObservableObject {
private let lm = CLLocationManager()
/// Is emitted when the `location` property changes.
public let locationWillChange = PassthroughSubject<CLLocation, Never>()
/**
The latest location provided by the `CLLocationManager`.
Updates of its value trigger both the `objectWillChange` and the `locationWillChange` PassthroughSubjects.
*/
#Published public private(set) var location: CLLocation? {
willSet {
locationWillChange.send(newValue ?? CLLocation())
}
}
/// The authorization status for CoreLocation.
#Published public var authorizationStatus: CLAuthorizationStatus?
/// A function that is executed when the `CLAuthorizationStatus` changes to `Denied`.
public var onAuthorizationStatusDenied : ()->Void = {presentLocationSettingsAlert()}
/// The LocationProvider intializer.
///
/// Creates a CLLocationManager delegate and sets the CLLocationManager properties.
public override init() {
super.init()
self.lm.delegate = self
self.lm.desiredAccuracy = kCLLocationAccuracyBest
self.lm.activityType = .fitness
self.lm.distanceFilter = 10
self.lm.allowsBackgroundLocationUpdates = true
self.lm.pausesLocationUpdatesAutomatically = false
self.lm.showsBackgroundLocationIndicator = true
}
/**
Request location access from user.
In case, the access has already been denied, execute the `onAuthorizationDenied` closure.
The default behavior is to present an alert that suggests going to the settings page.
*/
public func requestAuthorization() -> Void {
if self.authorizationStatus == CLAuthorizationStatus.denied {
onAuthorizationStatusDenied()
}
else {
self.lm.requestWhenInUseAuthorization()
}
}
/// Start the Location Provider.
public func start() throws -> Void {
self.requestAuthorization()
if let status = self.authorizationStatus {
guard status == .authorizedWhenInUse || status == .authorizedAlways else {
throw LocationProviderError.noAuthorization
}
}
else {
/// no authorization set by delegate yet
#if DEBUG
print(#function, "No location authorization status set by delegate yet. Try to start updates anyhow.")
#endif
/// In principle, this should throw an error.
/// However, this would prevent start() from running directly after the LocationProvider is initialized.
/// This is because the delegate method `didChangeAuthorization`,
/// setting `authorizationStatus` runs only after a brief delay after initialization.
//throw LocationProviderError.noAuthorization
}
self.lm.startUpdatingLocation()
}
/// Stop the Location Provider.
public func stop() -> Void {
self.lm.stopUpdatingLocation()
}
// todo deal with errors
public func getPlace(for location: CLLocation, completion: #escaping (CLPlacemark?) -> Void) {
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { placemarks, error in
guard error == nil else {
print("=====> Error \(error!.localizedDescription)")
completion(nil)
return
}
guard let placemark = placemarks?.first else {
print("=====> Error placemark is nil")
completion(nil)
return
}
completion(placemark)
}
}
}
/// Present an alert that suggests to go to the app settings screen.
public func presentLocationSettingsAlert(alertText : String? = nil) -> Void {
let alertController = UIAlertController (title: "Enable Location Access", message: alertText ?? "The location access for this app is set to 'never'. Enable location access in the application settings. Go to Settings now?", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string:UIApplication.openSettingsURLString) else {
return
}
UIApplication.shared.open(settingsUrl)
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
UIApplication.shared.windows[0].rootViewController?.present(alertController, animated: true, completion: nil)
}
/// Error which is thrown for lacking localization authorization.
public enum LocationProviderError: Error {
case noAuthorization
}
extension LocationProvider: CLLocationManagerDelegate {
public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
self.authorizationStatus = status
#if DEBUG
print(#function, status.name)
#endif
//print()
}
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
self.location = location
}
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
if let clErr = error as? CLError {
switch clErr {
case CLError.denied : do {
print(#function, "Location access denied by user.")
self.stop()
self.requestAuthorization()
}
case CLError.locationUnknown : print(#function, "Location manager is unable to retrieve a location.")
default: print(#function, "Location manager failed with unknown CoreLocation error.")
}
}
else {
print(#function, "Location manager failed with unknown error", error.localizedDescription)
}
}
}
extension CLAuthorizationStatus {
/// String representation of the CLAuthorizationStatus
var name: String {
switch self {
case .notDetermined: return "notDetermined"
case .authorizedWhenInUse: return "authorizedWhenInUse"
case .authorizedAlways: return "authorizedAlways"
case .restricted: return "restricted"
case .denied: return "denied"
default: return "unknown"
}
}
}

Trying to Reload FSCalendar with JSon data but it crashes

this question is linked with the my previous account's question.
How to make API call for this web service to fetch array of present and absent dates separately in swift?
code
import UIKit
import FSCalendar
class AttendenceViewController : UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance
{
#IBOutlet weak var calendar: FSCalendar!
var presentdays : Array = [String]()
var absentdays : Array = [String]()
fileprivate let gregorian: Calendar = Calendar(identifier: .gregorian)
fileprivate lazy var dateFormatter1: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
fileprivate lazy var dateFormatter2: DateFormatter = {
let formatter2 = DateFormatter()
formatter2.dateFormat = "dd"
return formatter2
}()
override func viewDidLoad() {
super.viewDidLoad()
self.getdateFromJSON()
}
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
let datestring2 : String = dateFormatter1.string(from: date)
if presentdays.contains(datestring2)
{
return UIColor.green
}
else if absentdays.contains(datestring2)
{
return UIColor.red
}
else
{
return nil
}
}
func getdateFromJSON()
{
let url = NSURL(string: "ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=2&month=6&year=2017")
let request = NSMutableURLRequest(url: url! as URL)
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error)
in
guard error == nil && data != nil else
{
print("Error:",error)
return
}
let httpstatus = response as? HTTPURLResponse
if httpstatus?.statusCode == 200
{
if data?.count != 0
{
if let responseJSON = (try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)) as? [String:Any],
let presentdetails = responseJSON["Present"] as? [[String:Any]],
let Absentdetails = responseJSON["Absent"] as? [[String:Any]] {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
self.presentdays = presentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap {
self.dateFormatter2.string(from:$0)
}
self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap { self.dateFormatter2.string(from:$0) }
DispatchQueue.main.async
{
self.calendar.reloadData()
}
}
}
else
{
print("No data got from URL")
}
}
else{
print("error httpstatus code is :",httpstatus?.statusCode)
}
}
task.resume()
}
}
I'm attaching the error log here please help thanks in advance.
ERROR LOG
2017-06-17 10:23:50.671 ezSchool[1848:24475]
-[UICachedDeviceWhiteColor unsignedLongLongValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.672
ezSchool[1848:24475] Failed to set (placeholderType) user defined
inspected property on (FSCalendar): -[UICachedDeviceWhiteColor
unsignedLongLongValue]: unrecognized selector sent to instance
0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475]
-[UICachedDeviceWhiteColor unsignedLongLongValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.672
ezSchool[1848:24475] Failed to set (firstWeekday) user defined
inspected property on (FSCalendar): -[UICachedDeviceWhiteColor
unsignedLongLongValue]: unrecognized selector sent to instance
0x608000059ec0 2017-06-17 10:23:50.672 ezSchool[1848:24475]
-[UICachedDeviceWhiteColor doubleValue]: unrecognized selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.673 ezSchool[1848:24475]
Failed to set (headerHeight) user defined inspected property on
(FSCalendar): -[UICachedDeviceWhiteColor doubleValue]: unrecognized
selector sent to instance 0x608000059ec0 2017-06-17 10:23:50.673
ezSchool[1848:24475] -[UICachedDeviceWhiteColor doubleValue]:
unrecognized selector sent to instance 0x608000059ec0 2017-06-17
10:23:50.673 ezSchool[1848:24475] Failed to set (headerTitleTextSize)
user defined inspected property on (FSCalendar):
-[UICachedDeviceWhiteColor doubleValue]: unrecognized selector sent to instance 0x608000059ec0
Replace the function getdateFromJSON() with
func getdateFromJSON()
{
let url = NSURL(string: "http://ezschoolportalapi.azurewebsites.net/api/Student/AttendanceDetails?schoolid=1&studentid=2&month=6&year=2017")
let request = NSMutableURLRequest(url: url! as URL)
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error)
in
guard error == nil && data != nil else
{
print("Error:",error ?? "error")
return
}
let httpstatus = response as? HTTPURLResponse
if httpstatus?.statusCode == 200
{
if data?.count != 0
{
if let responseJSON = (try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)) as? [String:Any],
let presentdetails = responseJSON["Present"] as? [[String:Any]],
let Absentdetails = responseJSON["Absent"] as? [[String:Any]] {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
self.presentdays = presentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap {
self.dateFormatter1.string(from:$0)
}
self.absentdays = Absentdetails.flatMap { dateFormatter.date(from: $0["Date"] as! String) }.flatMap { self.dateFormatter1.string(from:$0) }
DispatchQueue.main.async
{
self.calendar.reloadData()
}
}
}
else
{
print("No data got from URL")
}
}
else{
print("error httpstatus code is :",httpstatus?.statusCode ?? "5")
}
}
task.resume()
}
Please see whether the below attached image is your desired output

Recording video to specific album

I am creating an album in the users photo library, now I want to save a video there. I am saving the video to a file using:
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let filePath = documentsURL.URLByAppendingPathComponent("video")
Now I want to take the video, and save it to an album. I've found lots on saving to the camera roll, but nothing on saving to an album. Can it be done, and if so, how?
Assuming you have a PHAssetCollection specifying the album, you can use this PHAssetCollection extension:
extension PHAssetCollection {
private func isCameraRollAlbum() -> Bool
{
let query = PHAssetCollection.fetchAssetCollections(with: .smartAlbum,
subtype: .smartAlbumUserLibrary,
options: nil)
let result: PHAssetCollection? = query.firstObject
return self == result
}
func save(videoURL: URL, completion: #escaping (URL?, String?) -> ()) {
let isCameraRoll = isCameraRollAlbum()
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now()) {
PHPhotoLibrary.shared().performChanges({
if let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL) {
if isCameraRoll == false, let placeholder = assetRequest.placeholderForCreatedAsset {
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self)
albumChangeRequest?.addAssets([placeholder] as NSArray)
}
}
}, completionHandler: { (success, error) in
if success == false {
completion(nil, error?.localizedDescription)
}
else {
completion(videoURL, nil)
}
})
}
}
}
Remarks:
Method 'isCameraRollAlbum' was defined because it was found that the use of placeholders for the whole photo album doesn't work, and you only need to use
PHAssetChangeRequest.creationRequestForAssetFromVideo
to save a video to the whole photo library.
Using a background thread is not necessary.
Example usage, it is assumed a video named 'Video.mov' is in the Documents directory of the app. This will save it to the 'Camera Roll' album but a PHAssetCollection for any album can be specified:
let docsurl = try! FileManager.default.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let videoURL = docsurl.appendingPathComponent("Video.mov")
let fetchResult = PHAssetCollection.fetchAssetCollections(with:.smartAlbum,subtype:.smartAlbumUserLibrary,options: nil)
if let allMediaAlbum = fetchResult.firstObject {
allMediaAlbum.save(videoURL: videoURL) { (url, message) in
print("message = \(String(describing: message))")
}
}
For example, you can use this extension to obtain the PHAssetCollection for an album with a given name 'title':
class func getAlbum(title: String, completionHandler: #escaping (PHAssetCollection?) -> ()) {
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now()) {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %#", title)
let collections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let album = collections.firstObject {
completionHandler(album)
} else {
completionHandler(nil)
}
}
}
Example usage, saving video 'Video.mov' to album named 'My Umbrella':
let docsurl = try! FileManager.default.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let albumName = "My Umbrella"
let videoURL = docsurl.appendingPathComponent("Video.mov")
PHAssetCollection.getAlbum(title: albumName) { (album) in
if let album = album {
album.save(videoURL: videoURL, completion: { (url, error) in
if let url = url {
print("Video '\(url.lastPathComponent) saved to '\(albumName)'")
}
else {
print("Error: \(String(describing: error))")
}
})
}
}
(Keep in mind that the photos library can have multiple albums by the same name.)

api youtube in swift 3 error 403

I am trying to use youtube api in ios swift, and following this tutorial
http://www.appcoda.com/youtube-api-ios-tutorial/
HTTP Status Code = 403
Error while loading channel details: nil
I'm using swift 3
var urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=\(textField.text)&type=\(type)&key=\(apiKey)"
urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
// Create a NSURL object based on the above string.
let targetURL = URL(string: urlString)
// Get the results.
performGetRequest(targetURL, completion: { (data, HTTPStatusCode, error) -> Void in
if HTTPStatusCode == 200 && error == nil {
// Convert the JSON data to a dictionary object.
do {
let resultsDict = try JSONSerialization.jsonObject(with: data!, options: []) as! Dictionary<String, AnyObject>
// Get all search result items ("items" array).
let items: Array<Dictionary<String, AnyObject>> = resultsDict["items"] as! Array<Dictionary<String, AnyObject>>
// Loop through all search results and keep just the necessary data.
for i in 0 ..< items.count {
let snippetDict = items[i]["snippet"] as! Dictionary<String, AnyObject>
// Gather the proper data depending on whether we're searching for channels or for videos.
if self.segDisplayedContent.selectedSegmentIndex == 0 {
// Keep the channel ID.
self.desiredChannelsArray.append(snippetDict["channelId"] as! String)
}
else {
// Create a new dictionary to store the video details.
var videoDetailsDict = Dictionary<String, AnyObject>()
videoDetailsDict["title"] = snippetDict["title"]
videoDetailsDict["thumbnail"] = ((snippetDict["thumbnails"] as! Dictionary<String, AnyObject>)["default"] as! Dictionary<String, AnyObject>)["url"]
videoDetailsDict["videoID"] = (items[i]["id"] as! Dictionary<String, AnyObject>)["videoId"]
// Append the desiredPlaylistItemDataDict dictionary to the videos array.
self.videosArray.append(videoDetailsDict)
// Reload the tableview.
self.tblVideos.reloadData()
}
}
} catch {
print(error)
}
// Call the getChannelDetails(…) function to fetch the channels.
if self.segDisplayedContent.selectedSegmentIndex == 0 {
self.getChannelDetails(true)
}
}
else {
print("HTTP Status Code = \(HTTPStatusCode)")
print("Error while loading channel videos: \(error)")
}
// Hide the activity indicator.
self.viewWait.isHidden = true
})
return true
}
// MARK: Custom method implementation
func performGetRequest(_ targetURL: URL!, completion: #escaping (_ data: Data?, _ HTTPStatusCode: Int, _ error: NSError?) -> Void) {
// let request = NSMutableURLRequest(url: targetURL)
// request.httpMethod = "GET"
var request = URLRequest(url: targetURL)
request.httpMethod = "GET"
let sessionConfiguration = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfiguration)
/* let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
DispatchQueue.main.async(execute: { () -> Void in
completion(data, (response as! HTTPURLResponse).statusCode, error)
})
} as! (Data?, URLResponse?, Error?) -> Void)*/
/* let task = session.dataTask(with: request, completionHandler: ({ (data: Data?, response: URLResponse?, error: NSError?) -> Void in
DispatchQueue.main.async(execute: { () -> Void in
completion(data as Data?, (response as! HTTPURLResponse).statusCode, error)
})
} as! (Data?, URLResponse?, Error?) -> Void))*/
let task = session.dataTask(with: request) { data, response, error in DispatchQueue.main.async { completion(data, (response as! HTTPURLResponse).statusCode, error as? NSError) } }
task.resume()
}
First of all the JSON dictionary representation in Swift 3 is [String:Any] (aka Dictionary<String,Any>)
Second of all in Swift 3 all parameter labels in closures have been removed
func performGetRequest(_ targetURL: URL, completion: #escaping (Data?, Int, NSError?) -> Void) {
Do not use implicit unwrapped optionals for method parameter types. Either use regular optional (?) or non-optional.
Error 403 means Forbidden Access. Make sure you have the correct apiKey from google/youtube developer.
I also used the appcoda youtube api tutorial (which is in Swift 2 I think) and this is a working version of mine for swift 3.
func getVideosForChannelAtIndex() {
let urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=\(playlistID)&maxResults=\(maxResults)&key=\(apiKey)"
// Create a NSURL object based on the above string.
let targetURL = URL(string: urlString)
// Fetch the playlist from Google.
performGetRequest(targetURL!) { (data, HTTPStatusCode, error) -> Void in
if HTTPStatusCode == 200 && error == nil {
do {
self.videos = []
// Convert the JSON data into a dictionary.
let resultsDict = try JSONSerialization.jsonObject(with: data!, options: []) as! Dictionary<AnyHashable, Any>
// Get all playlist items ("items" array).
let items:Array<Dictionary<AnyHashable, Any>> = resultsDict["items"] as! Array<Dictionary<AnyHashable, Any>>
// Use a loop to go through all video items.
// for var i=0; i<items.count; ++i
for i in 0 ..< items.count {
let playlistSnippetDict = (items[i] as Dictionary<AnyHashable, Any>)["snippet"] as! Dictionary<AnyHashable, Any>
let video = Video()
video.title = playlistSnippetDict["title"] as? String
// video.thumbnail =
video.videoId = (playlistSnippetDict["resourceId"] as? Dictionary<AnyHashable, Any>)?["videoId"] as? String
guard let thumbnail = ((playlistSnippetDict["thumbnails"] as? Dictionary<AnyHashable, Any>)?["high"] as? Dictionary<AnyHashable, Any>)?["url"] as? String else {
video.thumbnail = UIImage(named: "Icon1024x1024")
return
}
guard let url:URL? = URL(string: thumbnail), let data:Data? = try? Data(contentsOf: url!) else {
video.thumbnail = UIImage(named: "Icon1024x1024")
return
}
if let dataImage = data {
video.thumbnail = UIImage(data: dataImage)
} else {
video.thumbnail = UIImage(named: "Icon1024x1024")
}
self.videos.append(video)
// Reload the tableview.
self.tblVideos.reloadData()
}
} catch {
print("json error: \(error)")
}
} else {
print("")
print("HTTP Status Code = \(HTTPStatusCode)")
print("")
//Show alertDialog here with Error
print("Error while loading videos: \(error?.localizedDescription)")
let alert = UIAlertView(title: "Oops!", message: error?.localizedDescription, delegate: self, cancelButtonTitle: "OK")
alert.show()
}
// Hide the activity indicator.
self.viewWait.isHidden = true
}
}
This is for the performGetRequest
func performGetRequest(_ targetURL: URL, completion: #escaping (_ data: Data?, _ HTTPStatusCode: Int?, _ error: Error?) -> Void) {
var request = URLRequest(url: targetURL)
request.httpMethod = "GET"
let sessionConfiguration = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfiguration)
let task = session.dataTask(with: request) { data, response, error in
DispatchQueue.main.async(execute: {
completion(data, (response as? HTTPURLResponse)?.statusCode, error)
})
}
task.resume()
}