Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
iPhoneでWebViewのCookieを共有する方法
#import "ASIHTTPRequest.h"
#import "ViewController.h"
@interface ViewController () {
}
- (IBAction)request:(id)sender;
@property(weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:@"http://lab.hisasann.com/cookie/setcookie.php"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[self setWebView:nil];
[super viewDidUnload];
}
- (IBAction)request:(id)sender {
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
NSLog(@"%@", cookie);
}
NSURL *httpUrl = [NSURL URLWithString:@"http://lab.hisasann.com/cookie/getcookie.php"];
__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:httpUrl];
[request setValidatesSecureCertificate:NO];
[request setPersistentConnectionTimeoutSeconds:10];
[request setCompletionBlock:^{
// Use when fetching text data
NSArray *cookies = [request responseCookies];
NSLog(@"responseCookie - %@", cookies.description);
NSLog(@"responseString - %@", [request responseString]);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"error - %@", [error localizedDescription]);
}];
[request startAsynchronous];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment