【问题标题】:iPhone's Pasteboard not recognizing a URLiPhone 的粘贴板无法识别 URL
【发布时间】:2011-12-15 23:31:40
【问题描述】:

我希望我的应用程序能够识别剪贴板的当前内容是否是 URL,如果是,我希望它将该 URL 加载到 Web 视图中。
我正在使用以下语句进行此检查:

 if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:@"public.url-name"]]) 
     ...code to load the URL into the webView

但它不起作用 - IF 语句始终返回 FALSE,即使剪贴板的内容显然是 URL。
奇怪的是,当我删除这个 IF 语句并继续将我从剪贴板读取的 URL 加载到 webView 中时,它工作得很好。所以它肯定只是 IF 语句由于某种原因不起作用。

这是完整的代码:

// executed on a Button-click:
-(IBAction) showClipBoard {
    pasteboard = nil;  // resetting the pasteBoard each time
    pasteboard = [UIPasteboard generalPasteboard];
    NSURL *tempURL = pasteboard.URL;    

    //Check the pasteboard's value-type:
    if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:@"public.url-name"]]) {
      NSLog(@"URL is: %@", pasteboard.string);       
      NSURL *url = [NSURL URLWithString: pasteboard.string];
      NSURLRequest *req = [NSURLRequest requestWithURL: url];
      [webView loadRequest:req];   
  }
else 
    NSLog(@"=NOT a valid web-address");

NSString *tempString = [pasteboard valueForPasteboardType:@"public.utf8-plain-text"];

// Show the URL in a text-view box called "clipText":
clipText.text = tempString;

}

有人知道这里有什么问题吗?

【问题讨论】:

    标签: iphone url uiwebview clipboard


    【解决方案1】:

    您现在可能已经想通了,但是,我遇到了同样的问题,以下对我有用:-

    if ([pasteboard containsPasteboardTypes: [NSArray arrayWithObject:@"public.url"]]) 
     ...code to load the URL into the webView
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2017-04-16
      • 2010-11-08
      相关资源
      最近更新 更多