问题
我设法按照教程开始工作,并且可以在设备之间成功传输数据。但是,我现在正试图在我的应用程序中实现它。
问题是,它不会在我的HostViewController
中刊登广告。
我可能错过了一些非常简单的东西,但我找不到问题。
HostViewController~广告商。
#import "HostViewController.h"
#import "AppDelegate.h"
@interface HostViewController ()
@property (nonatomic,strong) AppDelegate *appDelegate;
@end
@implementation HostViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[_matchName setDelegate:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)createMatchup:(id)sender {
_appDelegate.mcManager.peerID = nil;
_appDelegate.mcManager.session = nil;
_appDelegate.mcManager.browser = nil;
_appDelegate.mcManager.advertiser = nil;
[_appDelegate.mcManager.advertiser stop];
[_appDelegate.mcManager setupPeerAndSessionWithDisplayName:_matchName.text];
[_appDelegate.mcManager setupMCBrowser];
[_appDelegate.mcManager advertiseSelf:true];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[_matchName resignFirstResponder];
return YES;
}
@end
ConnectionViewController~导致浏览器。
#import "ConnectionsViewController.h"
#import "AppDelegate.h"
#import "LobbyViewController.h"
@interface ConnectionsViewController ()
@property (nonatomic, strong) AppDelegate *appDelegate;
@end
@implementation ConnectionsViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[_appDelegate mcManager] setupPeerAndSessionWithDisplayName:[UIDevice currentDevice].name];
[[_appDelegate mcManager] advertiseSelf:false];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)browseForDevices:(id)sender {
[[_appDelegate mcManager] setupMCBrowser];
[[[_appDelegate mcManager] browser] setDelegate:self];
[self presentViewController:[[_appDelegate mcManager] browser] animated:YES completion:nil];
}
-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController{
[_appDelegate.mcManager.browser dismissViewControllerAnimated:NO completion:nil];
//Here go to match lobby.
LobbyViewController *lobby = [[LobbyViewController alloc] init];
[self presentViewController:lobby animated:YES completion:nil];
}
-(void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController{
[_appDelegate.mcManager.browser dismissViewControllerAnimated:YES completion:nil];
}
@end
最后,;
MCManager~处理事件
@implementation MCManager
-(id)init{
self = [super init];
if (self) {
_peerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
return self;
}
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{
_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
_session = [[MCSession alloc] initWithPeer:_peerID];
_session.delegate = self;
}
-(void)setupMCBrowser{
_browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:_session];
}
-(void)advertiseSelf:(BOOL)shouldAdvertise{
if (shouldAdvertise) {
_advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
discoveryInfo:nil
session:_session];
[_advertiser start];
}
else{
[_advertiser stop];
_advertiser = nil;
}
}
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
}
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
}
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress{
}
-(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error{
}
-(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID{
}
@end
主机视图控制器是@interface HostViewController : UIViewController <MCAdvertiserAssistantDelegate,UITextFieldDelegate>
Connection ViewControiller是@interface ConnectionsViewController : UIViewController <MCBrowserViewControllerDelegate>
然而,在-(IBAction)createMatchup:(id)sender
运行后,我看不到设备/比赛名称。
摘要
createMatchup
运行后,浏览设备未显示设备/比赛名称。
答案 0 :(得分:0)
- (void)viewDidLoad {
....
_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
在 HostViewController。
的viewdidload中知道它很小= [