ios google登录,如何获取用户图片url?

我正在iOS / Swift谷歌登录。 我在github上做了一个演示。 我的演示项目: https : //github.com/tanggod/GoogleSignIn.git

根据谷歌( https://developers.google.com/identity/sign-in/ios/api/interface_g_i_d_profile_data#a8e229bffe211894473058c4ba247553c ),可以像这样获取用户图像url:

  • (NSURL *)imageURLWithDimension:(NSUInteger)维度
    获取方块每一侧的给定维度的用户配置文件图像URL(以像素为单位)。

但是,当我尝试获取imageURLWithDimension(120)时,应用程序总是崩溃。 我一直在努力争取半天,但仍然不知道为什么。 任何人都可以帮助我。 非常感谢你。

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)") } } 

崩溃数据如下:

 2015-09-30 18:54:46.662 GoogleLogin[96614:]  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:]  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) 

并且它表示signIn.currentUser.profile.imageURLWithDimension(120)是无效的表达式: 在此处输入图像描述

正如@ajmccall所说,错字!

但您可以按类别NSURL解决此问题。

新的Objective-C类:

NSURL + FILE.h

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

NSURL + FILE.m

导入“NSURL + FILE.h”

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

对于目标C:

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

对于Swift:

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

注意:100是维度。 这取决于您的要求。

斯威夫特3

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

要么

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

这是GoogleSign 1.10 pod中的一个错误 ,这是一个错误的错误 。 当他们可能正在调用[NSURL isFIFEUrl]时,他们正在调用[NSURL isFILEUrl] 。 (Ho ho ho,这很有趣!)

只需降级到Google/Sign 1.0.7

运行pod update以获得SDK的2.3.1。

背景信息,如果你关心:它实际上不是一个错字,即使它看起来像一个。 Google登录库的2.3版使用了另一个库 – libGIPNSURL+FIFE.a – 在版本2.3.0中意外省略了该库。

Subspecs消息是警告还是错误; 有这个atm:

2015-10-06 18:21:44.289 Blotter [691:739797]配置子规格失败@ CloudMessaging 2015-10-06 18:21:44.311 Blotter [691:739797]成功配置[]。 2015-10-06 18:21:44.312 Blotter [691:739797]无法配置[CloudMessaging]。 2015-10-06 18:21:44.312 Blotter [691:739797]子目录不存在,因此未配置[Analytics,AdMob,SignIn,AppInvite]。 2015-10-06 18:21:44.312 Blotter [691:739797]预计将出现亚种[测量]。