Skip to content

Instantly share code, notes, and snippets.

@melihmucuk
Last active December 24, 2015 02:29
Show Gist options
  • Select an option

  • Save melihmucuk/6730851 to your computer and use it in GitHub Desktop.

Select an option

Save melihmucuk/6730851 to your computer and use it in GitHub Desktop.
How to Use WCF Service With JSON on iOS
- (void)viewDidLoad
{
[super viewDidLoad];
// Stringlerde sorun çıkmaması için encoding yapıyoruz
NSString *param_ad = [@"Melih" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *param_soyad = [@"Mucuk" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSInteger *param_id = 1;
//Parametreleri verirken stringler için %@ , integer için ise %d kullanıyoruz
NSString *request = [NSString stringWithFormat:@"http://localhost:50419/Service1.svc/GetUserWithParams/%@/%@/?ID=%d",param_ad,param_soyad,*param_id];
NSURL *URL = [NSURL URLWithString:request];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:URL options:NSDataReadingUncached error:&error];
if(!error)
{
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
NSMutableArray *array= [json objectForKey:@"GetUserWithParamsResult"];
for(int i=0; i< array.count; i++)
{
NSDictionary *userInfo= [array objectAtIndex:i];
NSInteger *id1 = [userInfo objectForKey:@"ID"];
NSString *adi = [userInfo objectForKey:@"ADI"];
NSString *soyadi = [userInfo objectForKey:@"SOYADI"];
NSLog(@"ID: %d ADI: %@ SOYADI: %@", *id1,adi,soyadi);
}
}
}
Public Function GetUserWithParams(ByVal ID As Integer, ByVal AD As String, ByVal SOYAD As String) As List(Of IService1.Users) Implements IService1.GetUserWithParams
Dim users As New List(Of IService1.Users)
Dim user As New IService1.Users
user.ID = ID
user.ADI = AD
user.SOYADI = SOYAD
users.Add(user)
Return users
End Function
Imports System.ServiceModel
<ServiceContract()>
Public Interface IService1
<OperationContract()>
<WebGet(UriTemplate:="GetUser", RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped)> _
Function GetUser() As List(Of Users)
<OperationContract()>
<WebGet(UriTemplate:="GetUserWithParams/{AD}/{SOYAD}/?ID={ID}", RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped)> _
Function GetUserWithParams(ByVal ID As Integer, ByVal AD As String, ByVal SOYAD As String) As List(Of Users)
<DataContract()>
Class Users
<DataMember>
Public Property ID As Integer
<DataMember>
Public Property ADI As String
<DataMember>
Public Property SOYADI As String
End Class
End Interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment