1. kz56cd

    Posted

    kz56cd
Changes in title
+UIWebViewで表示元のボタン等を制御する
Changes in tags
Changes in body
Source | HTML | Preview
@@ -0,0 +1,72 @@
+
+## 経緯
+
+該当画面はUIWebViewで表示した際、「閉じる」ボタンが効かなかった。
+ボタン押下時は閉じるのではなく、一画面戻したかった為、方法を探った。
+
+
+## 概要
+
+
+
+## 方法
+
+jsを書き換える為のファイルを用意、プロジェクト内に格納:
+
+```
+window.close = function () {
+ history.back();
+};
+```
+
+
+
+`webViewDidFinishLoad`でjsを書き換える為のファイルを読み込む:
+
+```
+// URL読込後
+- (void)webViewDidFinishLoad:(UIWebView *)webView {
+ NSError *error = nil;
+ NSString *jsFromFile = [NSString stringWithContentsOfURL:[[NSBundle mainBundle]
+ URLForResource:@"JS_WebViewController" withExtension:@"txt"]
+ encoding:NSUTF8StringEncoding error:&error];
+ __unused NSString *jsOverrides = [webView
+ stringByEvaluatingJavaScriptFromString:jsFromFile];
+}
+```
+
+
+## 他
+
+```
+window.close = function () {
+ open("native://close");
+};
+```
+
+
+```
+- (void)webViewDidFinishLoad:(UIWebView *)webView {
+
+ NSError *error = nil;
+ NSString *jsFromFile = [NSString stringWithContentsOfURL:[[NSBundle mainBundle]
+ URLForResource:@"JS_SettingWebViewController" withExtension:@"txt"]
+ encoding:NSUTF8StringEncoding error:&error];
+ __unused NSString *jsOverrides = [webView stringByEvaluatingJavaScriptFromString:jsFromFile];
+
+
+}
+
+-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
+{
+ NSString *scheme = [request.URL.scheme stringByAppendingString:@":"];
+ scheme = [scheme stringByAppendingString:request.URL.resourceSpecifier];
+ if ([scheme isEqualToString:@"native://close"]) {
+ [self dismissViewControllerAnimated:YES completion:nil];
+ return NO;
+ }
+ else {
+ return YES;
+ }
+}
+```