3

アプリ (iOS6) に Facebook 共有を実装しました。コードは次のとおりです。

//完了ハンドラ

SLComposeViewControllerCompletionHandler __block completionHandler = ^(SLComposeViewControllerResult result) {
    UIAlertView *alert = nil;
    switch(result) {
        case SLComposeViewControllerResultCancelled: {
            alert = [UIAlertView alloc]initWithTitle:@"Cancelled" message:@"Your message wasn't shared" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        break;
        case SLComposeViewControllerResultDone: {
            alert = [UIAlertView alloc]initWithTitle:@"Posted" message:@"Your message was posted successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        break;
    }
}

// Facebook への投稿

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController *fbVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    fbVC.completionHandler = completionHandler;
    [self presentViewController:fbVC animated:YES completion:nil];
}

次の状況をテストしています。

  1. インターネットが利用可能で、ユーザーがテキストを入力し、投稿を押す
  2. インターネットが利用可能で、ユーザーがテキストを入力してキャンセルを押した
  3. インターネットが利用できず、ユーザーがテキストを入力して投稿を押した。

最初の 2 つは正常に動作します。3番目の状況では、予想どおり、アラートが表示されます

"Cannot Post to Facebook" - The post cannot be sent because connection to Facebook failed.

しかし、表示されたアラート ビューで [再試行] または [キャンセル] ボタンを押すと、「投稿済み」アラートが表示されます (完了ハンドラー タイプ SLComposeViewControllerResultDone が実行されます)。

これを防ぐ方法は?

4

2 に答える 2

1

URl を It(SLComposeViewController) に追加する場合は、http://www.stackoverflow.com 形式である必要があることを確認してください。そうしないと、「Facebook に投稿できません」と表示され続けます - Facebook への接続が失敗したため、投稿を送信できません。

HTH

于 2014-04-04T07:32:12.443 に答える
1

編集:まあ、3 番目の状況を修正するのは簡単でした。Apple が提供する到達可能性クラスを追加しました (ここからダウンロードできます)。必要なコードは次のとおりです。

#import "Reachability.h"

- (BOOL)internetConnected {
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable || reachability.connectionRequired); //required for iOS 7 and above
}

... 
...

case SLComposeViewControllerResultDone: {
    if(self.internetConnected) {
        alert = [UIAlertView alloc]initWithTitle:@"Posted" message:@"Your message was posted successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    } else {
        alert = [UIAlertView alloc]initWithTitle:@"Failed" message:@"Your message was not posted, no internet was available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    }
break;
于 2013-05-07T05:35:08.153 に答える