サーバーベースのアプリを作成しています。データベースにリクエストを送信するたびに、すべてのサーバー接続コードを入力する必要があります。これをどうにかして再利用することは可能ですか?PHP では、通常、接続するたびに呼び出すことができる dbConnect.php (または同様のもの) を呼び出すファイルがあります。
例、私はいつも使用しているこれを置き換えたいと思います:
- (void)doSomething
{
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url];
__weak ASIHTTPRequest *request_b = request;
[request setDelegate: self];
[request addRequestHeader:@"Content-Type" value:@"text/html; charset=utf-8;"];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setTimeOutSeconds: 10.0f];
[request setCachePolicy: ASIDoNotWriteToCacheCachePolicy | ASIDoNotReadFromCacheCachePolicy];
//Set the variables here
[request startAsynchronous];
}
...次のようなもので:
- (void)doSomething
{
LoadServerCode; //This loads all the server code as above
//Set variables
[request startAsynchronous];
}
前もって感謝します
編集:
少し明確にするために。UILabel や特別な方法で UIView を作成するなど、私がよく使用するいくつかのメソッドがあるとします。 MyConstructionMethods か何か...つまり、アプリ内のいくつかの異なる場所にラベルを作成したい場合は、次のように入力するだけです。
MyGreenLabel; //Done, the label is created and added to the view
... それ以外の:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
label.backgroundColor = [UIColor greenColor];
[self.view addSubview: label];