我的iphone应用程序非常简单,一个视图处理所有内容,在viewDidLoad中我检查是否有互联网连接,如果我们从网络加载,如果不是,我们从本地资源加载.这很好用.
//in viewDidOnload
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNetworkChange:)
name:kReachabilityChangedNotification object:nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if (status == NotReachable) {
//Do something offline
} else {
//Do sometihng on line
}
- (void)handleNetworkChange:(NSNotification *)notice{
NetworkStatus status = [reachability currentReachabilityStatus];
if (status == NotReachable) {
//Change to offline Message
} else {
//Relaunch online application
}
}
Run Code Online (Sandbox Code Playgroud)
为了测试我的handleNetworkChange事件,我关掉了所有的蜂窝数据,但是打开了wifi.在无线网络的范围内我启动了应用程序,一切都很完美.然后我走出wifi的范围之外,但我的handleNetworkChange从未触发(使用uiAlertView测试).站在wifi的范围之外,我的应用程序启动离线消息就好了.
我怀疑这是ViewController生命周期的问题,这个代码应该放在AppDelegate函数中吗?可能这是一个更好的设计开始.
我正在使用谷歌翻译API,我可能会发送相当多的文本进行翻译.在此场景中,Google建议您执行以下操作:
如果要在单个请求中发送更多数据,也可以使用POST来调用API.POST正文中的q参数必须小于5K字符.要使用POST,必须使用X-HTTP-Method-Override标头告诉Translate API将请求视为GET(使用X-HTTP-Method-Override:GET).Google Translate API文档
我知道如何使用CURL发出正常的POST请求:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Run Code Online (Sandbox Code Playgroud)
但是如何修改标头以使用X-HTTP-Method-Override?