Skip to content

Instantly share code, notes, and snippets.

@hisasann
Created November 8, 2012 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hisasann/4037787 to your computer and use it in GitHub Desktop.
Save hisasann/4037787 to your computer and use it in GitHub Desktop.
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