2017-01-25 8 views
0

gomobileのバインドを使用して、iOSにエクスポートするときに何らかの種類のデリゲート動作を実装できるかどうかは誰でも知っていますか?Gomobileのバインドの代理/コールバックの動作

つまり、iOSアプリのネットワークリクエストを処理するgoライブラリがあり、アプリをハングしないように非同期で行う必要があります。

解決策は、objcコードを送信する方法が見つからなかったため、objc補完ブロックを送信するか、デリゲートの何らかの並べ替えを実装することですリクエストがいつ終了したかを知ることができます。私は考えることができるすべてを試しました...どんなアイデアですか?ありがとう!

答えて

0

それが可能になります!ここで

はゴーコード:

Objetive-Cに従うとして実装されて
type NetworkingClient struct {} 

func CreateNetworkingClient() *NetworkingClient { 
    return &NetworkingClient {} 
} 

type Callback interface { 
    SendResult(json string) 
} 

func (client NetworkingClient) RequestJson (countryCode string, callback Callback) { 
    go func() { 
    safeCountryCode := url.QueryEscape(countryCode) 
    url := fmt.Sprintf("someApi/%s", safeCountryCode) 

    req, err := http.NewRequest("GET", url, nil) 
    if err != nil { 
     //Handle error 
    } 

    httpClient := &http.Client{} 

    resp, err := httpClient.Do(req) 
    if err != nil { 
     //Handle error 
    } 

    defer resp.Body.Close() 
     b, err := ioutil.ReadAll(resp.Body) 
     callback.SendResult(string(b)) 
     }() 
    } 

- (void)start { 

    ... 

    EndpointNetworkingClient* client = EndpointCreateNetworkingClient(); 
    [client requestJson:countryCode callback:self]; 
} 

//Receives the json string from Go. 
- (void)sendResult:(NSString*)json{ 
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; 
    id jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
    [self handleResponse:jsonDictionary]; 
} 
関連する問題