头文件中加入

UIActionSheetDelegate, FBRequestDelegate, FBDialogDelegate

三个协议

成员变量中加入

FBSession *mFBSession;

 void *mRawData;

 UIImage *mSubmitImage;

 BOOL mIsCheckLogin;

加入

@property (nonatomic, retain) UIImage *submitImage;

- (void)uploadPhoto;

- (void)checkLogin;

- (void)showStreamDialog:(NSString *)src link:(NSString *)imageURL;

- (void)beginWaiting;

- (void)endWaiting;

方法
.m文件中添加

@synthesize submitImage = mSubmitImage;

添加一个Action Sheet

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Do you want to ..."

                                 delegate:self

                            cancelButtonTitle:@"Cancel"

                         destructiveButtonTitle:nil

                            otherButtonTitles:@"Submit to facebook", @"Save to Album", nil];

 [actionSheet showInView:self.view];

 [actionSheet release];

然后实现UIActionSheetDelegate

#pragma mark -

#pragma mark ActionSheet Delegate Methods

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {

  if (buttonIndex == [actionSheet cancelButtonIndex]) {

    //cancel

  else if (buttonIndex == [actionSheet firstOtherButtonIndex]) {

     //submit to facebook

     // pause the simulation

     RootViewController* rootViewController = (RootViewController*)[self topViewController];

     rootViewController.simulationPaused = true;

     // if color picker is open, close it

     [self dismissColorPicker];

 

    // TODO: localize this!

     FluidModel* model = [FluidModel getInstance];

     self.submitImage = [model createImage:&mRawData];

     if (!self.submitImage) {

       // open an alert with just an OK button

       UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Save image"

                                   message:@"Not enough memory to create image."

                                   delegate:self

                              cancelButtonTitle:@"OK"

                              otherButtonTitles:nil];

       [alert show];

      [alert release];

    } else {

      [self checkLogin];

    }

  } else {

    //save to album

    [self beginWaiting];

    ...

  }

}

如果submit to facebook,则先检查login

- (void)checkLogin {

  FBSession* fbSession = [FBSession session];


  if (fbSession.isConnected == NO) {

    FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:fbSession] autorelease];

    dialog.delegate = self;

    [dialog show];

    mIsCheckLogin = YES;

  } else {

    [self uploadPhoto];

  }

}

login需要实现FBDialogDelegate,由于之后有其它程序也用到该协议,因此该协议之后再实现

login后开始uploadphoto

- (void)uploadPhoto {

  [self beginWaiting];

  NSDictionary *params = nil;

  [[FBRequest requestWithDelegate:self] call:@"facebook.photos.upload"

                       params:params

                     dataParam:(NSData*)self.submitImage];

}

uploadphoto需要实现FBRequestDelegate

#pragma mark -

#pragma mark FBRequestDelegate

- (void)request:(FBRequest*)request didLoad:(id)result {

  if ([request.method isEqualToString:@"facebook.photos.upload"]) {

    [self endWaiting];

 

    free(mRawData);

    NSDictionary *photoInfo = result;

    NSString *src = [photoInfo objectForKey:@"src"];

    NSString *imageURL = [photoInfo objectForKey:@"link"];

    [self showStreamDialog:src link:imageURL];

  }

}

 

- (void)request:(FBRequest*)request didFailWithError:(NSError*)error {

  [self endWaiting];

 

  NSLog(@"%@", [NSString stringWithFormat:@"Error(%d) %@", error.code, error.localizedDescription]);

  free(mRawData);

  UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Upload failed"

                              message:@"Please try again"

                             delegate:self

                        cancelButtonTitle:@"OK"

                         otherButtonTitles:nil];

  [alert show];

  [alert release];

}

request成功后打开新的Dialog,询问用户是否要将photo展示到wall

- (void)showStreamDialog:(NSString *)src link:(NSString *)imageURL {

  FBStreamDialog* dialog = [[[FBStreamDialog alloc] init] autorelease];

  dialog.delegate = self;

  dialog.userMessagePrompt = @"show to your friends"; 

  NSString* attachment = [NSString stringWithFormat:@"{\"name\":\"Flow Painter\",\"href\":\"%@\",\"caption\":\"I make my picture in Flow Painter. Come and take a look!\",\"description\":\"Download this app, paint your own picture!\",\"media\":[{\"type\":\"image\",\"src\":\"%@\",\"href\":\"%@\"}],\"properties\":{\"an iPhone App Made by\":{\"text\":\"Coconut Island Studio\",\"href\":\"%@\"}}}", appURL, src, imageURL, homepage];

  dialog.attachment = attachment;

  // replace this with a friend's UID

  // dialog.targetId = @"999999";

  [dialog show];

}

然后实现FBDialogDelegate

#pragma mark -

#pragma mark FBDialogDelegate

- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError*)error {

  NSLog(@"%@", [NSString stringWithFormat:@"Error(%d) %@", error.code, error.localizedDescription]);

  if (mIsCheckLogin) {

    UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Connect failed"

                                message:@"Can not connect to facebook"

                                delegate:self

                           cancelButtonTitle:@"OK"

                           otherButtonTitles:nil];

    [alert show];

    [alert release];

    free(mRawData);

  } else {

    UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Upload success"

                               message:@"Photo has uploaded into your album.You may publish the photo to your wall manually"

                                delegate:self

                           cancelButtonTitle:@"OK"

                           otherButtonTitles:nil];

    [alert show];

    [alert release];

  }

}

 

- (BOOL)dialog:(FBDialog*)dialog shouldOpenURLInExternalBrowser:(NSURL*)url {

  return YES;

}

 

- (void)dialogDidSucceed:(FBDialog*)dialog {

  if (mIsCheckLogin) {

    [self uploadPhoto];

    mIsCheckLogin = NO;

  } else {

    UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Upload success"

                                message:@"Your friends will find the photo on his wall"

                               delegate:self

                           cancelButtonTitle:@"OK"

                           otherButtonTitles:nil];

    [alert show];

    [alert release];

  }

}

 

-(void)dialogDidCancel:(FBDialog*)dialog {

  if (mIsCheckLogin) {

    free(mRawData);

  } else {

    UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"Upload success"

                                 message:@"Photo has uploaded into your album.You may publish the photo to your wall manually"

                                 delegate:self

                          cancelButtonTitle:@"OK"

                          otherButtonTitles:nil];

    [alert show];

    [alert release];

  }

}

最后解决在系统等待时的图标及屏蔽按键

- (void)beginWaiting {

  // show the waiting symbol

  progressView = [[UIActivityIndicatorView alloc]

  initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

  [progressView setCenter:CGPointMake(160.0f, 240.0f)];

  [progressView startAnimating];

  [self.topViewController.view addSubview:progressView];

 

  self.view.window.userInteractionEnabled = NO;

}

(通过self.view.window可以直接取得当前application的window,这样就可以直接屏蔽整个window而不用一个个地去屏蔽按键了)

- (void)endWaiting {

  // dismiss the waiting symbol

  [progressView removeFromSuperview];

  [progressView stopAnimating];

  [progressView release];

 

  self.view.window.userInteractionEnabled = YES;

}

posted on 2010-05-16 19:31  eagley  阅读(1271)  评论(0编辑  收藏  举报