Get larger facebook image through firebase login - swift

I am using firebase to login a user through facebook. This all works fine and I can get the users FB profile image, although it is to small. Can somebody tell me how to get a larger one, the code I am using:
override func viewDidLoad() {
let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
loginButton.delegate = self
self.view.addSubview(loginButton)
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError?) {
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
if let user = FIRAuth.auth()?.currentUser {
for profile in user.providerData {
let photoUrl = profile.photoURL?.absoluteString //SMALL IMAGE
}
}
}
}
(This is done in swift)

Once the FB user authenticates, make a FBSDKGraphRequest using FBSDKGraphRequestConnection to get the users FBid, using which you can get the users profile picture by pluggin the id here:
https://graph.facebook.com//picture?type=large&return_ssl_resources=1
instead of type=large you can use ?width=<width>&height=<height> too
too

Once you get the photo Url, just add "?width=400&height=400" to the photo url. Here, height=width=400, you can choose your own height and width. It surely works!

According to the Facebook documentation on Profile Picture, you should be able to specify the size by appending width and height to the url:
let photoUrl = profile.photoURL?.absoluteString + "?width=\(width)&height=\(height)"
or by specifying the type:
let photoUrl = profile.photoURL?.absoluteString + "?type=large"

Related

Google Classroom API integration with swift

I want to link my iOS app with Google Classroom on swift. My current goal is to be able to get a list of all the courses I am enrolled in.
Here is the code I am currently using
func googleClassroomList() {
//let sharedInstance = GIDSignIn.sharedInstance()
//let handler = sharedInstance
googleClassroomService.apiKey = "AIzaSyBOGamjhRuu45T2jT7Qa3LmtntSwgIxeqo"
let query = GTLRClassroomQuery_CoursesList.query()
query.pageSize = 1000
let classQuery = googleClassroomService.executeQuery(query, completionHandler: { ticket , fileList, error in
if error != nil {
let message = "Error: \(error?.localizedDescription ?? "")"
print(message)
} else {
if let list = fileList as? GTLRClassroomQuery_CoursesList {
self.fileList = list
print("List: \(list)")
}
else {
print("Error: response is not a file list")
}
}
}
)
}
Here is the error message:
Error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
I don't understand where I attach the OAuth access token, I tried putting it under apiKey but I don't really understand what I am supposed to do. For reference, I am using this auth scope. "https://www.googleapis.com/auth/classroom.courses"
First you have to sign-in using Google, and Google will manage the OAuth 2.0 token for you.
Here you have the instructions:
https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift
Also make sure to set the service.authorizer property when you are actually signed in:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
guard let user = user else {
return
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.setLoggedOut(loggedOut: false)
self.myClassroom.service.authorizer = user.authentication.fetcherAuthorizer()
self.showClassroomClasses()
}

How to get login username with AWS Mobile-hub SDK

