Objective-C UIWebView初始化错误

时间:2016-03-13 08:38:13

标签: ios objective-c iphone

当我尝试执行这段代码时,我的应用程序崩溃了。我是Objective-C的新手,所以我不确定问题是什么。

   - (void)loadWebView:(NSURL *)url {
      UIWebView *nWeb = _webView;
      if(nWeb == nil) {
       nWeb =[[UIWebView alloc]init];
      }
      [[self webView] loadRequest:[NSURLRequest requestWithURL:url]];
      self.view = nWeb;
    }

错误:

2016-03-13 08:32:39.885 [info][tid:com.facebook.React.JavaScript] Running application "TheNativeMobileWeb" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
2016-03-13 08:32:41.259 TheNativeMobileWeb[82483:9025502] VIEWCONTROLLER YEAH changed
2016-03-13 08:32:41.259 TheNativeMobileWeb[82483:9025502] https://facebook.github.io/react-native/
2016-03-13 08:32:41.259 TheNativeMobileWeb[82483:9025502] https://facebook.github.io/react-native/
1   0x11873ba2e StartWebThread()
2   0x110bb0eaf __pthread_once_handler
3   0x110bc5f13 _os_once
4   0x110bb0e4e pthread_once
5   0x11773925e +[WebView(WebPrivate) enableWebThread]
6   0x117733616 WebKitInitialize
7   0x10ebf3efb ___UIApplicationLoadWebKit_block_invoke
8   0x11087949b _dispatch_client_callout
9   0x110864e28 dispatch_once_f
10  0x10c4dabff _class_initialize
11  0x10c4e0d23 lookUpImpOrForward
12  0x10c4ef8bb objc_msgSend
13  0x10bd73fc0 -[ViewController loadWebView:]
14  0x10bd73db3 -[ViewController loadURL:]
15  0x10bd73cc0 -[ViewController changedURL:]
16  0x10d2a269c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
17  0x10d2a23db _CFXRegistrationPost
18  0x10d2a2142 ___CFXNotificationPost_block_invoke
19  0x10d2e7972 -[_CFXNotificationRegistrar find:object:observer:enumerator:]

1 个答案:

答案 0 :(得分:0)

可能是url中的问题,你可以打印url并直接在浏览器中执行,然后就可以理解了。

你试试这个,

    NSString *theOrigString = @"your_url_string";
    NSString *formattedString = [theOrigString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@", formattedString);

    - (void)loadWebView:(NSURL *)url {
          UIWebView *nWeb = _webView;
          if(nWeb == nil) {
           nWeb =[[UIWebView alloc]init];
           //Set WebView Frame
           nWeb.frame = CGRectMack(0,0,320,480); 
          }
          [nWeb loadRequest:[NSURLRequest requestWithURL:url]];
          self.view = nWeb;
        }

我希望能为你提供帮助。