第7章 $fh.init

クライアントアプリが対応するクラウドアプリと通信するためには、クライアント SDK はクラウドを呼び出す前に初期化される必要があります。

初期化プロセスでは、サーバーを呼び出してプラットフォーム上のアプリが有効かどうかを検証し、アプリのプロパティーが取得されます。このプロセスが完了するまでは、他の API メソッドを実行することはできません。初期化プロセスは非同期で実行されるので、メインのアプリスレッドがブロックされることはありません。

サポートされるプラットフォーム

  • JavaScript SDK

    • Cordova
    • Web Apps
  • Android SDK
  • iOS Objective-C SDK
  • iOS Swift SDK
  • .NET SDK

    • Windows
    • Xamarin

詳細なバージョン情報については、Supported Configurations (英語) を参照してください。

7.1. 例

JavaScript

アプリが読み込まれると Javascript SDK が初期化されるので、init API を呼び出す必要がありません。このため、たとえばアプリが $fh.cloud を読み込むと、すぐに使用することができます。アプリがいつ初期化を完了したかを確認するには、コールバックを "fhinit" に登録します。

$fh.on("fhinit", function(err, host){
  if(err){
    //Init has failed due to some error. Normally this is due to no network connection.
  } else {
    //The js sdk has initialized. The host value will be the URL of the cloud app the client app will communicate with.
  }
});

Android (Java)

FH.init(this, new FHActCallback() {
  public void success(FHResponse pRes) {
    // Init okay, free to use FHActRequest
    // Note: pRes will be null. To get the cloud host url, use FH.getCloudHost.
    String cloudAppUrl = FH.getCloudHost();
  }

  public void fail(FHResponse pRes) {
    // Init failed
    Log.e("FHInit", pRes.getErrorMessage(), pRes.getError());
  }
});

iOS (Objective-C)

#import <FH/FH.h>
#import <FH/FHResponse.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.

  // Call a cloud side function when init finishes
  void (^success)(FHResponse *)=^(FHResponse * res) {
    // Initialisation is now complete, you can now make FHCloud requests
    // Note: res will be nil. To get the cloud host url, use [FH getCloudHost].
    NSString * cloudAppUrl = [FH getCloudHost];
    NSLog(@"SDK initialised OK");
  };

  void (^failure)(id)=^(FHResponse * res){
    NSLog(@"Initialisation failed. Response = %@, res.rawResponse);
  };

  //View loaded, init the library
  [FH initWithSuccess:success AndFailure:failure];

  return YES;
}

iOS (Swift)

import FeedHenry

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  // Override point for customization after application launch.
  FH.init { (resp:Response, error: NSError?) -> Void in
    if let error = error {
      print("Initialisation failed. Response = \(error)")
      return
    }
    // Initialisation is now complete, you can now make FH.cloud calls.
    print("SDK initialised OK: \(resp.parsedResponse)")
  }
  return true
}

 .NET (C#)

try
{
  bool inited = await FHClient.Init();
  if(inited) {
    //Initialisation is successful
    //To get the cloud host url, use FH.GetCloudHost().
    string cloudAppUrl = FH.GetCloudHost();
  }
}
catch(FHException e)
{
  //Initialisation failed, handle exception
}