ios - 将链接放在 UIWebView 中并显示在不同的 View Controller 上

标签 ios objective-c uiwebview uiwebviewdelegate

我有一个 View Controller ,在其中使用 Apple 的地址簿创建了一个简单的联系人应用程序,我创建了另一个 View Controller ,它有一个 Web View ,当用户单击生成按钮时,该 View 创建并显示所有联系人和电话号码。现在我需要的是,在生成联系人时应该是超链接,当我单击任何联系人时,它应该显示我在第一个 View Controller 中完成的联系人的详细信息。我知道我必须使用webview的删除,但不知道如何实现它,到目前为止我就写了这些。

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    ViewController *latestView = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    [self.navigationController pushViewController:latestView animated:YES]; 
    return YES;
}

这是在 webview 中生成并显示联系人的功能,在这里我想知道如何添加超链接以及联系人详细信息

- (IBAction)createFileAction:(id)sender {



    NSString *tweet, *phoneNumbers=@"", *temp, *allContacts=@"";
    addressBooks =ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBooks);
    for (CFIndex i = 0; i < CFArrayGetCount(people); i++)
    {
        person12 = CFArrayGetValueAtIndex(people, i);
         tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        //Appending Last Name
        if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonLastNameProperty))!=NULL)
        {
            tweet=[tweet stringByAppendingString:@" "];
            tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonLastNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
        }


        tweet=[@"<h5>" stringByAppendingString:[NSString stringWithFormat:@"%@",tweet]];
        if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonFirstNameProperty))!=NULL)
        {


            ABMultiValueRef multi = ABRecordCopyValue(person12, kABPersonPhoneProperty);
            if(ABMultiValueGetCount(multi)!=0)
            {
            //For fetching multiple Phone Numbers
            for (int i=0; i < ABMultiValueGetCount(multi); i++) 
                {
            temp = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, i);
                phoneNumbers=[phoneNumbers stringByAppendingString:[NSString stringWithFormat:@"%@/",temp]];
                }



            //For Trimming characters in contacts (),-," "

            NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"()-\" \""];
            phoneNumbers = [[phoneNumbers componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
            phoneNumbers=[phoneNumbers substringToIndex:[phoneNumbers length]- 1];
            if([phoneNumbers rangeOfString:@"/"].location!=NSNotFound)
            {
                phoneNumbers = [phoneNumbers stringByReplacingOccurrencesOfString:@"/" withString:@" / "];
            }
            }
            else
            {
                phoneNumbers=[phoneNumbers stringByAppendingString:@"No Numbers"];

            }

            tweet=[tweet stringByAppendingString:@" ---> "];
            tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",phoneNumbers] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
            phoneNumbers=@"";

            NSLog(@"%@",tweet); 

        allContacts = [allContacts stringByAppendingFormat:@"%@</h5>",tweet];
        }

    }
    CFBridgingRelease(people);


        // To create file
        [self.fileMgr createFileAtPath:file contents:nil attributes:nil];

        [allContacts writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];


        //Test if file exists now that we have tried creating it
        if([self.fileMgr fileExistsAtPath:self.file])
        {

            NSString *content = @"Contact list generated click on view to show the list";
            [webview loadHTMLString:content baseURL:nil];
        }
        else 
        {

            NSString *content = @"Contact list is not created yet, click on create to generate the contact list";
            [webview loadHTMLString:content baseURL:nil];
        }



}

您能否建议对这两个代码进行修改以帮助我实现结果?

最佳答案

如果我理解正确的话,您想要打开 HTML 链接和 ViewController 实例。基本上您可以创建任何类型的链接 <a href="myapp://{some_id_of_the_person}">click here</a> ,假设您想将人员的 id 传递给您的 ViewController。

然后在[UIWebView webView:shouldStartLoadWithRequest:navigationType:]检查是否 request.URL.scheme与示例中的方案 ( myapp:// ) 匹配:

  • 如果是,则解析此人 ID 的 URL 并打开该人的 viewController(以及该方法的 return NO)。
  • 如果不只是 return YES (假设 View 中可能还有一些其他应该正常打开的标准链接)。

关于ios - 将链接放在 UIWebView 中并显示在不同的 View Controller 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18103933/

相关文章:

image - 在 UIWebView iOs 中调整字体大小和图像

ios - iphone(ios) 如何在服务器上上传和下载数据

ios - 更改 UIView 在 TableView 单元格中的位置

ios - 如何检测 UISwipeGestureRecognizer 的结束?

iOS7 UIPickerView/UIScrollView

iphone - UIWebView loadRequest 导致模拟器崩溃

iphone - 从 UIViewController 导航回来导致崩溃

IOS字符串长度比较问题

ios - 多个 stringByReplacingOccurrencesOfString 的有效方法

ios - iPhone:下载图像然后在带有@2x 扩展名的 UIImageView 中显示