`
hunxiejun
  • 浏览: 1146218 次
文章分类
社区版块
存档分类
最新评论

facebook iOS Tutorial (分享到facebook)

 
阅读更多

转自:http://developers.facebook.com/docs/guides/mobile/ios/


If you are an iOS developer then it's easy to get started with Facebook integration.

This tutorial walks you through the key steps to build your social iOS app.

We also have additional code samples to get you started in theSample Appssection.

Register your iOS App with Facebook

To begin integrating with the Facebook Platform,register your mobile website with Facebookand enter your app's basic information.


Note your App ID. You are going to need it when setting up your app in Xcode.


You application is now set up and you’re ready to begin integrating with Facebook!

Install the iOS SDK

Before you begin development with the Facebook iOS SDK, you will need to install the iOS dev tools, Git (the source control client we use for this SDK) and then clone the lastest version of the SDK fromGitHub:

Once you have everything installed you are now ready to set up your iOS app. First we will discuss Single Sign-On (SSO) which is a key implementation feature you should provide. After that section we will direct you to the SSO implementation where we walk you through the steps of setting up an iOS app.

Enable Single Sign-On for your App

In the initial release of the SDK, the authorize method always opened an inline dialog containing a UIWebView in which the authorization UI was shown to the user. Each iOS application has its own cookie jar, so this mechanism had a major disadvantage: it required users to enter their credentials separately for each app they authorized.

In the updated version of the SDK, we changed the authorization mechanism so that users no longer have to re-enter their credentials for every application on the device they want to authorize. The new mechanism relies on iOS's fast app switching. It works as follows:

If the app is running in a version of iOS that supports multitasking, and if the device has the Facebook app of version 3.2.3 or greater installed, the SDK attempts to open the authorization dialog withing the Facebook app. After the user grants or declines the authorization, the Facebook app redirects back to the calling app, passing the authorization token, expiration, and any other parameters the Facebook OAuth server may return.

If the device is running in a version of iOS that supports multitasking, but it doesn't have the Facebook app of version 3.2.3 or greater installed, the SDK will open the authorization dialog in Safari. After the user grants or revokes the authorization, Safari redirects back to the calling app. Similar to the Facebook app based authorization, this allows multiple applications to share the same Facebook user session through the Safari cookie.

If the app is running a version of iOS that does not support multitasking, the SDK uses the old mechanism of popping up an inline UIWebView, prompting the user to log in and grant access. TheFBSessionDelegateis a callback interface that your app should implement: The delegate's methods will be invoked when the app successful logs in or logs out. Read theiOS SDK documentationfor more details on this delegate.

Adding SSO support to your app is easy to do. Read theSingle Sign-Ondocument for a step-by-step guide on how to implement SSO.

Once you have set up a sample app with SSO support you can return to learn about some additional iOS SDK features.

Add Log Out to your App

When the user wants to stop using Facebook integration with your app, you can call thelogoutmethod to clear the app state and make a server request to invalidate the currentaccess_token.

[facebook logout:self];

Note that logging out will not revoke your application's permissions, but will simply clear your application'saccess_token. If a user that has previously logged out of your app returns, they will simply see a notification that they are logging into your app, not a notification to grant permissions. To modify or revoke an application's permissions, the user must visit the"Applications, Games, and Websites" tab of their Facebook privacy settings dashboard. You can also revoke an app's permissions programmatically using aGraph API call.

Use the Graph API

The FacebookGraph APIpresents a simple, consistent view of the Facebook social graph, uniformly representing objects in the graph (e.g., people, photos, events, and fan pages) and the connections between them (e.g., friend relationships, shared content, and photo tags).

You can access the Graph API by passing the Graph Path to the request method. For example, to access information about the logged in user, call:

// get information about the currently logged in user
[facebook requestWithGraphPath:@"me" andDelegate:self];

// get the posts made by the "platform" page
[facebook requestWithGraphPath:@"platform/posts" andDelegate:self];

// get the logged-in user's friends
[facebook requestWithGraphPath:@"me/friends" andDelegate:self];

Your delegate object should implement theFBRequestDelegateinterface to handle your request responses.

Note that the server response will be in JSON string format. The SDK uses an open source JSON libraryhttp://code.google.com/p/json-framework/to parse the result. If a parsing error occurs, the SDK will callbackrequest:didFailWithError:in your delegate.

A successful request will callbackrequest:didLoad:in your delegate. The result passed to your delegate can be anNSArray, if there are multiple results, or anNSDictionaryif there is only a single result.

Advanced applications may want to provide their own custom parsing and/or error handling, depending on their individual needs.

Read theiOS SDK documentationfor more details on theFBRequestDelegatedelegate.

Use the Facebook Dialogs

The iOS SDK provides a method for displaying aFacebook Platform dialog. The currently supported dialogs are:

  • OAuth Dialog- the login and permissions dialogs used in the authorization flow.

  • Feed Dialog- the dialog used for publishing posts to a user's feed.

To invoke a dialog:

[facebook dialog:@"feed" andDelegate:self];

This allows you to provide basic Facebook functionality in your app with a singe line of code -- no need to build native dialogs, make API calls, or handle responses.

Handle Errors

Errors are handled by theFBRequestDelegateandFBDialogDelegateinterfaces. Applications can implement these interfaces and specify delegates as necessary to handle any errors.

Read more about error handling in theiOS reference documentation.


Sample Apps

The Github repository contains a sample application that showcases the iOS SDK features.


分享到:
评论

相关推荐

    iOS tutorial

    《iOS tutoral》,包含如下内容 1. Objective-C 2. UI element 3. Audio/Video 4. map/location 5. web 6. file ...

    ios tutorial

    ios tutorial

    iOS.10.by.Tutorial

    iOS.10.by.Tutorial 學習iOS10 必看的好文檔 舊版本,可以找找新版

    Facebook流行框架使用教程Facebook-POP-Tutorial.zip

    This project is a tutorial on how to use the popular through Facebook framework. It is very easy and efficient framework that can help you make a decent movie in no time. In this project, you will ...

    iOS_tutorial

    很好的、全面、系统客观地列出了iOS的全貌,每个知识点都会有个生动例子,不错的概念书,可以让你知道如何去掌握iOS开发

    iOS 10 App Development Essentials

    103. An iOS 10 Social Media Integration Tutorial using UIActivityViewController 104. iOS 10 Facebook and Twitter Integration using SLRequest 105. An iOS 10 Twitter Integration Tutorial using SLRequest...

    iOS_5_By_Tutorials中文版.pdf

    当 raywenderlich.com iOS Tutorial Team 第一次开始研究 iOS 5 时,丰富的新的类库、新 的 API 和新的功能让我们很惊讶!这已经是 iOS 里的最大的升级了,包括成千上万的新成 员,你可以在程序里开始使用这些成员了...

    iOS_11_by_Tutorials_v1.1

    iOS_11_by_Tutorials_v1.1d电子书和代码 ,因为太大(100M+)没有上传权限(只能传70M左右)所以用的网盘地址,自己下载,如果失效请评论留言,仅用于交流学习,3Q

    IOSTutorial

    IOSTutorial

    iOS7 by Tutorials.zip

    iOS7 by Tutorial

    iOS 7 by Tutorial (pdf)

    iOS 7 by Tutorials is made for iOS 7 and Xcode 5. This book is for intermediate or advanced iOS developers, who already know the basics of iOS development but want to upgrade their skills to iOS 7. ...

    ios apprentice tutorial 1:getting started

    通过一个iOS开发示例学习iOS开发,并且了解object-c,xcode的使用。

    iOS.8.App.Development.Essentials

    An iOS 8 Facebook Integration Tutorial using UIActivityViewController Chapter 92. iOS 8 Facebook and Twitter Integration using SLRequest Chapter 93. An iOS 8 Twitter Integration Tutorial using ...

    iOS 5 By Tutorials(中文版)

    IOS中高级书籍 适合初级中级程序员 内容范围广 层次深 非常好的一本书 如果新手可以看另一本书iOS 6 Programming Cookbook 在我的另一个资源里有下载链接

    iOS 5 by Tutorials

    iOS iOS5 tutorial 教程

    the iOS Apprentice Chapter 4

    The iOS Apprentice 新手教程,对刚入门的有很大的指导意义,不过这个是英文版的,有网页版,使用带翻译的浏览器就能看懂,因为内容过大,这个只是第四章,还有后续的章节。

    facebookforios tutorial

    facebook skd集成到ios 工程当中的教程,是很好的教程呀。

    iOS.9.App.Development.Essentials

    An iOS 9 Social Media Integration Tutorial using UIActivityViewController Chapter 98. iOS 9 Facebook and Twitter Integration using SLRequest Chapter 99. An iOS 9 Twitter Integration Tutorial using ...

    Tutorial

    Tutorial

Global site tag (gtag.js) - Google Analytics