fb_license

技術標籤

@selector (1) 初使化區塊 (1) 物件 (1) 物件導向 (2) 型別 (4) 封裝 (1) 流程控制 (1) 陣列 (3) 推論型別 (2) 實機測試 (1) 蓋索林(gasolin) (1) 模組 (1) 憑證 (1) 轉型 (1) 羅康鴻 (121) 類別 (1) 變數 (5) Accelerometer (1) ActiveRecord (1) Activity (1) AFNetworking (1) alloc (1) Android (3) Animation (1) App (1) App ID (1) APP上傳 (1) ASP.NET (1) AVAudioPlayer (1) block (1) C# (2) class (1) CLLocationManager (1) CLLocationManagerDelegate (1) CMMotionManager (4) Controller (1) delegate (1) DELETE語法 (1) Device Motion資料 (1) Dialog (1) DropDownList (1) dynamic language (2) Facebook SDK (9) FBRequest (5) FBRequestConnection (2) FMDB (1) Gesture Recognizers (6) GROUP BY (2) Gyro (1) HAVING (1) IBAction (1) IBOutlet (1) id (3) inheritance (1) init (1) Insert (1) instance variable (1) Interface Builder (1) iOS (70) iOS idea (7) iOS Introduction (1) Layout (1) Magnetometer (1) Menu (2) Method (2) MKMapView (1) MKPointAnnotation (1) MS SQL (5) Nil (1) NSArray (1) NSDictionary (1) NSError (1) NSFileManager & .plist (1) NSMutableArray (1) NSMutableDictionary (1) NSNotificationCenter (1) NULL (1) object (2) Objective-C (16) Objective-C idea (1) ORDER BY (1) Parameter (1) property (1) protocol (2) Provisioning (1) Proximate Sensor (1) Q and A (2) R類別 (1) Rails (9) RESTful SOA (9) Ruby (8) Scene (1) SEELECT (1) Segue (2) SEL (1) SELECT語法 (1) Shake (1) Simulator (1) SOA (8) SQL (6) SQL Server (5) SQL函數 (1) SQL彙總函數 SQL (1) SQLite (1) Storyboard (1) Style (1) Swift (1) Table (1) target & action (1) Theme (1) Toast (1) TRUNCATE TABLE語法 (1) UIActionSheet (1) UIActionSheetDelegate (1) UIActivityIndicatorView (1) UIAlertView (1) UIBarButtonItem (1) UIButton (1) UICollectionView (1) UICollectionViewDataSource (1) UIControl (9) UIDatePicker (1) UIImage (1) UIImagePickerController (2) UIImagePickerControllerDelegate (2) UIImageView (1) UILabel (1) UILongPressGestureRecognizer (1) UINavigationController (2) UIPanGestureRecognizer (1) UIPinchGestureRecognizer (1) UIProgressView (1) UIResponder (1) UIRotationGestureRecognizer (1) UISegmentedControl (1) UISlider (1) UIStepper (1) UISwipeGestureRecognizer (1) UISwitch (1) UITabBarController (1) UITableView (1) UITableViewDataSource (1) UITapGestureRecognizer (1) UITextField (1) UITextFieldDelegate (1) UITextView (2) UITextViewDelegate (1) UIToolBar (1) UIView (8) UIWebView (1) UPDATE語法 (1) var (2) VB.NET (7) View (4) WHERE子句 (1) XML (1)

2013/09/30

[iOS] FBRequest刪除 - requestForDeleteObject:


透過FBRequestrequestForDeleteObject:,刪除Facebook的物件。









事前的準備:
1. 建立2個場景Scene,一個為登入頁面,一個為登入後主頁面

  • 登入頁面:其中有個登入按鈕
  • 登入後主頁面:1個文字標籤Label、1個PO文按鈕、1個刪除PO文按鈕和1個登出按鈕



2. 建立繼承自UIViewController的LoginController,並設定在登入場景中



3. 建立繼承自UIViewController的MainController,並設定在登入後的主場景中



4. 設定登入場景轉場至主場景的轉場Segue,並命名為go_main






requestForDeleteObject:可以以行除Facebook的物件,在此以PO文物件為例,現在開始建立刪除PO文的相關程式!

第1步:登入並取得刪除PO文權限

  • 在"登入"按鈕上建立對應的action - loginTocuh

  • 使用FBSession的+openActiveSessionWithPublishPermissions:...開啟Session,並帶入以陣列方式帶入刪除物件所需的權限@[@"publish_actions"],其與PO文的權限也相同,其中的defaultAudience:參數給予FBSessionDefaultAudienceEveryone表示PO的文所有人都看的到。在成功登入後即透過"go_main"轉場segue,轉場至主頁面



