質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Q&A

解決済

1回答

2399閲覧

Objective-CからのJavascript実行

algo

総合スコア13

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

0グッド

0クリップ

投稿2015/06/25 06:16

Objective-Cでブラウザアプリを作成しています。
スワイプでJavascriptを実行したいのですが、
(null)となってしまい取得できません。

以下のコードを記述しています。

lang

1// スワイプされた際に呼び出される処理。 2- (void)view_SwipeLeft:(UISwipeGestureRecognizer *)sender { 3 NSString* title =[self.webview stringByEvaluatingJavaScriptFromString:@"document.URL"]; 4 NSLog(@"swiped to Left"); 5 NSLog(@"%@",title); 6}

"swiped to Left"はログに出るので、スワイプの認識はしているようです。
UIViewの作成は以下のようにしています。
どのようにすればよいでしょうか。

lang

1#import "ViewController.h" 2#import <QuartzCore/QuartzCore.h> 3 4@interface ViewController () 5 6@end 7 8@implementation ViewController 9 10@synthesize indicatorBackView; 11@synthesize indicator; 12 13@synthesize webview; 14 15 16- (void)viewDidLoad 17{ 18 [super viewDidLoad]; 19 20 21 [self setfirstpage]; 22} 23 24-(void)setfirstpage{ 25 26 // UIWebViewのインスタンス初期化 27 UIWebView *webView = [[UIWebView alloc]init]; 28 29 // デリゲート 30 webView.delegate = self; 31 32 33 // Webページの大きさを画面に合わせる 34 CGRect rect = self.view.frame; 35 webView.frame = rect; 36 webView.scalesPageToFit = YES; 37 38 // インスタンスをビューに追加する 39 [self.view addSubview:webView]; 40 41 42 //iOSバーションの取得 43 float iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; 44 45 //iOS 7.0以上の場合 46 if(iOSVersion >= 7.0) { 47 //iOS7のステータスバー対策としてWebViewの高さを調整。 48 //ステータスバーの高さを取得 49 float statusBarH = MIN([UIApplication sharedApplication].statusBarFrame.size.height, [UIApplication sharedApplication].statusBarFrame.size.width); 50 51 //ステータスバーの高さ分、WebViewのY座標をずらす 52 CGPoint vPoint = (CGPoint) { CGRectGetMinX(webView.frame), CGRectGetMinY(webView.frame) + statusBarH }; 53 54 //ステータスバーの高さ分、WebViewのHeightを引く 55 CGSize vSize = (CGSize) { CGRectGetWidth(webView.frame), CGRectGetHeight(webView.frame) - statusBarH }; 56 57 //WebViewへ設定 58 webView.frame = (CGRect) { vPoint, vSize }; 59 } 60 61 // URLを指定 62 NSURL *url = [NSURL URLWithString:@"https://example.com/; 63 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 64 65 // リクエストを投げる 66 [webView loadRequest:request]; 67 68 for (id subView in webView.subviews) { 69 if ([[subView class] isSubclassOfClass:[UIScrollView class]]) { 70 ((UIScrollView *)subView).bounces = NO; // バウンス禁止 71 } 72 } 73 74 //インジケータ背景を用意する 75 indicatorBackView = [[UIView alloc] initWithFrame:CGRectMake((self.view.bounds.size.width/2)-50, (self.view.bounds.size.height/2)-60, 100, 100)]; 76 indicatorBackView.backgroundColor = [UIColor grayColor]; 77 indicatorBackView.alpha = 0.5; 78 [[indicatorBackView layer] setCornerRadius:5.0]; 79 [self.view addSubview:indicatorBackView]; 80 81 //インジケーターを用意する 82 indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 83 indicator.frame = CGRectMake(indicatorBackView.bounds.size.width/2-20,indicatorBackView.bounds.size.height/2-20,40,40); 84 [indicatorBackView addSubview:indicator]; 85 86 /* 右スワイプ */ 87 UISwipeGestureRecognizer* swipeRightGesture = 88 [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(view_SwipeRight:)]; 89 90 // 右スワイプを認識するように設定 91 swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight; 92 93 // ビューにジェスチャーを追加 94 [self.view addGestureRecognizer:swipeRightGesture]; 95 96 /* 左スワイプ */ 97 UISwipeGestureRecognizer* swipeLeftGesture = 98 [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(view_SwipeLeft:)]; 99 100 // 右スワイプを認識するように設定 101 swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft; 102 103 // ビューにジェスチャーを追加 104 [self.view addGestureRecognizer:swipeLeftGesture]; 105 106 107}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

lang

1// UIWebViewのインスタンス初期化 2 UIWebView *webView = [[UIWebView alloc]init];

これを

lang

1// UIWebViewのインスタンス初期化 2 webview = [[UIWebView alloc]init];

こうしなければいけませんでした。初歩的なミスです

投稿2015/06/25 07:07

algo

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問