I've been working for a while on the login part of my app. I'm trying to use ASW Mobile Hub for this matter. I found a way to get it work with the different providers I need: my own user pool, FB and Google.
The problem is that I've been searching here and all over the AWS documentation trying to find the way to get user data (Username and some othe user data like picture, email and so on). I can get it if I'm using the FBSDK directly (usingFBSDKGraphRequest) but I don't know how to do it if the user choose to login in my cognito-user-pool. Also I cannot see what provider the user used once succeeded.
I can find some other ways to get that, but using the old SDK o directly Cognito calls and initially is not what I need. Here's the code I'm using to present the login window:
override func viewDidLoad() {
super.viewDidLoad()
if !AWSSignInManager.sharedInstance().isLoggedIn {
presentAuthUIViewController()
}
}
func presentAuthUIViewController() {
let config = AWSAuthUIConfiguration()
config.enableUserPoolsUI = true
config.addSignInButtonView(class: AWSFacebookSignInButton.self)
config.addSignInButtonView(class: AWSGoogleSignInButton.self)
AWSAuthUIViewController.presentViewController(
with: self.navigationController!,
configuration: config, completionHandler: { (provider:
AWSSignInProvider, error: Error?) in
if error == nil {
// SignIn succeeded.
} else {
// end user faced error while loggin in, take any
required action here.
}
})
}
So, the question is, how can I get the relevant user info, once the signin is succeeded?
If the user used cognito login, you can use the below code to get the username.
let identityManager = AWSIdentityManager.default()
let identityUserName = identityManager.identityProfile?.userName
For retrieving the provider once user succeeds, keep it in the session as below
func onLogin(signInProvider: AWSSignInProvider, result: Any?,
authState: AWSIdentityManagerAuthState, error: Error?) {
let defaults = UserDefaults.standard
defaults.set(signInProvider.identityProviderName, forKey:
"identityProviderName")
}
Hope this answer helps.
Updated Code to get Username:
let pool = AWSCognitoIdentityUserPool.init(forKey: "CognitoUserPools")
let username = pool.currentUser()?.username
I've been working on a workaround till I sort this out in a more elegant way. I guess that I need to go deeper in Cognito's understanding. But the fact is even the sample provided by Amazon doesen't show the User's Name...
Sample Amazon app screen
So, in the meantime, I modified the source code of the Cognito library AWSUserPoolsUIOperations to send me the data directly to my app, on a message:
#implementation AWSUserPoolsUIOperations
-(void)loginWithUserName:(NSString *)userName
password:(NSString *)password
navigationController:(UINavigationController *)navController
completionHandler:(nonnull void (^)(id _Nullable, NSError *
_Nullable))completionHandler {
self.userName = userName;
NSDictionary* userInfo = #{#"username": self.userName};
[[NSNotificationCenter defaultCenter]
postNotificationName:#"UsernameNotification"
object:nil userInfo:userInfo];
And then just getting the message in the app and storing the value.
#objc private func TestNotification(_ notification: NSNotification){
if let dict = notification.userInfo as NSDictionary? {
if let username = dict["username"] as? String {
appEstats.username = username
defaults.set(username, forKey: sUserName)
}
}
}
As I said is not the solution but in the meantime it works.

Swift displayed firebase photoUrl cannot replace image by image picker

I use Firebase as the back-end of my app. When user finish authentication they will go to profile create page. It displays the user profile picture from facebook.
I use this code to display
func displayProfilePic(user: FIRUser?){
let photoURL = user?.photoURL
struct last {
static var photoURL: NSURL? = nil
}
last.photoURL = photoURL; // to prevent earlier image overwrites later one.
if let photoURL = photoURL {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
let data = NSData.init(contentsOfURL: photoURL)
if let data = data {
let image = UIImage.init(data: data)
dispatch_async(dispatch_get_main_queue(), {
if (photoURL == last.photoURL) {
self.profilePic.image = image
}
})
}
})
} else {
profilePic.image = UIImage.init(named: "DefaultPic")
}
However, I also let user to pick their own profile picture by camera or photo library. When user choose their Image it will display the picked Image for 1 to 2 second, and display back the facebook profile picture. That's mean, the "picked image cannot replace the facebook profile picture"
It is my code for the camera and photo library
func openGallary(){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
print("Pick Photo")
self.imagePicker.delegate = self
self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.imagePicker.allowsEditing = true
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
}
func openCamera(){
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
self.imagePicker.allowsEditing = true
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}else{
print("you got no camara")
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
profilePic.image = image
}
Firebase Authentication takes a photo URL, so the public location where a profile photo for the user is present. If you want to allow the user to upload a new profile photo, you will have to find a place to store the photo and then put the URL into the Firebase Authentication profile.
One place to keep such a profile picture would be Firebase Storage. See the documentation on how to upload an image from iOS and then generate a download URL that you store in the user profile.

Update Image When Parse Query Done

