在 iOS 9.0 中使用服务
Consuming services in iOS 9.0
我在 iOS.
中实现了以下消费服务的代码
NSString *urlStr=[NSString stringWithFormat:@“SomeURL”];
NSString *jsonStr=[NSString stringWithFormat:@“{\”SomeParameter\”:\”%@\”}”];
NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlStr]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSUTF8StringEncoding];
SBJSON *parser = [[SBJSON alloc] init];
NSArray *arryTerritory = (NSArray *) [parser objectWithString:theReply error:nil];
NSLog(@“%@”,[arryTerritory description]);
这是我的代码,当我 运行 xCode 6.4 和 iOS 9.0 的代码能够接收 NSData *postData
和 NSString *postLength
中的数据时.
但是在使用iOS 9.0 编译代码时,我无法获取数据。
输出为 nil
。
使用 xCode 7 Beta 中的服务是否有任何不同的过程。需要与这些版本兼容的 ipa。
这里我应该在info.plist需要安全通信的地方加减速。这是我添加到 info.plist 的代码。我认为它是 ios 9 和 xCode 7 bete 版本中的新代码。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我在 iOS.
中实现了以下消费服务的代码NSString *urlStr=[NSString stringWithFormat:@“SomeURL”];
NSString *jsonStr=[NSString stringWithFormat:@“{\”SomeParameter\”:\”%@\”}”];
NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlStr]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSUTF8StringEncoding];
SBJSON *parser = [[SBJSON alloc] init];
NSArray *arryTerritory = (NSArray *) [parser objectWithString:theReply error:nil];
NSLog(@“%@”,[arryTerritory description]);
这是我的代码,当我 运行 xCode 6.4 和 iOS 9.0 的代码能够接收 NSData *postData
和 NSString *postLength
中的数据时.
但是在使用iOS 9.0 编译代码时,我无法获取数据。
输出为 nil
。
使用 xCode 7 Beta 中的服务是否有任何不同的过程。需要与这些版本兼容的 ipa。
这里我应该在info.plist需要安全通信的地方加减速。这是我添加到 info.plist 的代码。我认为它是 ios 9 和 xCode 7 bete 版本中的新代码。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>