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

2024-02-08

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

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

  • (NSURL *) imageURLWithDimension: (NSUInteger) 维度
    获取正方形每条边给定尺寸(以像素为单位)的用户个人资料图像 URL。

但是,当我尝试获取尺寸为(120)的图像 URL 时,应用程序总是崩溃。我为此苦恼了半天,还是不知道为什么。 任何人都可以帮助我。非常感谢。

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:] <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


对于目标 C :

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

对于斯威夫特:

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

注:100为尺寸。这取决于您的要求。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ios google登录,如何获取用户图片url? 的相关文章

随机推荐

  • 如何将配置变量添加到我的 CMake 脚本中?

    我想添加一个用户在 cmake gui 中单击 配置 后必须设置的变量 有没有办法做到这一点 使用 set 命令指定 CACHE 参数 例如 set NAME INCLUDE default value CACHE FILEPATH des
  • ColdFusion 非作用域与变量作用域:性能与可读性?

    在我的 ColdFusion 代码中 我养成了始终将变量作用域视为默认作用域的习惯 即 当另一个作用域不适合时 我的理解是 这提高了效率 因为 ColdFusion 处理器不必花费周期来确定变量所包含的范围 然而 我一直对这让我的代码如此冗
  • 随机生成数字、字母、符号密码

    我想生成一个随机密码 它应该由符号 字母和数字组成 例如 tqpV4aJ 我 WZuYvBv7 S OPToyu0u a 包含6 10个字符 b 恰好包含集合 中的 1 个符号 c 恰好包含 1 位数字 即 0 9 中的任何数字 但不包括数
  • 如何判断 Socket 何时已断开连接

    在客户端 我需要知道我的套接字连接何时 是否已断开 但是 Socket Connected 属性始终返回 true 即使在服务器端已断开连接并且我尝试通过它发送数据之后也是如此 谁能帮我弄清楚这里发生了什么事 我需要知道套接字何时被断开 S
  • webpackHtmlPlugin:控制注入文件的顺序

    I use HtmlWebpackPlugin用于自动生成index thml与 webpack 编译的输出 出于性能原因 我将条目分成vendors and project 像那样 entry vendors vendors js Tim
  • 跟踪目录中的所有文件以 git LFS 但忽略该目录中存在的单个文件夹

    假设我有一个目录 my dir 和其中的一些文件 我其中有一个子目录 my dir subdir 文件结构 我的目 录 我的目录子目录 如何通过 git LFS 跟踪 my dir 但不通过 git LFS 跟踪 my dir my dir
  • 将深度复制构造函数添加到 std::unique_ptr

    我想储存一些std unique ptr
  • 在 OpenCV 上获取屏幕尺寸

    如何获取 OpenCV 上的计算机屏幕分辨率 我需要使用整个屏幕宽度并排显示两个图像 OpenCV 需要我想要创建的确切窗口大小 您可以在有或没有 opencv 的情况下使用此解决方案跨平台解决方案 if WIN32 include
  • 为什么 Vue.js 允许推送到 prop 数组?

    当我们尝试直接更改 prop 值时 Vue js 会显示警告 如下所示 Vue component Games template div ol li game li ol div
  • 在C#中,有没有办法始终能够获取当前焦点窗口的选定文本内容?

    在我的 c Net 应用程序中 我一直在尝试能够检索当前聚焦窗口中当前选定的文本 注意可以是windows中打开的任何窗口 比如word 或者safari 我能够检索当前聚焦控件的句柄 使用对 user32 dll 和 kernel32 d
  • C++ 中阿拉伯字符串的反转

    如何使用 C 反转阿拉伯字符串 例如 的反义词是 阿拉伯字母的形状根据单词中的位置而不同 词首 词中或词尾 连接阿拉伯字母还有其他规则吗 正如 Petesh 所说 根据我能找到的参考资料 例如维基百科 http en wikipedia o
  • d3 色阶 - 与多种颜色呈线性?

    我正在尝试创建一些类似于量化标度的东西 但其行为类似于线性色标 当我尝试将多种颜色放入线性比例时 它似乎只在前两种颜色之间进行缩放 我想要多种颜色 例如量化比例 但在这些颜色之间淡入淡出 我不确定这是否可能 red and green wo
  • NoSuchMethodError:Jersey 客户端中的 MultivaluedMap.addAll

    我正在尝试使用 Jersey Client 模拟对我的 Web 服务的 HTTP 请求 我尝试实施简单的例子 http jersey java net documentation latest user guide html d0e2365
  • 如何在 Swift 中将键分配给 SKActions

    我希望有人能够帮助我解决这个问题 我似乎找不到一种方法来为removeActionWithKey 方法的Sprite Kit 的SKAction 分配键 我还尝试将操作分配给字典中的键 但程序无法识别键分配 因此返回零值 这是我尝试做的 v
  • C++中的动态对象[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我从c 转到c 我不明白什么是动态对象 所以想象一下你有 A 类并创建像 A a new A 这样的对象是正常的 但是对象 a 是什么 它和
  • 客户端服务器程序的多线程

    我正在尝试使用我一直在开发的客户端 服务器程序来实现多线程 我需要允许多个客户端同时连接到服务器 我目前有 4 个类 一个客户端 一个服务器 一个协议和一个处理线程的工作人员 以下代码是我为这些类编写的代码 套接字服务器类 public c
  • Jmeter 而控制器似乎没有将变量评估为数字

    我正在编写一个 jmeter 脚本 该脚本会不断加载数据 直到表达到指定的大小 我有一个 while 循环 其中有一个 HTTP 采样器来加载数据 然后是另一个带有 XPath 后处理器的 HTTP 采样器来检查表大小 它们调用两个不同的
  • get_dummies (Pandas) 和 OneHotEncoder (Scikit-learn) 之间的优缺点是什么?

    我正在学习不同的方法将分类变量转换为机器学习分类器的数字 我遇到了pd get dummies方法和sklearn preprocessing OneHotEncoder 我想看看它们在性能和使用方面有何不同 我找到了一个关于如何使用的教程
  • 树形视图闪烁?

    我开始知道 通过添加 TreeView BeginUpdate 将防止树视图闪烁 但是当我将其添加到我的项目中时 树视图的所有节点都会消失 任何人都可以告诉我为什么会发生这种情况 这是我使用 TreeView 的代码片段 BeginUpda
  • ios google登录,如何获取用户图片url?

    我正在研究 iOS Swift 谷歌登录 我在github上做了一个demo 我的演示项目 https github com tanggod GoogleSignIn git https github com tanggod GoogleS