是否可以从Appdelegate方法中为目标C中的“ POST”调用Web服务(如JSON解析)?

Rohit_RK

是否可以从AppDelegate.m类为POST调用Web服务(如JSON解析)?我想在应用程序启动时使用Web Service将数据发布到服务器。

#pragma mark JSON Delegates

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [responseData  setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [responseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    responseData = nil;

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSDictionary *DictData = (NSDictionary*)[responseString JSONValue];




    }
安布·卡尔提克(Anbu.Karthik)

在这里,我附加了示例方法,将其添加到您的AppDelegate.h中

@interface AppDelegate : UIResponder   <UIApplicationDelegate>
{

NSURLConnection *clearSession;
NSMutableData * responseData;

}

在您的AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 [self clearsession];  // this is your method Name
return YES;
}


-(void)clearsession

{
// call the clear session
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"yourURLName.php"]];
// get the session ID and UserID from Sqlite

NSString *requestString = [NSString stringWithFormat:@"user_id=%@",passyourString,nil];
NSMutableData *requestData =[NSMutableData dataWithBytes:[requestString UTF8String] length: [requestString length]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
clearSession = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[clearSession start];

}

#pragma mark - NSUrlConnectionDelegate Methods

-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response
{
if (responseData == NULL)
{
    responseData = [[NSMutableData alloc] init];
}

[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
 UIAlertView *customAlert = [[UIAlertView alloc]initWithTitle:@"No NetWork" message:@"Interet Connection is Lost" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[customAlert show];


}




- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

   NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData: responseData options: kNilOptions error:nil];
    NSString *str=[jsonDict objectForKey:@"result"];
    if ([str isEqualToString:@"success"])

    {
        // Add your Home Viewcontroller 

   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
   UIViewController *viewController  = [storyboard instantiateViewControllerWithIdentifier:@"yourIDentifierName"];
   self.window.rootViewController=viewController;
    }
 else

  {
      // do ur stuff here 
   }


  }

选择二号

使用此链接,这是protocol方法

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

是否可以调用Java中的最终目标?

目标C中的“调用方法”或“发送消息”

是否可以合并Java枚举(如C#中的Bitwise)?

在Java中检查字符串是否可以解析为Double的最快方法

是否可以在HTTP中缓存POST方法?

C#编译器或JIT是否可以优化Lambda表达式中的方法调用?

是否可以在POST Json中附加文件?

在JSP中调用Web服务

是否可以从C ++中的汇编中调用内置函数

如何在appdelegate中调用viewController的方法

是否可以从C中的数组调用函数?

是否可以修改函数参数?(如&在C ++中)

解析来自PHP中的HTTP Web服务(JSON)的响应

从AppDelegate调用的方法中应有预期的“]”

目标c中的嵌套方法调用

在目标C中解析json

将JSON Web服务中的UTF-16数据解析为PHP中的字符串

在目标C中解析流式JSON

Json在Struts 2.0中解析Web服务?

目标C中的JSON对象

是否可以在web.xml中为contextloaderlistner和mvc服务提供相同的xml文件

解析Web服务中的json字符串

Son中的重写方法是否可以调用C ++中父亲模板类的重写方法?

如何使用目标 c 中的本地通知每天在特定时间调用 Web 服务?

使用asp.net mvc 中的web api 从服务Angular 6 调用REST 的POST 方法

是否可以从同名方法中调用先前定义的方法?

是否可以在@Repository 中调用服务?

C# 在方法中调用方法时是否可以将 EventArgs 转换为 FormClosingEventArgs?

是否可以根据实际类中的局部变量为方法调用返回模拟对象?