I'm using Parse for my app written in Swift. It is a golf app that allows the user to have a profile. The user can edit the profile on an edit profile view controller, save it and then they're taken back to the main profile view controller. The problem I'm having is if the user changes their profile image and saves it, the image isn't updated on the main profile view controller but the rest of the new profile info is. My belief is the timing is off with the Parse query and the image isn't coming back in time. Here is my query for my main profile page. I tried using the "dispatch_async" method but this doesn't seem to be working. Thanks in advance.
func getProfileFromBackground() {
profileData.removeAll()
if let userQuery = PFUser.query() {
userQuery.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
userQuery.findObjectsInBackgroundWithBlock({ (currentUserProfile:[PFObject]?, error: NSError?) -> Void in
if error == nil {
for object:PFObject in currentUserProfile! {
self.profileData.append(object)
for data in self.profileData {
dispatch_async(dispatch_get_main_queue()) {
self.golferNameLabel.text = data.objectForKey("name") as? String
self.usernameLabel.text = "Username: \(data.objectForKey("username")!)" as String
self.golferProfileImage.file = data.objectForKey("profileImage") as? PFFile
self.golferProfileImage.loadInBackground()
}
}
}
} else {
print(error)
}
})
}
}

How can I do a Facebook login using Swift 2.0 and iOS 9.1?

I have added the latest Facebook SDK to my XCode 7.1 project written for iOS 9.1. Unfortunately all I know is Swift, not Objective C. Facebook's developer site documentation only has the documentation in Objective C. Every search on Google reveals documentation that is just too old because things have changed so much just in the past 6 months. So I'm kind of lost.
I did all the easy stuff with the plist file.
I was able to use:
import FBSDKCoreKit
import FBSDKLoginKit
I also added:
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
and
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
To my AppDelegate.swift file. This all works and builds successfully. I have the correct frameworks added as well, obviously. Beyond this point I'm at a standstill because I don't know the syntax for adding a login button, for capturing what I assume would be returned json strings with tokens and other profile information I can use to store in the user's account, etc.
You're on the right track.
Have you set up your pList yet?
You're gonna have to add to your VC, add a login button, deal with delegate, and then deal with FB info. Something like (but not exactly) this:
class yourVC: UIViewController, FBSDKLoginButtonDelegate, UITextFieldDelegate
{
var loginView : FBSDKLoginButton = FBSDKLoginButton()
viewDidLoad() {
loginView.frame = CGRectMake(20, 20, theWidth-40, 40)
self.view.addSubview(loginView)
loginView.readPermissions = ["public_profile", "email", "user_friends","user_birthday"]
loginView.delegate = self
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if ((error) != nil)
{
//handle error
} else {
returnUserData()
}
}
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,interested_in,gender,birthday,email,age_range,name,picture.width(480).height(480)"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
let id : NSString = result.valueForKey("id") as! String
print("User ID is: \(id)")
//etc...
}
})
}
You're gonna have to play around with the returnData() part to get it right, but this code should get you 90% of the way there.
I've included a wide range of permissions (user age, interested in, etc), but you'll have to configure them yourself. Honestly this part (the hard part) is really similar with it's object C counterpart, which is much better documented. Just download some get projects where it's working, and try to parse them together into something that suits your fancy.
It can be hard to get it all working, but give it a go, and keep at it!
If you want to perform login from Facebook on click of your custom button, this will help:
#IBAction func onClickFacebookButton(sender: UIButton){
let login = FBSDKLoginManager()
login.loginBehavior = FBSDKLoginBehavior.SystemAccount
login.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: {(result, error) in
if error != nil {
print("Error : \(error.description)")
}
else if result.isCancelled {
}
else {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "first_name, last_name, picture.type(large), email, name, id, gender"]).startWithCompletionHandler({(connection, result, error) -> Void in
if error != nil{
print("Error : \(error.description)")
}else{
print("userInfo is \(result))")
}
})
}
})
}