我正在尝试导入 GoogleAPIClient 或 GoogleAPIClientForREST

2024-04-17

我正在努力追随谷歌的教程 https://developers.google.com/drive/ios/quickstart?ver=swift制作他们的 QuickStart 应用程序来学习如何使用 Swift 进行 API 调用。我完全按照教程进行操作并最终得到了这段代码

import GoogleAPIClient
import GTMOAuth2
import UIKit

class ViewController: UIViewController {

    private let kKeychainItemName = "Drive API"
    private let kClientID = "592019061169-nmjle7sfv8i8eahplae3cvto2rsj4gev.apps.googleusercontent.com"

    // If modifying these scopes, delete your previously saved credentials by
    // resetting the iOS simulator or uninstall the app.
    private let scopes = [kGTLAuthScopeDriveMetadataReadonly]

    private let service = GTLServiceDrive()
    let output = UITextView()

    // When the view loads, create necessary subviews
    // and initialize the Drive API service
    override func viewDidLoad() {
        super.viewDidLoad()

        output.frame = view.bounds
        output.editable = false
        output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
        output.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]

        view.addSubview(output);

        if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychainForName(
            kKeychainItemName,
            clientID: kClientID,
            clientSecret: nil) {
            service.authorizer = auth
        }

    }

    // When the view appears, ensure that the Drive API service is authorized
    // and perform API calls
    override func viewDidAppear(animated: Bool) {
        if let authorizer = service.authorizer,
            let canAuth = authorizer.canAuthorize, canAuth {
            fetchFiles()
        } else {
            presentViewController(
                createAuthController(),
                animated: true,
                completion: nil
            )
        }
    }

    // Construct a query to get names and IDs of 10 files using the Google Drive API
    func fetchFiles() {
        output.text = "Getting files..."
        let query = GTLQueryDrive.queryForFilesList()
        query.pageSize = 10
        query.fields = "nextPageToken, files(id, name)"
        service.executeQuery(
            query,
            delegate: self,
            didFinishSelector: "displayResultWithTicket:finishedWithObject:error:"
        )
    }

    // Parse results and display
    func displayResultWithTicket(ticket : GTLServiceTicket,
                                 finishedWithObject response : GTLDriveFileList,
                                 error : NSError?) {

        if let error = error {
            showAlert("Error", message: error.localizedDescription)
            return
        }

        var filesString = ""

        if let files = response.files(), !files.isEmpty {
            filesString += "Files:\n"
            for file in files as! [GTLDriveFile] {
                filesString += "\(file.name) (\(file.identifier))\n"
            }
        } else {
            filesString = "No files found."
        }

        output.text = filesString
    }


    // Creates the auth controller for authorizing access to Drive API
    private func createAuthController() -> GTMOAuth2ViewControllerTouch {
        let scopeString = scopes.joinWithSeparator(" ")
        return GTMOAuth2ViewControllerTouch(
            scope: scopeString,
            clientID: kClientID,
            clientSecret: nil,
            keychainItemName: kKeychainItemName,
            delegate: self,
            finishedSelector: "viewController:finishedWithAuth:error:"
        )
    }

    // Handle completion of the authorization process, and update the Drive API
    // with the new credentials.
    func viewController(vc : UIViewController,
                        finishedWithAuth authResult : GTMOAuth2Authentication, error : NSError?) {

        if let error = error {
            service.authorizer = nil
            showAlert("Authentication Error", message: error.localizedDescription)
            return
        }

        service.authorizer = authResult
        dismissViewControllerAnimated(true, completion: nil)
    }

    // Helper for showing an alert
    func showAlert(title : String, message: String) {
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: UIAlertControllerStyle.Alert
        )
        let ok = UIAlertAction(
            title: "OK",
            style: UIAlertActionStyle.Default,
            handler: nil
        )
        alert.addAction(ok)
        presentViewController(alert, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

我的问题是对于

import GoogleAPIClient

我收到错误“没有这样的模块 GoogleAPIClient”,这对我来说很奇怪,因为 GTMOAuth2 没有收到错误,即使它是我认为的同一个 Pod 的一部分(我对此很陌生,所以我可能正在破坏术语)。

通过研究这个问题,我发现 GoogleAPIClientForREST 应该替换 GoogleAPIClient。GitHub 上的此文档 https://github.com/google/google-api-objectivec-client-for-rest/wiki/BuildingTheLibrary说只在代码中使用 GoogleAPIClientForREST 而不是 GoogleAPIClient,但我也遇到了同样的错误。

然后我想也许我可以重新安装 Pod,并对 Google 的教程进行一些更改。在教程中,它说在终端中执行此代码

$ cat << EOF > Podfile &&
> platform :ios, '7.0'
> use_frameworks!
> target 'QuickstartApp' do
>     pod 'GoogleAPIClient/Drive', '~> 1.0.2'
>     pod 'GTMOAuth2', '~> 1.1.0'
> end
> EOF
> pod install &&
> open QuickstartApp.xcworkspace

所以我想也许我可以在终端代码中将 GoogleAPIClient 替换为 GoogleAPIClientForREST,但这给我带来了同样的错误

正如您在屏幕截图中看到的,框架位于左侧,但我仍然收到“没有这样的模块”错误。

嵌入式二进制文件和链接框架

搜索路径

I also found some suggestions here https://stackoverflow.com/a/38182451/7120487 that I tried to follow, but I didn't completely understand the explanation. Nevertheless, I tried, and did this (if I did it wrong please tell me): enter image description here

所以我试图让 GoogleAPIClient 或 GoogleAPIClientForREST 工作。感谢您的帮助


将其用于您的 Podfile:

platform :ios, '7.0'
use_frameworks!
target 'QuickstartApp' do
    pod 'GoogleAPIClientForREST/Drive', '~> 1.1.1'
    pod 'GTMOAuth2', '~> 1.1.0'
end

将您的导入更改为

import GoogleAPIClientForREST

然后按照此处的说明迁移项目:从 GoogleAPIClient 迁移到 GoogleAPIClientForREST https://github.com/google/google-api-objectivec-client-for-rest/wiki/Migrating-From-GTL-to-GTLR

这主要涉及通过一些字交换将 GTL 调用更改为 GTLR 调用。例如,GTLServiceDrive变成GTLRDriveService.

关于框架搜索路径,此图显示了您可能需要更改的部分(请注意,它适用于我使用默认值):

搜索路径也可以针对每个目标。这是显示应用程序目标和框架搜索路径的图像:

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

我正在尝试导入 GoogleAPIClient 或 GoogleAPIClientForREST 的相关文章

随机推荐

  • 在 Entity Framework Core 中使用 SQL 视图

    例如 我有这样的模型 public class Blog public int BlogId get set public string Url get set public BlogImage BlogImage get set publ
  • 具有多个服务器的计划任务 - 单点责任

    我们有一个 Spring JPA Web 应用程序 我们使用两个运行应用程序并使用相同数据库的 tomcat 服务器 您的应用程序要求之一是执行 cron 计划任务 经过简短的研究 我们发现 Spring 框架为 cron 作业提供了一个非
  • Struts2中ActionMapper、ActionProxy、ActionInitation、ActionContext对象的范围?

    任何人都可以描述一下我的对象吗 ActionMapper ActionProxy ActionInvocation ActionContext在 Struts2 应用程序中创建 由于我是 Struts2 框架的新手 我对这些对象的范围感到非
  • AWS ElasticSearch:如何将策略应用于索引

    我们有一个 AWS ElasticSearch 域 正在向其中写入记录 文档 我现在已经在 Kibana 中创建了索引状态 生命周期管理 ISM ILM 策略 并且可以将该策略应用于 Kibana 中的索引 现在 我想在从处理索引写入的 J
  • 从 Roslyn ClassDeclarationSyntax 获取类 FullName(包括命名空间)

    我有一个来自 roslyn 语法树的 ClassDeclarationSyntax 我是这样读的 var tree SyntaxTree ParseText sourceCode var root CompilationUnitSyntax
  • d3.event 在去抖动函数内为 null

    当尝试使用 mousemove 事件处理程序的去抖版本时 d3 event is null 我想使用d3 mouse此去抖动处理程序中的对象 但是d3 event返回 null 并抛出错误 我怎样才能访问d3 event在下面的代码中 a
  • 匹配两个不同文件中最接近的值并打印特定列

    大家好 我有两个文件 每个文件都有 N 列和 M 行 File1 1 2 4 6 8 20 4 8 10 12 15 5 7 9 11 File2 1 a1 b1 c5 d1 2 a1 b2 c4 d2 3 a2 b3 c3 d3 19 a
  • 如何在 C# 中获得正确的 HTML 编码?

    我正在尝试从网络词典中获取某个单词的发音 例如 在下面的代码中 我想得到的发音good from http collinsdictionary com http collinsdictionary com HTTP Agility Pack
  • 如何统计SVN分支中更改或添加的行数?

    我想将 SVN 分支中的行更改数量相加 这样我就可以从另一方知道我在项目过程中走了多远 并估计当我将其与主干合并时发生冲突的概率 我能想到的方法是获取统一的 diff 并进行一些 grep wc l hack 但问题是很难分离不同的文件类型
  • 进程退出的问题

    假设我有一个 ID 为 1234 的进程 该进程在我的应用程序运行之前运行 我有这个代码 Process app Process GetProcessById 1234 MessageBox Show app MainWindowTitle
  • WINAPI_FAMILY_PARTITION 有何作用?

    我正在阅读头文件的定义winapifamily h并注意以下定义WINAPI FAMILY PARTITION define WINAPI FAMILY PARTITION Partitions Partitions 该宏的一般用法 作为示
  • 我们可以在javascript中将对象分配给cookie吗?

    有谁知道是否可以在javascript中将一个对象分配给cookies 如果是的话 我们该怎么做呢 如果您可以将对象序列化为其规范的字符串表示形式 并且可以将其从所述字符串表示形式反序列化回其对象形式 那么您可以将其放入 cookie 中
  • 从空整数到逗号列表中的指针的转换

    我知道在我们的现代世界中 NULL 和 0 并不是指针操作的最佳实践 根据 cppreference 指针转换 空指针常量 参见 NULL 可以是 转换为任意指针类型 结果为空指针 该类型的值 这种转换 称为空指针转换 允许作为单个转换转换
  • Activator.CreateInstance 找不到构造函数(MissingMethodException)

    我有一个具有以下构造函数的类 public DelayCompositeDesigner DelayComposite CompositeObject InitializeComponent compositeObject Composit
  • 如果 URL 以 https:// 开头,则不会显示网站图标

    我在使用 favicon ico 时遇到一个问题 这是我的链接相关代码 已包含在标头部分中 问题是 如果 url 以 http 开头 我可以在所有浏览器中查看该图标 当地址以 https 开头时 图标不会在 IE 浏览器中显示 有什么我需要
  • OpenGL固定功能着色器实现[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有任何包装器可以在 OpenGL ES 2 0 之上模拟 OpenGL ES 1 1 API 我进
  • iOS8 - 模拟器 - 如何模拟位置

    我的应用程序跟踪您的位置 对于 iOS8 我必须改变它启动位置服务的方式 我得到了这个工作 self locationManager requestAlwaysAuthorization 并将 NSLocationAlwaysUsageDe
  • 从当前日期减去月份 sql

    我正在尝试从今天减去日期 获取 1 个月前直到永远的报告 到目前为止我已经尝试过 DATE SUB NOW INTERVAL 1 MONTH 这是上下文 SELECT contracts currency ROUND SUM CASE WH
  • 如何将 Google Analytics 跟踪 ID 添加到 GitHub Pages

    可能是一个简单的问题 但我现在对添加充满疑问谷歌分析跟踪ID to GitHub 页面 我正在使用 GitHub 自动页面生成器来创建我的 GitHub 页面 但它要求提供 Google Analytics 跟踪 ID 我尝试注册 Goog
  • 我正在尝试导入 GoogleAPIClient 或 GoogleAPIClientForREST

    我正在努力追随谷歌的教程 https developers google com drive ios quickstart ver swift制作他们的 QuickStart 应用程序来学习如何使用 Swift 进行 API 调用 我完全按