- (IBAction)loginTouch:(id)sender { //透過+[openActiveSession allowLoginUI:YES...]進行Facebook登入, //這次使用openActiveSessionWithPublishPermissions取得可以刪除物件的請求, //當然,刪除物件的權限與PO文的權限相同,以陣列的方式@[@"publish_actions"]帶入, //defaultAudience參數代入FBSessionDefaultAudienceEveryone表示PO文的資訊所有人皆可看的到 [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"] defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session , FBSessionState status , NSError *error) { if(error != nil){ NSLog(@"error:%@", [error description]); } //若無error且session的status等於FBSessionStateOpen //成功登入Facebook開啟連結 if(error == nil && status == FBSessionStateOpen) { //則透過轉場識別字"go_main"轉場至MainController [self performSegueWithIdentifier:@"go_main" sender:self]; } }]; }


第2步:PO文
  • 在"PO文"按鈕上建立對應的action - postTouch,同時加上objectID屬性,以將PO文後的物件ID - id保存下來,後續才能透過物件ID進行刪除的動作

  • 透過FBRequest的requestForPostStatusUpdate:建立PO文的請求,接著建立FBRequestConnection物件,透過addReuqest:加入reuqest至FBRequestConnection中,在透過FBRequestConnection物件的start方法將reuqest實際送出,以實際執行PO文的動作,注意,在成功PO文後,會將PO文的物件ID保存在objectID屬性中。
- (IBAction)postTouch:(id)sender { //透過FBRequest的requestForPostStatusUpdate:若得PO文的FBReuqest請求物件, //其中帶入要PO文的訊息"來自於iOS程式的訊息。" FBRequest *request = [FBRequest requestForPostStatusUpdate:@"來自於iOS程式的訊息。"]; //透過FBReuqestConnection物件 FBRequestConnection *connection = [[FBRequestConnection alloc] init]; //FBReuqestConnection物件透過addRequest加PO文的FBRequest加入其中 //其中的copletionHandler參數則帶入一個block, //此block會在FBReuqestConnection將FBRequest送出後執行。 [connection addRequest:request completionHandler:^(FBRequestConnection *connection , id result , NSError *error) { NSString *message = nil; //若error不為nil表示執行成功, //則message帶入PO文成功字樣, //若不成功則帶入PO文失敗字樣。 if(error == nil) { message = @"PO文成功,請至您的Facebook版面查看訊息。"; //成功PO文後,將物件id表存在objectID屬性中 self.objectID = [result objectForKey:@"id"]; } else { //message = @"PO文失敗!"; message = [error description]; } //透過UIAlertView顯示PO文結果 UIAlertView *view = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"關閉" otherButtonTitles: nil]; [view show]; }]; //透過FBReuqestConnection的start, //實際將透過addReuqest方法加入的request送出, //以執行request中的PO文動作。 [connection start]; }


第3步:刪除PO文

  • 在"刪除PO文"按鈕上建立對應的action - deleteTouch





  • 透過FBRequest的requestForDeleteObject:建立刪除物件的請求,帶入先前PO文後的物件id,其存放在objectID屬性中,所以透過objectID轉帶入至requestForDeleteObject:的參數中。接著建立FBRequestConnection物件,透過addReuqest:加入reuqest至FBRequestConnection中,在透過FBRequestConnection物件的start方法將reuqest實際送出,以實際執行刪除PO文的動作。
  •   - (IBAction)deleteTouch:(id)sender { //透過FBRequest的requestForDeleteObject:取得刪除物件的FBReuqest請求物件, //其中帶入要objectID屬性,即是PO先前PO文後文後的物件ID FBRequest *request = [FBRequest requestForDeleteObject:self.objectID]; //透過FBReuqestConnection物件 FBRequestConnection *connection = [[FBRequestConnection alloc] init]; //FBReuqestConnection物件透過addRequest加刪除PO文的FBRequest加入其中 //其中的copletionHandler參數則帶入一個block, //此block會在FBReuqestConnection將FBRequest送出後執行。 [connection addRequest:request completionHandler:^(FBRequestConnection *connection , id result , NSError *error) { NSString *message = nil; //若error不為nil表示執行成功, //則message帶入PO文成功字樣, //若不成功則帶入PO文失敗字樣。 if(error == nil) { message = @"刪除PO文成功,請至您的Facebook版面查看訊息。"; } else { //message = @"刪除PO文失敗!"; message = [error description]; } //透過UIAlertView顯示PO文結果 UIAlertView *view = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"關閉" otherButtonTitles: nil]; [view show]; }]; //透過FBReuqestConnection的start, //實際將透過addReuqest方法加入的request送出, //以執行request中的刪除PO文動作。 [connection start]; }

    第4步:執行
    • 接下登入,此時會出現取得權限的畫面,點選"PO文"後至您的Facebook查看此文,而後在點選"刪除PO文"在回到Facebook查看此文已不存在,已被刪除









    別忘了補上登出程式

    • 為"登出"按鈕加上action - logoutTouch

    • 透過closeAndClearTokenInformation關閉session以登出Facebook,並透過dismissViewControllerAnimated:方法關閉目前所在的場景

    - (IBAction)logoutTouch:(id)sender { //透過closeAndClearTokenInformation關閉session以登出Facebook [FBSession.activeSession closeAndClearTokenInformation]; //關閉此登出畫面 [self dismissViewControllerAnimated:YES completion:nil]; }






    檔案連結:myiosapp_requestForDeleteObject.zip (此Facebook AppID已刪除,僅提供程式參考)



    還有什麼物件可以刪除?