1

Objective C での同様の質問に対する回答は次のとおりですが、MonoTouch に変換する正しい方法がわかりません。

基本的に、私は JavaScript エラーをキャッチして、少なくともファイル名と行番号を知りたいのですが、残念ながら、window.onerrorこの重要な情報は得られません。

特に、ネイティブ ライブラリを公開する必要があるかどうか、またはこれを純粋な MonoTouch で記述できるかどうかはわかりません。

4

1 に答える 1

4

Robert Sanders と Kresimir Prcela の回答に触発されたPablo のUIWebView+Debugカテゴリを採用しました。

彼のコードには、リモート Web インスペクターを有効にするために使用できるプライベート API 呼び出しも含まれていることに注意してください。
(何らかの理由で、これは私にはうまくいきません。)

更新: Mountain Lion で UIWebView をデバッグする方法は次のとおり です。古いバージョンの Chromium をダウンロードする必要があります。

デバッグ中はプライベート API のみを使用することを忘れないでください。アプリを送信してこれらの呼び出しを削除するのを忘れると、Apple はアプリを拒否します。このため、Xcode と MonoDevelop コードの両方でDEBUG条件が使用されます。

私が使用している完全なソースコードは次のとおりです。

Xcode プロジェクト

UIWebView+Debug.h

//
//  WebView+Debug.h
//  VOL
//
//  Created by Pablo Guillen Schlippe on 26.07.11.
//  Copyright 2011 Medienhaus.
//

/*

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 associated documentation files (the "Software"), to deal in the Software without restriction, 
 including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all copies or 
 substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

 */

// This little drop in category is based on the following StackOverflow article:
// https://stackoverflow.com/questions/193119/


#ifdef DEBUG

// Use this to toggle logging
#define kDidParseSource         0
#define kFailedToParseSource    1
#define kExceptionWasRaised     1
#define kDidEnterCallFrame      0
#define kWillExecuteStatement   0
#define kWillLeaveCallFrame     0

void enableRemoteWebInspector(void);

#endif

UIWebView+Debug.m

//
//  WebView+Debug.m
//  VOL
//
//  Created by Pablo Guillen Schlippe on 26.07.11.
//  Copyright 2011 Medienhaus. All rights reserved.
//
    
#ifdef DEBUG

#import <objc/runtime.h>
#import "UIWebView+Debug.h"

@class WebView;
@class WebFrame;
@class WebScriptCallFrame;

#pragma mark -

static NSString* getAddress() {
    id myhost =[NSClassFromString(@"NSHost") performSelector:@selector(currentHost)];
    
    if (myhost) {
        for (NSString* address in [myhost performSelector:@selector(addresses)]) {
            if ([address rangeOfString:@"::"].location == NSNotFound) {
                return address;
            }
        }
    }
    
    return @"127.0.0.1";
}

void enableRemoteWebInspector() {
    [NSClassFromString(@"WebView") performSelector:@selector(_enableRemoteInspector)];
    NSLog(@"Point your browser at http://%@:9999", getAddress());
}

#pragma mark -

@interface ScriptDebuggerDelegate : NSObject

-(id)functionNameForFrame:(WebScriptCallFrame*)frame;
-(id)callerForFrame:(WebScriptCallFrame*)frame;
-(id)exceptionForFrame:(WebScriptCallFrame*)frame;

@end

#pragma mark -

@implementation ScriptDebuggerDelegate

// We only have access to the public methods declared in the header / class
// The private methods can also be accessed but raise a warning.
// Use runtime selectors to suppress warnings

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

-(id)functionNameForFrame:(WebScriptCallFrame*)frame {
    SEL functionNameSelector = @selector(functionName);
    return [(id)frame performSelector:functionNameSelector];
}

-(id)callerForFrame:(WebScriptCallFrame*)frame {
    SEL callerSelector = @selector(caller);
    return [(id)frame performSelector:callerSelector];
}

-(id)exceptionForFrame:(WebScriptCallFrame*)frame {
    SEL exceptionSelector = @selector(exception);
    return [(id)frame performSelector:exceptionSelector];
}

#pragma clang diagnostic pop

- (void)webView:(WebView *)webView      didParseSource:(NSString *)source
 baseLineNumber:(unsigned)lineNumber
        fromURL:(NSURL *)url
       sourceId:(int)sid
    forWebFrame:(WebFrame *)webFrame {
    if (kDidParseSource)
        NSLog(@"ScriptDebugger called didParseSource: \nsourceId=%d, \nurl=%@", sid, url);
}

// some source failed to parse
- (void)webView:(WebView *)webView failedToParseSource:(NSString *)source
 baseLineNumber:(unsigned)lineNumber
        fromURL:(NSURL *)url
      withError:(NSError *)error
    forWebFrame:(WebFrame *)webFrame {
    if (kFailedToParseSource)
        NSLog(@"ScriptDebugger called failedToParseSource:\
              \nurl=%@ \nline=%d \nerror=%@ \nsource=%@",
              url, lineNumber, error, source);
}

- (void)webView:(WebView *)webView  exceptionWasRaised:(WebScriptCallFrame *)frame
       sourceId:(int)sid
           line:(int)lineno
    forWebFrame:(WebFrame *)webFrame {
    if (kExceptionWasRaised)
        NSLog(@"ScriptDebugger exception:\
              \nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
              sid,
              lineno,
              [self functionNameForFrame:frame],
              [self callerForFrame:frame],
              [self exceptionForFrame:frame]);
}

// just entered a stack frame (i.e. called a function, or started global scope)
- (void)webView:(WebView *)webView    didEnterCallFrame:(WebScriptCallFrame *)frame
       sourceId:(int)sid
           line:(int)lineno
    forWebFrame:(WebFrame *)webFrame {
    if (kDidEnterCallFrame)
        NSLog(@"ScriptDebugger didEnterCallFrame:\
              \nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
              sid,
              lineno,
              [self functionNameForFrame:frame],
              [self callerForFrame:frame],
              [self exceptionForFrame:frame]);
}

// about to execute some code
- (void)webView:(WebView *)webView willExecuteStatement:(WebScriptCallFrame *)frame
       sourceId:(int)sid
           line:(int)lineno
    forWebFrame:(WebFrame *)webFrame {
    if (kWillExecuteStatement)
        NSLog(@"ScriptDebugger willExecuteStatement:\
              \nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
              sid,
              lineno,
              [self functionNameForFrame:frame],
              [self callerForFrame:frame],
              [self exceptionForFrame:frame]);
}

// about to leave a stack frame (i.e. return from a function)
- (void)webView:(WebView *)webView   willLeaveCallFrame:(WebScriptCallFrame *)frame
       sourceId:(int)sid
           line:(int)lineno
    forWebFrame:(WebFrame *)webFrame {
    if (kWillLeaveCallFrame)
        NSLog(@"ScriptDebugger willLeaveCallFrame:\
              \nsourceId=%d \nline=%d \nfunction=%@, \ncaller=%@, \nexception=%@",
              sid,
              lineno,
              [self functionNameForFrame:frame],
              [self callerForFrame:frame],
              [self exceptionForFrame:frame]);
}

@end

#pragma mark -

@interface UIWebView ()

-(id)setScriptDebugDelegate:(id)delegate;

@end

#pragma mark -

@implementation UIWebView (Debug)

- (void)webView:(id)sender didClearWindowObject:(id)windowObject
       forFrame:(WebFrame*)frame {
    ScriptDebuggerDelegate* delegate = [[ScriptDebuggerDelegate alloc] init];
    objc_setAssociatedObject(sender, @"ScriptDebuggerDelegate", delegate, OBJC_ASSOCIATION_RETAIN);
    [sender setScriptDebugDelegate:delegate];
}

@end

#endif

モノタッチ プロジェクト

AppDelegate.cs

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    [Conditional("DEBUG")]
    [DllImport ("__Internal", EntryPoint = "enableWebInspector")]
    public extern static void EnableRemoteWebInspector ();

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        // It will tell you the port in the console,
        // More info here: http://antony_perkov.blogspot.com/2012/03/debugging-uiwebview-content-in.html
        EnableRemoteWebInspector(); 
        return true;
    }
}    

LinkWith属性を適切に機能させることができなかったので、これをプロジェクト プロパティに入れました。

iPhone ビルド プロジェクトのオプション

シミュレーター

-gcc_flags "-L${ProjectDir}/Native -lNativeLib-arm7 -force_load ${ProjectDir}/Native/libNativeLib-arm7.a"

デバイス

-gcc_flags "-L${ProjectDir}/Native -lNativeLib-i386 -force_load ${ProjectDir}/Native/libNativeLib-i386.a"

カスタム コマンド > ビルド前

指示

シミュレーター

sh ${SolutionDir}/NativeLib/compile-arm "${ProjectConfigName}"

デバイス

sh ${SolutionDir}/NativeLib/compile-arm "${ProjectConfigName}"

作業ディレクトリ

${SolutionDir}/NativeLib

最後に、これらはビルド スクリプトです。

コンパイル-i386

xcodebuild -project NativeLib.xcodeproj -target NativeLib -sdk iphonesimulator -configuration $1 clean build
cp build/$1-iphonesimulator/libNativeLib.a ../ProjectName/Native/libNativeLib-i386.a

コンパイルアーム

xcodebuild -project NativeLib.xcodeproj -target NativeLib -sdk iphoneos -arch armv6 -configuration $1 clean build
cp build/$1-iphoneos/libNativeLib.a ../ProjectName/Native/libNativeLib-arm6.a
xcodebuild -project NativeLib.xcodeproj -target NativeLib -sdk iphoneos -arch armv7 -configuration $1 clean build
cp build/$1-iphoneos/libNativeLib.a ../ProjectName/Native/libNativeLib-arm7.a

これはこれを行うための最良の方法ではないかもしれませんが、うまく機能します。
知っている場合は、より簡単な解決策を自由に投稿してください。

于 2012-07-17T19:05:42.933 に答える