11

I am working on iOS/Swift google sign-in. I made a demo in github. My demo project: https://github.com/tanggod/GoogleSignIn.git

According to google(https://developers.google.com/identity/sign-in/ios/api/interface_g_i_d_profile_data#a8e229bffe211894473058c4ba247553c), the user image url can be fetched like this:

  • (NSURL *) imageURLWithDimension: (NSUInteger) dimension
    Gets the user's profile image URL for the given dimension in pixels for each side of the square.

But, when I try to get imageURLWithDimension(120), the App always crash. I have been struggling on this for half an day and still don't know why. Anybody can help me. thank you very much.

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
    print("=== UIViewController sign in")

    if (error == nil) {
        // Perform any operations on signed in user here.
        let userId = user.userID                  // For client-side use only!
        //let idToken = user.authentication.idToken // Safe to send to the server
        let name = user.profile.name
        let email = user.profile.email
        if user.profile.hasImage{
            // crash here !!!!!!!! cannot get imageUrl here, why?
            // let imageUrl = user.profile.imageURLWithDimension(120)
            let imageUrl = signIn.currentUser.profile.imageURLWithDimension(120)
            print(" image url: ", imageUrl.absoluteString)
        }
        // ...
        print(" userId: ", userId)
        //            print(" idToken: ", idToken)
        print(" name: ", name)
        print(" email: ", email)
        labelName.text = name
        labelEmail.text = email
    } else {
        print("\(error.localizedDescription)")
    }
}

the crash data is as follows:

2015-09-30 18:54:46.662 GoogleLogin[96614:] <GMR/INFO> App measurement v.1100000 started
2015-09-30 18:54:46.675 GoogleLogin[96614:5594982] Successfully configured [SignIn].
2015-09-30 18:54:46.675 GoogleLogin[96614:5594982] Failed to configure [].
2015-09-30 18:54:46.676 GoogleLogin[96614:5594982] Subspecs not present, so not configured [Analytics, AdMob, AppInvite, CloudMessaging, Maps].
2015-09-30 18:54:46.676 GoogleLogin[96614:5594982] Subspecs expected to be present [SignIn, Measurement].
2015-09-30 18:54:46.706 GoogleLogin[96614:] <GMR/INFO> Network status has changed. code, status: 2, Connected
=== UIViewController sign in
2015-09-30 18:54:49.335 GoogleLogin[96614:5594982] -[NSURL isFIFEUrl]: unrecognized selector sent to instance 0x7fe248c335c0
2015-09-30 18:56:46.968 GoogleLogin[96614:5594982] -[NSURL isFIFEUrl]: unrecognized selector sent to instance 0x7fe248f11f20
2015-09-30 18:56:46.973 GoogleLogin[96614:5594982] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL isFIFEUrl]: unrecognized selector sent to instance 0x7fe248f11f20'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102304f65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104927deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010230d58d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010225af7a ___forwarding___ + 970
    4   CoreFoundation                      0x000000010225ab28 _CF_forwarding_prep_0 + 120
    5   GoogleLogin                         0x0000000101db6214 -[GIDProfileData imageURLWithDimension:] + 75
    6   GoogleLogin                         0x0000000101d29d0d _TFC11GoogleLogin14ViewController6signInfS0_FTGSQCSo9GIDSignIn_16didSignInForUserGSQCSo13GIDGoogleUser_9withErrorGSQCSo7NSError__T_ + 2541
    7   GoogleLogin                         0x0000000101d2aa98 _TToFC11GoogleLogin14ViewController6signInfS0_FTGSQCSo9GIDSignIn_16didSignInForUserGSQCSo13GIDGoogleUser_9withErrorGSQCSo7NSError__T_ + 88
    8   GoogleLogin                         0x0000000101dbdeb8 __37-[GIDSignIn addCallDelegateCallback:]_block_invoke + 123
    9   GoogleLogin                         0x0000000101db5ab6 -[GIDCallbackQueue fire] + 147
    10  GoogleLogin                         0x0000000101dbdacb __38-[GIDSignIn addDecodeIdTokenCallback:]_block_invoke_2 + 385
    11  libdispatch.dylib                   0x00000001053eaef9 _dispatch_call_block_and_release + 12
    12  libdispatch.dylib                   0x000000010540b49b _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x00000001053f334b _dispatch_main_queue_callback_4CF + 1738
    14  CoreFoundation                      0x00000001022653e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    15  CoreFoundation                      0x0000000102226939 __CFRunLoopRun + 2073
    16  CoreFoundation                      0x0000000102225e98 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x00000001066b5ad2 GSEventRunModal + 161
    18  UIKit                               0x00000001034b0676 UIApplicationMain + 171
    19  GoogleLogin                         0x0000000101d2c36d main + 109
    20  libdyld.dylib                       0x000000010543f92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

And it says signIn.currentUser.profile.imageURLWithDimension(120) is invalid expression: enter image description here

ajmccall
  • 2,024
  • 23
  • 42
DàChún
  • 4,751
  • 1
  • 36
  • 39
  • I have almost the exact same code in my project, using a value of `200` works. Are you unable to give me details about the crash? – ajmccall Sep 30 '15 at 16:53
  • thank you @ajmccall , I added the crash log and updated the post. I tried 200, it didn't work for me. – DàChún Sep 30 '15 at 17:02
  • Which version of Google's SDK are you using? Are you using cocoapods to manage it? It's looks like a typo somewhere `[NSURL isFIFEUrl]` should probably be `[NSURL isFILEUrl]:` – ajmccall Sep 30 '15 at 17:05
  • Yes, I use cocoapods. pod 'Google/SignIn' . But I am using swift, where to write [NSURL isFIFEUrl] ? – DàChún Sep 30 '15 at 17:05
  • Version? I'm using `Google (1.0.7)` and `GoogleSignIn (2.2.0)` – ajmccall Sep 30 '15 at 17:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91018/discussion-between-ajmccall-and-user9527). – ajmccall Sep 30 '15 at 17:10

6 Answers6

19

For Objective C :

if (user.profile.hasImage)
{
    NSURL *url = [user.profile imageURLWithDimension:100];
    NSLog(@"url : %@",url);
}

For Swift :

if user.profile.hasImage
{
    let pic = user.profile.imageURLWithDimension(100) 
    Print(pic)
}

Note : 100 is the dimension. Its depend on your requirement.

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
7

Swift 3

let dimension = round(imageSize.width * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)

OR

let dimension = round(100 * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
4

Exactly like @ajmccall said, a typo error!

But you may workaround it by category NSURL.

New an Objective-C class:

NSURL+FILE.h

#import <Foundation/Foundation.h>

@interface NSURL (FILE)
- (BOOL) isFIFEUrl;
@end

NSURL+FILE.m

import "NSURL+FILE.h"

@implementation  NSURL (FILE)
- (BOOL) isFIFEUrl {
    return [self isFileURL];
}

@end
Community
  • 1
  • 1
pinxue
  • 1,736
  • 12
  • 17
3

It's a bug in GoogleSign 1.10 pod, and it's a typo bug at that. They are calling [NSURL isFIFEUrl] when they probably be calling [NSURL isFILEUrl]. (Ho ho ho that is hilarious!)

Simply downgrade to Google/Sign 1.0.7 and it will work.

ajmccall
  • 2,024
  • 23
  • 42
  • downgrade to GoogleSign 2.2.0. but compile failed: Use of unresolved identifier 'GGLContext'. Still trying. – DàChún Sep 30 '15 at 18:06
  • Hi, change your Podfile to read `pod 'Google/SignIn', '1.0.7'` and type `pod install`. – ajmccall Sep 30 '15 at 19:03
  • Hi ajmccall, I tried SDK 2.2.0 and 1.0.7, neither of them works for me. Pinxue's solution works for me. I know you spent lot of time on this issue, but I have to choose Pinxue's solution. Anyway, thank you so much for your efforts. – DàChún Sep 30 '15 at 20:17
  • Alright, but you're going to need to learn how `pod` works very soon. – ajmccall Sep 30 '15 at 21:36
2

Run pod update to get 2.3.1 of the SDK.

Background info if you care: It's actually not a typo, even though it looks like one. Version 2.3 of the Google sign-in library makes use of another library -- libGIPNSURL+FIFE.a -- that was accidentally omitted in version 2.3.0.

Todd Kerpelman
  • 16,875
  • 4
  • 42
  • 40
0

Are the Subspecs messages just warnings or errors; have this atm:

2015-10-06 18:21:44.289 Blotter[691:739797] Configuring subspecs failed @ CloudMessaging 2015-10-06 18:21:44.311 Blotter[691:739797] Successfully configured []. 2015-10-06 18:21:44.312 Blotter[691:739797] Failed to configure [CloudMessaging]. 2015-10-06 18:21:44.312 Blotter[691:739797] Subspecs not present, so not configured [Analytics, AdMob, SignIn, AppInvite]. 2015-10-06 18:21:44.312 Blotter[691:739797] Subspecs expected to be present [Measurement].

iHolm
  • 131
  • 1
  • 2