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}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。