Skip to content

Instantly share code, notes, and snippets.

@cnsoft
Created July 2, 2013 17:26
Show Gist options
  • Select an option

  • Save cnsoft/5911269 to your computer and use it in GitHub Desktop.

Select an option

Save cnsoft/5911269 to your computer and use it in GitHub Desktop.
UIWebView NSURLConnection NSURLResponse 用法.
//
http://ioscreator.com/loading-a-website-with-uiwebview/
Now modify the ViewDidLoad method in ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//1
NSString *urlString = @"http://ioscreator.com";
//2
NSURL *url = [NSURL URLWithString:urlString];
//3
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//4
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//5
[NSURLConnection sendAsynchronousRequest:request queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] > 0 && error == nil) [myWebView loadRequest:request];
else if (error != nil) NSLog(@"Error: %@", error);
}];
}
http://blog.csdn.net/totogo2010/article/details/7686164
Delphi FMX Webrowser 如何使用 NSURLConnection
- (void)viewDidLoad
{
[super viewDidLoad];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
[self.view addSubview: webView];
[webView loadRequest:request];
}
@cnsoft

cnsoft commented Jul 2, 2013

Copy link
Copy Markdown
Author

@cnsoft

cnsoft commented Jul 2, 2013

Copy link
Copy Markdown
Author

Edit: adapting for edited question

NSHTTPCookieStorage has a -setCookies:forURL:mainDocumentURL: method, so the easy thing to do is use NSURLConnection and implement -connection:didReceiveResponse:, extracting cookies and stuffing them into the cookie jar:

  • ( void )connection: (NSURLConnection *)connection
    didReceiveResponse: (NSURLResponse *)response
    {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSArray *cookies;

    cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields:
    [ httpResponse allHeaderFields ]];
    [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
    setCookies: cookies forURL: self.url mainDocumentURL: nil ];
    }
    (You can also simply extract an NSDictionary object from the NSHTTPCookie with properties, and then write the dictionary to the disk. Reading it back in is as easy as using NSDictionary's -dictionaryWithContentsOfFile: and then creating the cookie with -initWithProperties:.)

Then you can pull the cookie back out of the storage when you need it:

  • ( void )reloadWebview: (id)sender
    {
    NSArray *cookies;
    NSDictionary *cookieHeaders;
    NSMutableURLRequest *request;

    cookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
    cookiesForURL: self.url ];
    if ( !cookies ) {
    /* kick off new NSURLConnection to retrieve new auth cookie */
    return;
    }

    cookieHeaders = [ NSHTTPCookie requestHeaderFieldsWithCookies: cookies ];
    request = [[ NSMutableURLRequest alloc ] initWithURL: self.url ];
    [ request setValue: [ cookieHeaders objectForKey: @"Cookie" ]
    forHTTPHeaderField: @"Cookie" ];

    [ self.webView loadRequest: request ];
    [ request release ];
    }
    share|improve this answer
    edited May 10 '11 at 19:32

answered May 10 '11 at 19:01

more tension
2,327615
There is no need to manually manage NSHTTPCookieStorage this way. Any time the URL loading system is using HTTP and the cookie accept policy is set to accept cookies, NSHTTPCookieStorage is managed automatically - storing cookies if set in response headers, and setting headers for stored cookies when sending requests. – kball May 10 '11 at 19:52
Sure. Of course, that approach means the developer is ignorant of what cookies are saved and sent with each request, which can be undesirable if, say, the site is setting unwanted tracking cookies. Using NSHTTPCookieStorage at all can be a heavyweight solution, too, though typically only on Mac OS X. For example, see: cocoabuilder.com/archive/cocoa/… – more tension May 10 '11 at 20:14
1
Appreciate the long answer, though it seems to be a considerable amount of work to set a single cookie. We ended up doing this: lists.apple.com/archives/Webkitsdk-dev/2003/Sep/msg00003.html – Mauvis Ledford May 10 '11 at 20:34

@cnsoft

cnsoft commented Jul 2, 2013

Copy link
Copy Markdown
Author

@cnsoft

cnsoft commented Jul 2, 2013

Copy link
Copy Markdown
Author

http://blog.csdn.net/wangyx810328/article/details/8752295

在UIWebView中设置cookie
分类: IOS 2013-04-02 16:58 54人阅读 评论(0) 收藏 举报
项目中,需要在打开3g网页时,通过cookie传递一些信息。

实现代码如下:

[plain] view plaincopy
NSURL *url = [NSURL URLWithString:_urlstr];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
[self.myWeb loadRequest:request];
之前,设置或者删除cookie。

[plain] view plaincopy
//////////////////////////////////////////////////////
//设置cookie

  • (void)setCookie{

    NSMutableDictionary *cookiePropertiesUser = [NSMutableDictionary dictionary];
    [cookiePropertiesUser setObject:@"cookie_user" forKey:NSHTTPCookieName];
    [cookiePropertiesUser setObject:uid forKey:NSHTTPCookieValue];
    [cookiePropertiesUser setObject:@"xxx.xxx.com" forKey:NSHTTPCookieDomain];
    [cookiePropertiesUser setObject:@"/" forKey:NSHTTPCookiePath];
    [cookiePropertiesUser setObject:@"0" forKey:NSHTTPCookieVersion];

    // set expiration to one month from now or any NSDate of your choosing
    // this makes the cookie sessionless and it will persist across web sessions and app launches
    /// if you want the cookie to be destroyed when your app exits, don't set this
    [cookiePropertiesUser setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

    NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookiePropertiesUser];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieuser];
    }

//清除cookie

  • (void)deleteCookie{
    NSHTTPCookie *cookie;

    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    NSArray *cookieAry = [cookieJar cookiesForURL: [NSURL URLWithString: _urlstr]];

    for (cookie in cookieAry) {

    [cookieJar deleteCookie: cookie];  
    

    }
    }

@cnsoft

cnsoft commented Jul 2, 2013

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment