Created
November 8, 2012 09:38
-
-
Save hisasann/4037787 to your computer and use it in GitHub Desktop.
iPhoneでWebViewのCookieを共有する方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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