xcode 将 Google Objective-C API 'GTL' 添加到 iPhone 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11370752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Google Objective-C API 'GTL' to iPhone project
提问by Conor Taylor
How to I add the Google Drive API to my iPhone project to I can use it?
如何将 Google Drive API 添加到我的 iPhone 项目中以便我可以使用它?
So far, I have dragged the GTL project into my current app project (so that it is nested under my app project). Then, under my app target's build phases, I added GTL.framework, and then added GTL.framework to my 'Link binary with Libraries' (see attached pic). This throws the following error:
到目前为止,我已经将 GTL 项目拖到了我当前的应用程序项目中(以便它嵌套在我的应用程序项目下)。然后,在我的应用程序目标的构建阶段,我添加了 GTL.framework,然后将 GTL.framework 添加到我的“Link binary with Libraries”(见附图)。这会引发以下错误:
clang: error: no such file or directory: '/Users/xxx/Library/Developer/Xcode/DerivedData/Golf-hfbczyaemhyzgvbrtgdxqnlzeuaa/Build/Products/Debug-iphonesimulator/GTL/GTL'
How to I fix this?
我该如何解决这个问题?
回答by Rob
I struggled with this exact issue for most of the day today, and I found it extremely frustrating. I have finally figured it all out so here is a straightforward step by step guide on how to add the Google API to an iOS7 project using XCode5, using ARC, without having to create Workspaces or any of that.
今天大部分时间我都在为这个确切的问题而苦苦挣扎,我发现它非常令人沮丧。我终于想通了,所以这里是一个简单的分步指南,介绍如何使用 XCode5、使用 ARC 将 Google API 添加到 iOS7 项目,而无需创建工作区或任何其他内容。
The answer provided by RawMean works well, but it gave me issues with ARC. I also didn't like the fact that you had to add project, create a workspace and then delete the project. So my solution will deal with both these issues.
RawMean 提供的答案效果很好,但它给了我 ARC 的问题。我也不喜欢您必须添加项目、创建工作区然后删除项目的事实。所以我的解决方案将处理这两个问题。
- Check out the code. To do this, you can just run
svn checkout http://google-api-objectivec-client.googlecode.com/svn/trunk/ google-api-objectivec-client-read-only
from your terminal. I will refer to this code as "Google's code". - Go to your project's Build Phases. Expand "Link Binary With Libraries" and add
Security.framework
andSystemConfiguration.framework
. These two are required by Google's code. - Go to your project's Build Settings. Using the search box there, look for
Other Linker Flags
(make sure "All" is selected to the left of the search box). Add-ObjC -all_load
. - Now search for
User headers search path
and add the full path to Goggle's/Source
directory. Make sure you selectrecursive
. - Using Finder, go to Google's
/Source/OAuth2/Touch
directory. Drag and dropGTMOAuth2ViewTouch.xib
into your project. - Go back to Finder and go to Google's
/Source
directory. Drag and dropGTLCommon_Sources.m
andGTLCommon_Networking.m
into your project. - Now, you need to import the files for the services you want to use. In my case, I need to use Google Drive, so I'll add those. In finder, go to Google's
/Source/Services/Drive/Generated
directory. Drag and dropGTLDrive.h
andGTLDrive_Sources.m
into your project. If you want to use other services, go to their directory and import the appropriate .h and .m file. - For some reason, Google's code doesn't use ARC, so if you try to build right now, you will get ARC compile errors. So we need to disable ARC for Google's code only. To do this, go back to your project's Build Phases, but this time expand "Compile Sources". Make sure that
GTLCommon_Sources.m
andGTLCommon_Networking.m
are there. Select them, press enter and type in-fno-objc-arc
. This will disable ARC for both of them. Make sure you don't add this option for any other file (unless you know what you're doing). - You are done! Now, whenever you want to use Google's API, just import
GTMOAuth2ViewControllerTouch.h
and your service header. In my case, since I'm using Google Drive, I will also importGTLDrive.h
.
- 查看代码。为此,您只需
svn checkout http://google-api-objectivec-client.googlecode.com/svn/trunk/ google-api-objectivec-client-read-only
从终端运行即可。我将此代码称为“Google 代码”。 - 转到您项目的构建阶段。展开“Link Binary With Libraries”并添加
Security.framework
和SystemConfiguration.framework
。这两个是 Google 的代码所必需的。 - 转到您项目的构建设置。使用那里的搜索框,查找
Other Linker Flags
(确保选择了搜索框左侧的“全部”)。添加-ObjC -all_load
. - 现在搜索
User headers search path
并添加到 Goggle/Source
目录的完整路径。确保选择recursive
. - 使用 Finder,转到 Google 的
/Source/OAuth2/Touch
目录。拖放GTMOAuth2ViewTouch.xib
到您的项目中。 - 返回 Finder 并转到 Google 的
/Source
目录。拖放GTLCommon_Sources.m
,并GTLCommon_Networking.m
为您的项目。 - 现在,您需要为要使用的服务导入文件。就我而言,我需要使用 Google Drive,所以我会添加这些。在 finder 中,转到 Google 的
/Source/Services/Drive/Generated
目录。拖放GTLDrive.h
,并GTLDrive_Sources.m
为您的项目。如果要使用其他服务,请转到其目录并导入相应的 .h 和 .m 文件。 - 出于某种原因,Google 的代码不使用 ARC,因此如果您现在尝试构建,您将收到 ARC 编译错误。所以我们只需要为 Google 的代码禁用 ARC 。为此,请返回项目的构建阶段,但这次展开“编译源”。确保
GTLCommon_Sources.m
并GTLCommon_Networking.m
在那里。选择它们,按回车键并输入-fno-objc-arc
。这将为他们俩禁用 ARC。确保您没有为任何其他文件添加此选项(除非您知道自己在做什么)。 - 你完成了!现在,无论何时您想使用 Google 的 API,只需导入
GTMOAuth2ViewControllerTouch.h
您的服务标头。就我而言,由于我使用的是 Google Drive,因此我还将导入GTLDrive.h
.
I hope that helps and saves some people from pulling all their hair out.
我希望这能帮助并拯救一些人免于拔掉所有头发。
回答by RawMean
I struggled with this error message as well. This is how I solved it:
我也遇到了这个错误消息。我是这样解决的:
Make sure you have added the folder for the service that you are using under GTLSource/Common/ (e.g., add the Drive folder for GoogleDrive).
确保您已在 GTLSource/Common/ 下添加了您正在使用的服务的文件夹(例如,为 GoogleDrive 添加 Drive 文件夹)。
Under GTL.xcodeproj (that you have already added to your workspace) find the GTLSource folder and drag it to your main project (Golf in your case). Done!
在 GTL.xcodeproj(您已经添加到您的工作区)下找到 GTLSource 文件夹并将其拖到您的主项目(在您的情况下为 Golf)。完毕!
Now you can remove references to the GTL.xcodeproj that you have added to the workspace.
现在您可以删除对已添加到工作区的 GTL.xcodeproj 的引用。
With this approach, you don't even need to add the libraries (so remove them from the list of linked libraries if you have added them).
使用这种方法,您甚至不需要添加库(因此,如果已添加,请将它们从链接库列表中删除)。
The Google API documentation is nothing like Apple's documentation (it's not good).
Google API 文档与 Apple 的文档完全不同(不好)。
I should also mention that I'm building an app for iOS and not MacOSX, but this should work for OSX as well.
我还应该提到我正在为 iOS 而不是 MacOSX 构建一个应用程序,但这也应该适用于 OSX。
回答by firescar96
Not only doing the above, but go under "[Project Name] Targets->Build Phases> Compile Sources" and click the + button. Then add all the .m files, for some reason most of the aren't automatically.
不仅要执行上述操作,还要在“[Project Name] Targets->Build Phases> Compile Sources”下单击 + 按钮。然后添加所有 .m 文件,出于某种原因,大多数文件不是自动添加的。
I also had to delete (the reference) to "GTLDrive_Souces.m" from the Drive folder, but I don't understand why i had to do that part.
我还必须从 Drive 文件夹中删除(引用)“GTLDrive_Souces.m”,但我不明白为什么我必须这样做。
回答by Adarsh G J
Better to use Pod
- How to install CocoaPods and setup with your Xcode project for reference : [http://blogs.triffort.com/?p=309][1]
Open the pod file and add
pod 'Google-API-Client/Drive', '~> 1.0'save pod file and call pod install in terminal. Note:pod file you must give link_with 'Your_project_name', 'Your_project_nameTests' after this line only add your library
最好使用 Pod
- 如何安装 CocoaPods 并使用您的 Xcode 项目进行设置以供参考:[ http://blogs.trifort.com/?p=309][1]
打开pod文件并添加
pod 'Google-API-Client/Drive', '~> 1.0'保存 pod 文件并在终端中调用 pod install。注意:pod 文件你必须在这一行之后给 link_with 'Your_project_name', 'Your_project_nameTests' 只添加你的库
回答by Esqarrouth
this is does not really solve the problem of installing Google API's but in this repo I accessed Google Forms from an iOS app without using Google's API. https://github.com/goktugyil/QorumLogs
这并没有真正解决安装谷歌 API 的问题,但在这个 repo 中,我从 iOS 应用程序访问了谷歌表单,而不使用谷歌的 API。https://github.com/goktugyil/QorumLogs
So you may skip the installing API part in some projects
所以有些项目你可以跳过安装API部分
Here is the tutorial of how to set it up: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md
这是如何设置它的教程:https: //github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md
Heres the code to do it:
这是执行此操作的代码:
private static var googleFormLink: String!
private static var googleFormAppVersionField: String!
private static var googleFormUserInfoField: String!
private static var googleFormMethodInfoField: String!
private static var googleFormErrorTextField: String!
/// Setup Google Form links
static func setupOnlineLogs(#formLink: String, versionField: String, userInfoField: String, methodInfoField: String, textField: String) {
googleFormLink = formLink
googleFormAppVersionField = versionField
googleFormUserInfoField = userInfoField
googleFormMethodInfoField = methodInfoField
googleFormErrorTextField = textField
}
private static func sendError(#text: String) {
var url = NSURL(string: googleFormLink)
var postData = googleFormAppVersionField + "=" + text
postData += "&" + googleFormUserInfoField + "=" + "anothertext"
postData += "&" + googleFormMethodInfoField + "=" + "anothertext"
postData += "&" + googleFormErrorTextField + "=" + "anothertext"
var request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)
var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true)
}