UIWebView 役に立ちそうなメソッド集


## タイトルの取得

```objectivec:
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
```
http://stackoverflow.com/questions/2275876/how-to-get-the-title-of-a-html-page-displayed-in-uiwebview

## ソースの取得

```objectivec:
NSString *source = [self stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];

/* Body内のみ */
// NSString *source = [self stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
```
http://stackoverflow.com/questions/817218/get-entire-document-html-as-string

## 表示をクリア

```objectivec:
[webView loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];
```
http://stackoverflow.com/questions/2933315/clear-uiwebview-content-upon-dismissal-of-modal-view-iphone-os-3-0

## Cookieの削除

```objectivec:
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie  *cookie in [storage cookies]) {
    [storage deleteCookie:cookie];
}
```
http://stackoverflow.com/questions/1852515/how-to-clear-cookies-from-nshttpcookiestorage-more-then-once
## Cacheの削除

```objectivec:
NSURLCache *cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
```
http://stackoverflow.com/questions/5468553/clearing-uiwebview-cache
## XMLのエスケープ

```objectivec:NSMutableString+XMLEscape.m
- (NSMutableString *)xmlSimpleUnescape
{
    [self replaceOccurrencesOfString:@"&amp;"  withString:@"&"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"&quot;" withString:@"\"" options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"&#x27;" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"&#x39;" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"&#x92;" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"&#x96;" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"&gt;"   withString:@">"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"&lt;"   withString:@"<"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    
    return self;
}

- (NSMutableString *)xmlSimpleEscape
{
    [self replaceOccurrencesOfString:@"&"  withString:@"&amp;"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"\"" withString:@"&quot;" options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"'"  withString:@"&#x27;" options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@">"  withString:@"&gt;"   options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    [self replaceOccurrencesOfString:@"<"  withString:@"&lt;"   options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    
    return self;
}
```
http://stackoverflow.com/questions/5468553/clearing-uiwebview-cache

## URLのエスケープ
```objectivec:NSString+URLEscape.m
- (NSString*)escapedUrlString
{
    return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                               NULL,
                                                               (CFStringRef)self,
                                                               NULL,
                                                               (CFStringRef)@"!*'();:@&=+$,/?%#[]._-",
                                                               kCFStringEncodingUTF8 ));
}

- (NSString*)decodedUrlString
{
    return (NSString *) CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(
                                                                                                  NULL,
                                                                                                  (CFStringRef)self,
                                                                                                  CFSTR(""),
                                                                                                  kCFStringEncodingUTF8));
}
```
http://blog.daisukeyamashita.com/post/1686.html