【问题标题】:Opening youTube in iOS not work在 iOS 中打开 YouTube 不起作用
【发布时间】:2016-06-10 20:04:13
【问题描述】:

我正在尝试在我的应用中创建一个 youtube 应用

加载webView的mt源码为:

ViewController 头文件:

@interface ViewController : UIViewController

@property (nonatomic, strong) UIWebView *webView;

@end

ViewController 实现文件 - (void)viewDidLoad { [超级视图DidLoad]; // 在加载视图后进行任何额外的设置,通常从 nib 开始。

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
self.webView.layer.borderWidth = 1.0;
self.webView.layer.borderColor = [UIColor redColor].CGColor;

self.webView.backgroundColor = [UIColor blackColor];

self.webView.center = self.view.center;

[self.view addSubview:self.webView];

NSString* embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
    background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

NSURL *url = [NSURL URLWithString:@"https://www.youtube.com/watch?v=K14RnTVt194"];

NSString* html = [NSString stringWithFormat:embedHTML, url, self.webView.frame.size.width, self.webView.frame.size.height];

[self.webView loadHTMLString:html baseURL:nil];

}

运行时,我的应用程序崩溃并显示以下屏幕

如何解决这个问题?

【问题讨论】:

    标签: ios youtube


    【解决方案1】:

    在我看来,崩溃出现在您的 stringWithFormat 调用中,因为尝试用 NSURL 对象而不是 NSString 替换 embedHTML 中的“%@”。

    【讨论】:

      【解决方案2】:

      您的代码运行良好,因此问题不是因为 webview。您可以在左侧面板上看到“Thread 1”,线程崩溃前的最后一个进程显示“__psynch_mutexwait”。在某些情况下,您可能会遇到此错误;

      1. 设置错误NSTimer
      2. 设置错误或无法管理NSNotification(尤其是 Key-Value-Observer)
      3. 其中一个对象尝试访问后台线程上的CoreData 对象,同时使用主线程上下文或同时在不同线程(后台或主线程)上使用相同的托管对象上下文。

      尝试在其中一种情况下检查您是否做错了什么。另外,尝试异步操作;

      dispatch_async(dispatch_get_current_queue(), ^{
          // The work that needs to be done on main thread
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-18
        • 2018-09-04
        • 1970-01-01
        • 2016-03-23
        • 2013-05-14
        • 2017-06-23
        • 2015-07-07
        • 2013-05-17
        相关资源
        最近更新 更多